I Kings - Chapter 3
Solomon married the daughter of the Pharaoh in order to cement a treaty with Egypt. (He married the daughters of many other nations. They converted, but were insincere, as we will see.) Being faithful to G-d, Solomon traveled to Gibeon to offer sacrifices. While he was there, G-d appeared and offered to grant Solomon whatever he desired. All Solomon requested was the wisdom to rule his people well. He could have asked for riches or to subjugate his enemies, but he didn't; G-d gave him these things anyway. Solomon was promised honor and wealth all his life, unconditionally. (He was also promised a long life if he lived up to David's example, but he fell short of that goal.)
When Solomon awoke, he thought his "conversation" with G-d was just a dream, rather than a true prophecy. According to the Midrash, he found that he could understand the language of the animals and birds. Solomon returned to Jerusalem and made a feast.
Soon it was time to put his prowess to the test. Two prostitutes shared a house and they had given birth within days. But one of the babies had died. One woman said that the other switched the live baby for her own dead son; the other said the live boy was truly her own. Solomon did the only rational thing one can do when presented with such a situation: he called for a sword.
Sword brought, Solomon then said, "Cut the living baby in two and give half to each woman." One woman screamed, "What are you, nuts? Give it to her!" (Loosely paraphrased.) The other woman said, "Let it be neither mine nor hers! Cut away!" From their reactions, Solomon knew that the first woman, who had compassion and preferred that the child be given away and live, was the true mother. (The other woman didn't really want the child, she preferred that the mother of the living baby share her pain.) Everyone was impressed with the test Solomon had devised to discover the true mother's identity and his reputation spread.