The winter solstice has long been an important festival in Northern Europe — in fact it was celebrated for many thousands of years before the Christians adopted it and rebranded it as Christmas. The first settlers in New Zealand came from tropical countries, so the solstices were not an issue for them. It was not until the arrival of later settlers that Christmas became important. However, as with many imports, something was lost in translation — in this case the timing. Midsummer is a time of holidays and beach parties, and no one wants to be reminded of the impending arrival of winter, particularly in the far south of the country.
Because of this, the residents of Oban on Stewart Island decided this year to hold a midwinter party. (They enjoy parties and figure that any excuse is better than none.) By custom, everyone was asked to bring along a small gift. The gifts were distributed essentially at random, but someone had the bright idea of recording the donor and the recipient of each parcel. In this community everyone took part: everybody brought a present and everybody received one, so there must have been at least one complete cycle of giving (A gave to B, B gave to C, …, someone gave back to A). Determining these cycles was a bit beyond the partygoers, at least in the immediate aftermath — so that is where you come in.
Write a program that reads the details of the gifts and determines all the cycles of giving. You may assume that everybody gives and receives exactly one present.
The input describes several parties, each containing details of two or more gifts. Each gift appears as a pair of names on one line, separated by a single space; the first person gave a gift to the second person. Each name contains no more than 20 letters. There are never more than 200 people at a party. The list of gifts for a party is terminated by a line consisting of a single # character. The whole sequence of parties is also terminated by such a line.
For each party, output a heading line followed by one or more cycle lines. The heading line consists of the words Party number followed by a space and the party number (a running number starting at 1). A cycle line is a series of names separated by the word to (with a space on each side) and terminated by a period (.). The first and last name in any cycle must be the same. The cycles must be listed so that their first names appear in the same order as those names first appear in the input. Separate successive parties with a blank line.