Under construction

Still being built. Things may change

Sort a scrambled itinerary (Large)

Interview

Time limit5sMemory limit512 MB

Summary
Rebuild the flight order by chaining tickets from the starting city that never appears as a destination.
Level

Easy3 of 10

Topics
Hash map
Solved
No attempts yet

Problem

Mary bought a bundle of one way tickets that take her through several transfers. For example, SFO to DFW, DFW to JFK, JFK to MIA, MIA to ORD.

Transferring at the same city twice or more makes no sense, so Mary never buys such an itinerary.

After she received the tickets she shuffled them and forgot the original order. Write a program that sorts the shuffled tickets back into the real itinerary order.

Input

The first line contains the number of test cases TT. The TT test cases follow.

The first line of each test case contains the number of tickets NN. Then NN tickets follow, and each ticket takes two lines: the source on the first line and the destination on the second line. An airport code is three uppercase letters.

Output

For each test case, print one line in the form Case #x: itinerary, where xx is the test case number starting from 1 and itinerary is the list of tickets sorted into the real itinerary order. Write each ticket as source-destination and separate consecutive tickets with a single space.

Constraints

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1041 \le N \le 10^4
  • The tickets of a test case are a shuffle of one itinerary Mary bought, so they always rebuild into one valid itinerary.
  • No city is visited twice inside a test case, so the answer is unique.

Examples2

  1. Example 1

    Input
    2
    1
    SFO
    DFW
    4
    MIA
    ORD
    DFW
    JFK
    SFO
    DFW
    JFK
    MIA
    
    Expected output
    Case #1: SFO-DFW
    Case #2: SFO-DFW DFW-JFK JFK-MIA MIA-ORD
    
  2. Example 2

    Input
    1
    1
    ICN
    NRT
    
    Expected output
    Case #1: ICN-NRT