Sort a scrambled itinerary (Small)
InterviewTime limit5sMemory limit512 MB
Rebuild each shuffled set of flight tickets into the single chain where each arrival matches the next departure.
Problem
Mary bought a bundle of one way flight tickets that take her through several cities with transfers. For example SFO->DFW, DFW->JFK, JFK->MIA, MIA->ORD, where the arrival airport of each segment is the departure airport of the next one.
Transferring through the same city twice makes no sense, so Mary never buys such an itinerary.
After she received the tickets she shuffled them and forgot the original order. Sort the shuffled tickets back into the real travel order.
Input
The first line contains the number of test cases . Then test cases follow.
The first line of each test case contains the number of tickets . The next lines contain the tickets in shuffled order. One ticket takes two lines: the first line is the departure airport code and the second line is the arrival airport code. An airport code is three uppercase letters.
Output
For each test case print one line in the form "Case #x: itinerary", where is the test case number starting from 1 and itinerary is the list of segments in real travel order.
Write each segment as the departure code and the arrival code joined by a hyphen, for example "SFO-DFW", and separate consecutive segments with a single space.
Constraints
- The tickets of each test case are one itinerary Mary bought, shuffled, so they always rebuild into one valid itinerary.
- Within a test case the itinerary never visits the same city twice, so the answer is unique.