The present invention provides heterocyclic-substituted pyrrolopyridines and pyrrolopyrimidines of Formula I:wherein X, Y, Z, L, A, R5, n, m, and r are defined above, 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.