Under construction

Still being built. Things may change

Sort a scrambled itinerary (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Rebuild each shuffled set of flight tickets into the single chain where each arrival matches the next departure.
Level

Easy3 of 10

Topics
Hash map, Graph
Solved
No attempts yet

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 TT. Then TT test cases follow.

The first line of each test case contains the number of tickets NN. The next 2N2N lines contain the NN 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 xx 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

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1001 \le N \le 100
  • 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.

Examples3

  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
    
  3. Example 3

    Input
    1
    4
    CCC
    MMM
    BBB
    CCC
    AAA
    BBB
    ZRH
    AAA
    
    Expected output
    Case #1: ZRH-AAA AAA-BBB BBB-CCC CCC-MMM