The present invention provides heterocyclic derivatives of pyrazol-4-yl-pyrrolo[2,3 djpyrimidines of Formula Ia or a pharmaceutically acceptable salt thereof; wherein: A is H, C1-6 alkyl, C2-6 alkenyl, C2-6 alkynyl, C3-I4 cycloalkyl, C2-13 heterocycloalkyl, C6-14 aryl, C1-14 heteroaryl, C3-14 cycloalkyl-C1-4 alkyl, C2-13 heterocycloalkyl-C1-4 alkyl, C6-14 aryl-C1-4 alkyl, or C14 heteroaryl-C14 alkyl, wherein said C1-6 alkyl, C2-6 alkenyl, C2-6 alkynyl, C3-14 cycloalkyl, C2-13 heterocycloalkyl, C6-14 aryl, C1-14 heteroaryl, C3-14 cycloalkyl-C1-4 alkyl, C2-13 heterocycloalkyl-C1-4 alkyl, C6-14 aryl-C1-4 alkyl, and C1-14 heteroaryl-C1-4 alkyl are each optionally substituted with I, 2, 3, 4, 5, or 6 independently selected R8 substituents; L is absent, C(=O), C(=O)NH, S(=O), or S(O)2; X is CH or N; Y is H, cyano, halo, C1-4 alkyl, or C1-4 haloalkyl; Z is CR7 or N; R1, R2, and R3 are each independently H, hydroxyl, halo, C1-3 alkyl, or C1-3 haloalkyl; R4 and R5 are each independently H, C1-3 alkyl, or C1-3 haloalkyl; or R4 and R5 together with the carbon atom to which they are attached can form a 3-, 4-, 5-, 6- υr 7-membered cycloalkyl ring; as well as their compositions and methods of use, that modulate the activity of Janus kinases (JAKs) and are useful in the treatment of diseases related to the activity of JAKs including, for example inflammatory disorders, autoimmune disorders, cancer, and other diseases.