SCAVHUNT - Scavenger Hunt

Bill has been the greatest boy scout in America and has become quite a superstar because he always organized the most wonderful scavenger hunts (you know, where the kids have to find a certain route following certain hints). Bill has retired now, but a nationwide election quickly found a successor for him, a guy called George. He does a poor job, though, and wants to learn from Bill’s routes. Unfortunately Bill has left only a few notes for his successor. Bill never wrote his routes completely, he only left lots of little sheets on which he had written two consecutive steps of the routes. He then mixed these sheets and memorized his routes similarly to how some people learn for exams: practicing again and again, always reading the first step and trying to remember the following. This made much sense, since one step always required something from the previous step. George however would like to have a route written down as one long sequence of all the steps in the correct order. Please help him make the nation happy again by reconstructing the routes.

Input

The first line contains the number of scenarios. Each scenario describes one route and its first line tells you how many steps (3 ≤ S ≤ 333) the route has. The next S − 1 lines each contain one consecutive pair of the steps on the route separated by a single space. The name of each step is always a single string of letters.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print S lines containing the steps of the route in correct order. Terminate the output for the scenario with a blank line.

Example

Input:
2
4
SwimmingPool OldTree
BirdsNest Garage
Garage SwimmingPool
3
Toilet Hospital
VideoGame Toilet

Output:
Scenario #1:
BirdsNest
Garage
SwimmingPool
OldTree

Scenario #2:
VideoGame
Toilet
Hospital

Added by:Daniel Gómez Didier
Date:2008-11-17
Time limit:1.356s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO
Resource:2007 - Circuito de Maratones ACIS / REDIS

hide comments
2017-02-01 09:40:53
easy one my 200th
2016-08-08 20:28:25
And STL it is ! :)
2016-06-27 19:12:51 Piyush Kumar
The question can be made more interesting with tighter constraints!

=(Francky)=> If you think there are no similar problem with harder constraints ; feel free to set a new edition with finely tuned constraints. As always, if possible, in order to allow slow languages, and reject fast language with bad complexity... Use resource field for credit.

Re: Thanks! Generating test data for this will be tiresome, I will see what I can do ! :)

Last edit: 2016-06-28 08:34:45
2015-04-17 13:52:58 krish
too easy...use topological sort.
2015-03-30 10:47:04 SangKuan
i use a hash and single linked list...get ac
2012-03-11 15:13:48 Kemal Maulana
My AC code always prints a blank line after each scenario
2011-11-08 14:02:32 I Made Krisna Widhiastra
WA, can someone provide another testcases,

Is the last output also print a blank line?

Thanks...
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.