In the Mahabharata, Arjuna is frequently described as Kadipdhvaja, or the one who has a monkey flag. In the popular imagination, this flag is associated with Hanuman. And this association does have a scriptural basis; in Bhima's encounter with Hanuman, which I discuss in my answer here, Hanuman tells him that during the battle of Kurukshetra he will emit shouts from the flag of Arjuna's chariot to intimidate the Kauravas. See this chapter of the Vana Parva of the Mahabharata:
From fraternal feeling and affection, I will do good unto thee, by diving into the army of thy foes copiously furnished with arrows and javelins. And, O highly powerful one, O hero, when thou shall give leonine roars, then shall I with my own, add force to shouts. Remaining on the flagstaff of Arjuna's car will I emit fierce shouts that will damp the energy of thy foes. Thereby ye will slay them easily.
But my question is, what is the origin of Arjuna's monkey flag? Was it always a flag associated with Hanuman, or did Hanuman just enter it during the Mahabharata war?
The story I've always heard about the monkey flag is the one told in the Bengali Mahabharata and in this excerpt from the Amar Chitra Katha comic book "Tales of Arjuna". Arjuna once goes on a pilgrimage to Rameshwaram, at the southern tip of India, and sees Rama's bridge. He boasts that if he were Rama, he wouldn't need the help of the Vanaras in building a bridge, he could make a sturdy bridge to Lanka with his arrows. Now Hanuman happens to be in the area, so he takes the form of a small monkey and makes a wager with Arjuna that if Arjuna builds a bridge of arrows, he wil be able to break it. The conditions are that if Arjuna wins then the monkey becomes his slave, and if the monkey wins then Arjuna has to commit suicide. In any case, Arjuna builds a bridge of arrows, but then the monkey just touches it and it breaks. Arjuna is devastated, and is about to jump into a funeral pyre when a Brahmana boy comes there and suggests that Arjuna and the monkey have a rematch, with the boy being the judge. They agree, and Arjuna makes another bridge. This time Hanuman is not able to break it, even when he becomes gigantic. It is at that moment that Hanuman realizes that the boy is nothing but his beloved Rama, and Arjuna realizes the boy is his beloved Krishna. In any case, Krishna resolves the dispute by telling Hanuman to appear on the flag of Arjuna's chariot, to fulfill the conditions of the bet.
But I'm not aware of any scriptures that describe this story. And also, as I discuss in this question, Arjuna received his chariot from Varuna the ocean god, who got it from Chandra the moon god. And when he got it, it already had the monkey flag; see this chapter of the Adi Parva of the Mahabharata:
The smoke-bannered celestial [Agni] welcoming with reverence [Varuna] the ruler of the waters, that fourth of the Lokapalas, said unto that eternal god of gods, "Give me without loss of time that bow and quiver, and that ape-bannered car also, which were obtained from king Soma." ... Varuna also gave two inexhaustible quivers, and he also gave a car furnished with celestial weapons and whose banner bore a large ape.... It had been made by Viswakarman, the architect of the universe and one of the lords of creation, after severe ascetic meditation. Its splendour, like that of the sun, was so great that no one could gaze at it. It was the very car from which the lord Soma had vanquished the Danavas. Resplendent with beauty, it looked like an evening cloud reflecting the effulgence of the setting sun. It was furnished with an excellent flag-staff of golden colour and great beauty. And there sat upon that flag-staff a celestial ape of form fierce like that of a lion or a tiger. Stationed on high, the ape seemed bent upon burning everything it beheld.
So did the chariot have the monkey flag from the time it was made by Vishwakarma and used by Chandra, and if so who does it depict? Was it always meant to depict Hanuman, or does it depict some other "celestial ape" and did Hanuman just enter into the flag during the Mahabharata war? It could depict Vrishakapi, an ape described in the Vedas who was a ally of Indra but also had an affair with Indra's wife Shachi.
In any case, does anyone know any scriptures which describe either Arjuna's encounter with Hanuman or some other origin of Arjuna's monkey flag?
EDIT: The user sv. just pointed me to this chapter of the Virata Parva of the Mahabharata, where Dronacharya recognizes Arjuna while the latter is dressed like a woman. Here's what Dronacharya says:
O offspring of the Ganges, apparelled as a woman, this is Kiriti called after a tree, the son of the enemy of the mountains, and having on his banner the sign of devastator of the gardens of Lanka's lord.
Now the translator claims this verse does not appear in all manuscripts, but assuming it's authentic it's further evidence that Arjuna's monkey flag was always associated with Hanuman, who is the one who burned the gardens of Ravana. But on the other hand, it makes it all the more strange that the flag was already on the chariot when Varuna gave it to Arjuna.