AlgoUniversity
  • Go Back
Discussion
The Travaler :

Author

Gurankit Pal Singh

Difficulty Level : Easy

Submissions : 234

Asked In :

Marks :10

: 0 | : 0

There is a traveler who wants to travel to different places.

Given the edges between various cities, he wants to travel in a unique pattern.

Given a string $$$firstCity$$$, he decided to start from the $$$firstCity$$$, and visit its neighbor city which comes first in the list of edges. After visiting all the cities connected to a particular city, he will move to the next city in the list and repeat the same till all cities are visited.

You have to print the order of cities in which the traveler will travel.

Input

The fist line contains an integer $$$n - (1 \le n \le 10^5)$$$, denoting the number of edges.

The next $$$n$$$ lines contain strings $$$city1$$$ and $$$city2$$$, denoting a directed edge from $$$city1$$$ to $$$city2$$$. $$$(1 \le |city1|,|city2| \le 20)$$$

The last line contains a string $$$firstCity$$$, denoting the starting city. $$$(1 \le |firstCity| \le 20)$$$

Output

Print a single string in each line, denoting the order in which the traveler will travel.

Example

Input
5
Banglore Hyderabad
Banglore Chennai
Hyderabad Mumbai
Hyderabad Delhi
Chennai Kerala
Banglore
Output
Banglore
Hyderabad
Chennai
Mumbai
Delhi
Kerala

Note

In the sample case, the traveler will start from Bangalore, and first visits all its neighbors.

After Bangalore, he will visit Hyderabad, and then Chennai (as Hyderabad comes in the list earlier than Chennai). Then as he has visited Hyderabad earlier than Chennai, he will visit the neighbors of Hyderabad first, and then the neighbors of Chennai.

So the final ordering will be:

  • Bangalore
  • Hyderabad
  • Chennai
  • Mumbai
  • Delhi
  • Kerala

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.