"And they (B'nei Yisroel) turned and ascended toward Bashan, and Og, King of Bashan, went out toward them, he and his nation, to battle them at Edre'i. And Hashem said to Moshe, 'Do not fear him, for I have given him and his nation and his territory into your hand...'" (Bamidbar 21:33-34) Rashi, invoking the interpretation of Midrash Tanchuma, explains that Moshe was apprehensive about battling Og, for Og is identified as the refugee from the War Between the Kings who informed Avrohom Avinu that Lot was taken captive and thereby enabled Lot's rescue (Bereshis 14:13); Moshe feared that the merit of Avrohom would serve to protect Og in the impending battle with B'nei Yisroel.
There appears to be a major contradiction posed by this explanation, for Rashi (on Bereshis ibid.) quotes Midrash Bereshis Rabbah that Og's intent in informing Avrohom about the capture of Lot was insincere and actually nefarious; Og's intent was for Avrohom to try to rescue Lot and to be killed in the process, for such rescue would entail entering the War Between the Kings and fighting against the stronger side. That being the case, what merit could have accrued to Og such that Moshe feared battling him? Og was evil, having attempted to cause the death of Avrohom, and, on the contrary, one would expect Og's actions regarding Avrohom and Lot to be a major discredit to him.
Chizkuni and Sifsei Chachamim (on Bamidbar 21:34) address this question and invoke the Gemara in Horayos (10b), which states that the performance of a mitzvah, even for a negative purpose, can yield substantial and eternal reward. As such, since Og prompted Avrohom to perform the mitzvah of rescuing Lot, Moshe was correct to suspect that significant merit accrued to Og, and this merit would protect him in battle against B'nei Yisroel.
This approach, however, seems quite perplexing, as why would any merit associated with Og be so great that it could protect him in battle against Hashem's Chosen People who were performing the mitzvah of conquering Eretz Yisroel? How could the merit of Og, accrued with malicious and murderous intent, override the wellbeing and safety of B'nei Yisroel as they attempted to perform a mitzvah? Was Og's merit truly so great?
An alternative approach may be suggested. On his own, even as a result of promoting Avrohom to rescue Lot, Og had inadequate merit to successfully battle B'nei Yisroel. However, a close look at the language of Rashi indicates that any merit associated with Og needs to be understood differently.
Rashi (on Bamidbar ibid.) writes that "Moshe feared that the zechus (merit) of Avrohom may stand in Og's favor". In other words, Moshe did not fear that the merit of Og would protect him, but rather, Moshe feared that the merit of Avrohom, which is great and eternal, would extend to Og, due to Og having benefited Avrohom by enabling him to rescue Lot. (V. Sefer Mesillas Yesharim [ch. 19] regarding the merit of the tzaddikim of the generation extending to others.) Moshe suspected that just as the merit of Avrohom extended to Lot and facilitated the latter's rescue from S'dom (v. Rashi on Bereshis 19:17, from Midrash Rabbah), due to Lot's refusal in Mitzrayim to disclose that Avrohom was Sarah's husband, irrespective of Lot's many other negative qualities, so too did Og's promoting Avrohom to rescue Lot cause the merit of Avrohom to extend to Og, irrespective of Og's overall evil ways.
Hashem advised Moshe that such was not at all the case. The merit of a tzadddik can extend to others if they identify with the tzadddik and embody his values and deeds, to whatever degree, for the good values and deeds of such people are a constant reflection of the influence of the tzaddik upon their lives. However, one whose life is the antithesis of the values and deeds of a tzadddik does not enjoy the extension of his merit, for such a person's life is not a reflection of the influence of the tzaddik and has nothing to do with the tzaddik. Lot, despite having veered from the ways and values of Avrohom, nonetheless deep down always held fast to that which Avrohom represented. The selfless hachnasas orchim (hospitality/welcoming of guests) exhibited by Lot, and his rock-solid belief in Hashem upon fleeing S'dom, were a reflection of the continuing and eternal impact that Avrohom had upon him, despite his having strayed far from Avrohom's path in other major areas of life. This firm, underlying connection between Lot and Avrohom enabled the merit of Avrohom to extend to Lot. On the other hand, despite having prompted Avrohom to rescue Lot, Og did not at all embody the ways of Avrohom; hence would the merit of Avrohom not extend to Lot.
The message here is clear. Although we can benefit and become elevated from association with tzadddikim and talmidei chachamim, such association is really meaningful only if we do our best to learn from them and emulate their ways. In such a case, our actions are a credit to the tzadddikim and talmidei chachamim. However, if we fail to try to absorb and embody the teachings and ways of tzadddikim and talmidei chachamim, our association with them is not so meaningful, and their merit does not truly relate to us.
May our learning and mitzvah performance always be a reflection of the tzadddikim and talmidei chachamim who teach and guide us. Such is surely a win-win relationship.