Rebuild each shuffled set of flight tickets into the single chain where each arrival matches the next departure.
Easy3Hash mapGraphInterviewNo attempts yetTime limit5sMemory limit512 MBMary 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.
The first line contains the number of test cases T. Then T test cases follow.
The first line of each test case contains the number of tickets N. The next 2N lines contain the N 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.
For each test case print one line in the form "Case #x: itinerary", where x 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.