Scavenger Hunt

Interview

Time limit1sMemory limit128 MB

Summary
Given S-1 ordered pairs of steps from a route of S steps, reconstruct the full sequence of steps in order.
Level

Medium4 of 10

Topics
Graph, Hash map, Implementation
Solved
No attempts yet

Problem

Bill was the greatest boy scout leader in America and became quite a star because he always organized the most wonderful scavenger hunts (the game where kids follow hints to find a certain route). Bill has now retired, but a nationwide vote quickly chose his successor, a man named George. George, however, does a poor job and wants to learn from Bill's routes. Unfortunately, Bill left his successor only a few notes.

Bill never wrote down a route in full. Instead, he left many small slips of paper, each showing two consecutive steps of a route. He then shuffled these slips and memorized his routes the way some people study for exams: reading the first step over and over and trying to recall the one that follows. This made a lot of sense, because each step always followed from the previous one.

George would like each route written out as one long sequence of all its steps in the correct order. Please help him by reconstructing the routes from the shuffled slips.

Input

The first line contains the number of scenarios (routes). Each scenario describes one route; its first line gives the number of steps SS (3≤S≤3333 \le S \le 333) on the route. The next S−1S-1 lines each contain one pair of consecutive steps of the route, separated by a single space. The name of each step is always a single string made of letters.

Output

For each scenario, first print a line of the form Scenario #i:, where ii is the scenario number starting from 1. Then print the SS steps of the route in the correct order, one per line. Separate consecutive scenarios with a single blank line; do not print an extra blank line after the final scenario.

Examples3

  1. Example 1

    Input
    2
    4
    SwimmingPool OldTree
    BirdsNest Garage
    Garage SwimmingPool
    3
    Toilet Hospital
    VideoGame Toilet
    
    Expected output
    Scenario #1:
    BirdsNest
    Garage
    SwimmingPool
    OldTree
    
    Scenario #2:
    VideoGame
    Toilet
    Hospital
    
  2. Example 2

    Input
    1
    3
    Apple Banana
    Banana Cherry
    
    Expected output
    Scenario #1:
    Apple
    Banana
    Cherry
    
  3. Example 3

    Input
    1
    3
    Banana Cherry
    Apple Banana
    
    Expected output
    Scenario #1:
    Apple
    Banana
    Cherry