During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.

Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).

After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.

Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.


-----Input-----

The first line contains integer n (2 ≤ n ≤ 2·10^5) — the number of students in the queue. 

Then n lines follow, i-th line contains the pair of integers a_{i}, b_{i} (0 ≤ a_{i}, b_{i} ≤ 10^6), where a_{i} is the ID number of a person in front of a student and b_{i} is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.

The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.


-----Output-----

Print a sequence of n integers x_1, x_2, ..., x_{n} — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.


-----Examples-----
Input
4
92 31
0 7
31 0
7 141

Output
92 7 31 141 



-----Note-----

The picture illustrates the queue for the first sample. [Image]

Difficulty: interview

Starter code:

Solutions:
n=int(input())
d={}
d1={}
for i in range(n):
  a,b=list(map(int,input().split()))
  d[a]=b
  d1[b]=a
r=[0]*n
front=0
i=1
while i<n:
  r[i] = d.get(front)
  front = r[i]
  i += 2
for f in list(d.keys()):
  if d1.get(f) == None:
    front = f
    break
r[0]=front
i=2
while i<n:
  r[i] = d.get(front)
  front = r[i]
  i += 2
print(*r)




n = int(input())
ID = 1000001
thprev = [-1 for i in range(ID)]
thpost = [-1 for i in range(ID)]
prev = []
post = []
for i in range(n):
    a,b = [int(s) for s in input().split()]
    prev.append(a)
    post.append(b)
    thprev[a] = i
    thpost[b] = i
    
sth = thprev[0]
v = post[sth]
q1 = []
while v>0 and sth>=0:      
    q1.append(v)
    sth = thprev[v]
    v = post[sth]

start1 = thprev[0]
for i in range(n):
    if i!=start1 and thpost[prev[i]]==-1:
        start2 = i
        break
    
sth = start2
u = prev[sth]
v = post[sth]
q2 = [u]
while v>0 and sth>=0:      
    q2.append(v)
    sth = thprev[v]
    v = post[sth]


q1.reverse()
q2.reverse()
q = []
label = 2

while q1 or q2:
    if label==1:
        q.append(q1.pop())
        label = 2
    else:
        q.append(q2.pop())
        label = 1
print(' '.join(map(str,q)))
        
    
    
    
    
    
    
    
    

    



n = int(input())
a = {}
b = {}
ans = [0] * n
s = set()
for i in range(n):
    x, y = list(map(int, input().split()))
    a[x] = y
    b[y] = x
    s.add(x)
    s.add(y)
cur = a[0]
for i in range(1, n, 2):
    ans[i] = cur
    if cur in a:
        cur = a[cur]

frst = -1
for i in s:
    if i not in b:
        frst = i
        break
cur = frst
i = 0
for i in range(0, n, 2):
    ans[i] = cur
    if cur in a:
        cur = a[cur]
    
print(*ans)


import bisect

n = int(input())
pairs = [list(map(int, input().split())) for _ in range(n)]

front_set = set()
rear_set = set()

for pair in pairs:
    front_set.add(pair[0])
    rear_set.add(pair[1])

students = [-1] * n

pairs.sort()
fronts = [pair[0] for pair in pairs]
rears = [pair[1] for pair in pairs]

pos = 0
students[pos + 1] = rears[pos]
pos += 2

while pos + 1 < len(pairs):
    i = bisect.bisect_left(fronts, students[pos - 1])
    students[pos + 1] = rears[i]
    pos += 2

students[0] = [i for i in front_set - rear_set][0]
pos = 1
while pos + 1< len(pairs):
    i = bisect.bisect_left(fronts, students[pos - 1])
    students[pos + 1] = rears[i]
    pos += 2

print(' '.join(map(str, students)))


n = int(input())
after = {}
sets = [set(), set()]
for i in range(n):
	a, b = list(map(int, input().split()))
	sets[0].add(a)
	sets[1].add(b)
	after[a] = b
original_list = [0, (sets[0] - sets[1]).pop()]
for i in range(2, n + 2):
	original_list.append(after[original_list[i - 2]])
print(*original_list[1:n + 1])


n = int(input())
d1 = dict()
d2 = dict()
ar = [0] * (30000000)
for i in range(n):
    a, b = map(int, input().split())
    ar[a] += 1
    ar[b] += 1    
    d1[a] = b
    d2[b] = a
if n % 2 == 0:
    x = 0
    ar1 = []
    ar2 = []
    while x in d1:
        ar1.append(d1[x])
        x = d1[x]
    x = 0
    while x in d2:
        ar2.append(d2[x])
        x = d2[x]
    ar2 = list(reversed(ar2))
    for i in range(len(ar2)):
        print(ar2[i], end = ' ')
        print(ar1[i], end = ' ')
else:
    arx = []
    for i in range(1000000):
        if ar[i] == 1:
            arx.append(i)
    a = arx[0]
    b = arx[1]
    if b in d2:# a in d2
        a, b = b, a
    x = 0
    ar1 = []
    ar2 = []
    while x in d1:
        if d1[x] != 0:
            ar1.append(d1[x])
        x = d1[x]
        if x == 0:
            break
    
    x = a
    while x in d2:
        ar2.append(d2[x])
        x = d2[x]    
    ar2 = list(reversed(ar2))
    for i in range(len(ar2)):
        print(ar2[i], end = ' ')
        print(ar1[i], end = ' ')    
    print(a)

n=int(input())
d,d1={},{}
for i in range(n):
  a,b=map(int,input().split())
  d[a],d1[b]=b,a
r = [list(filter(lambda f:d1.get(f)==None, d.keys()))[0], d.get(0)]
for i in range(2,n):
  r.append(d.get(r[i-2]))
print(*r)

import sys

inp = sys.stdin

n = int(inp.readline())
after = [-1] * (10 ** 6 + 1)
before = [-1] * (10 ** 6 + 1)
used = [0] * (10 ** 6 + 1)

for i in range(n):
	x, y = map(int, inp.readline().split())
	after[x] = y
	before[y] = x
	used[x] = used[y] = 1

ans = [0] * n
i = 1
x = 0
while after[x] > 0:
	x = after[x]
	ans[i] = x
	i += 2

for i in range(10 ** 6 + 1):
	if used[i] == 1 and before[i] == -1:
		x = i
		break

i = 0
while (i < n) and (x > 0):
	ans[i] = x
	i += 2
	x = after[x]

for i in ans:
	print(i, end=' ')

next={}
appear={}
front={}
n=int(input())
for i in range(1,n+1):
	[a,b]=[int(x) for x in input().split()]
	next[a]=b
	front[a]=True
	front[b]=False
	if a not in appear:
		appear[a]=1
	else:
		appear[a]=2
	if b not in appear:
		appear[b]=1
	else:
		appear[b]=2
a=0
b=0
for x in appear.keys():
	if appear[x]==1 and front[x]:
		print(x,next[0],end=" ")
		a=x
		b=next[0]
cnt=2
for i in range(0,n-1):
	if cnt==n:
		break
	print(next[a],end=" ")
	cnt+=1
	if cnt==n:
		break
	print(next[b],end=" ")
	cnt+=1
	if cnt==n:
		break
	a=next[a]
	b=next[b]
print()


def solve(xs):
    starts = list({x[0] for x in xs}.difference({x[1] for x in xs}))
    map_next = {x[0]:x[1] for x in xs}
    ret = starts[:]
    starts = [0]+starts
    for _ in range(len(xs) - 1):
        starts[0] = map_next[starts[0]]
        ret.append(starts[0])
        starts.reverse()
    return ret

xs = [list(map(int, input().split())) for i in range(int(input()))]
print(" ".join(map(str, solve(xs))))


n = int(input())

s = dict()
for i in range(n):
    (a,b) = [int(element) for element in input().split(' ')]
    s[a] = b

v = []
v.append(0)
t = set(s.values())
for key in list(s.keys()):
    if(key not in t):
        v.append(key)
        break
for i in range(n-1):
    v.append(s[v[-2]])
v = [str(elem) for elem in v]
print(' '.join(v[1:]))


n = int(input())
queue = []
dist = [0]*1000001
dist2= [0]*1000001
dictx = {}
dictx2 = {}
for i in range(n):
    a, b = map(int, input().split(' '))
    dictx[a] = b
    dictx2[b] = a
    dist[a]+=1
    dist[b]+=1
    dist2.append(a)
    dist2.append(b)
if n % 2 == 0:
    indx = 0
    final = [0] * n
    curr = 0
    while curr in dictx:
        curr = dictx[curr]
        final[2*indx+1] = str(curr)
        indx += 1

    indx = -1
    curr = 0
    while curr in dictx2:
        curr = dictx2[curr]
        final[2*indx] = str(curr)
        indx -= 1
        
    print(' '.join(final))

if n % 2 == 1:
    yes = False
    maf = []
    for i in dist2:
        if dist[i] == 1:
            maf.append(i)    
    
    if maf[0] in dictx.keys():
        frontmaf = maf[0]
    else:
        frontmaf = maf[1]
    
    ################## 
    indx = 0
    final = [0] * n
    curr = frontmaf
    while curr in dictx:
        final[2*indx] = str(curr)
        curr = dictx[curr]
        indx += 1
    final[2*indx] = str(curr)

    indx = 0
    curr = 0
    while curr in dictx and dictx[curr] != 0:
        curr = dictx[curr]
        final[2*indx + 1] = str(curr)
        indx += 1

    print(' '.join(final))

'''
Created on ٢٧‏/١١‏/٢٠١٤

@author: mohamed265
'''
 
first = set()
second = set()
map = {}
n = int(input())
for i in range(n):
    temp = input().split()
    first.add(int(temp[0]))
    second.add(int(temp[1]))
    map[int(temp[0])] = int(temp[1])
    

# print(first)
# print(second)
# print(map)

temp = 0
res = [temp for x in range(n + 10)]
j = 2 
temp = 0

while temp in map.keys() :
    if  j > n + 1:
        break
    temp = map[temp]
    res[j] = temp
    j += 2
    
# print(res)
x = first - second
temp = int(x.pop())
j = 1

while temp != 0:
    res[j] = temp
    j += 2
    try:
        temp = map[temp]
    except:
        break
    
 
for i,  x  in  enumerate(res):
    if  i < n + 1:
        if  i != 0:   
            print(x , end=' ')
    
    
    
    


def last(i, find, finish):
    while dict_a[find] != finish:
        keys_a[i] = dict_a[find]  # �������� ����� ������������� ������
        find = keys_a[i]
        i += 2

n = int(input())  # ���������� ��������� � �������
dict_a = {}
dict_b = {}
for i in range(n):
    c = input().split()
    dict_a[int(c[0])] = int(c[1])  # � dict_a ���������� ���� ��������� � ������ �������
    dict_b[int(c[1])] = None  # � dict_b - �� �� ���� � �������� �������
    
keys_a = sorted(dict_a.keys())
dict_b = sorted(dict_b.keys())

pos = 0  # ���� ������� � ���������� �������� � �������
while keys_a[pos] == dict_b[pos]:
    pos += 1
start = keys_a[pos]
end = dict_b[pos]

while (pos+1 < n) and (dict_b[pos+1] == start):
    start = keys_a[pos+1]
    pos += 1
while (pos+1 < n) and (keys_a[pos+1] == end):
    end = dict_b[pos+1]
    pos += 1

keys_a = [start]+[0]*(n-2)+[end]  # ����� ����� ����� (����� �� ��������� ����� ����������)
dict_b = None

if n % 2 == 0:
    last(2, start, 0) 
    last(1, 0, end)
else:
    last(2, start, end) 
    last(1, 0, 0)    

for i in range(n-1):
    print(keys_a[i], end = ' ')
print(keys_a[n-1])

def last(i, find, finish):
    while dict_a[find] != finish:
        keys_a[i] = dict_a[find]  # �������� ����� ������������� ������
        find = keys_a[i]
        i += 2

n = int(input())  # ���������� ��������� � �������
dict_a = {}
dict_b = {}
for i in range(n):
    c = input().split()
    dict_a[int(c[0])] = int(c[1])  # � dict_a ���������� ���� ��������� � ������ �������
    dict_b[int(c[1])] = None  # � dict_b - �� �� ���� � �������� �������
    
keys_a = sorted(dict_a.keys())
dict_b = sorted(dict_b.keys())

pos = 0  # ���� ������� � ���������� �������� � �������
while keys_a[pos] == dict_b[pos]:
    pos += 1
start = keys_a[pos]
end = dict_b[pos]

while (pos+1 < n) and (dict_b[pos+1] == start):
    start = keys_a[pos+1]
    pos += 1
while (pos+1 < n) and (keys_a[pos+1] == end):
    end = dict_b[pos+1]
    pos += 1

keys_a = [start]+[0]*(n-2)+[end]  # ����� ����� ����� (����� �� ��������� ����� ����������)
dict_b = None

if n % 2 == 0:
    last(2, start, 0) 
    last(1, 0, end)
else:
    last(2, start, end) 
    last(1, 0, 0)    

for i in range(n-1):
    print(keys_a[i], end = ' ')
print(keys_a[n-1])

n = int(input())
maxn = 10 ** 6 + 1
nxt = [0] * maxn
nxta = [0] * maxn
ans = [0] * n
used = [0] * maxn
for i in range(n):
    x, y = map(int, input().split())
    if x == 0:
        s = i
    nxta[x] = i
    nxt[i] = y
    used[x] += 1
    used[y] += 2
for i in range(maxn):
    if used[i] == 1:
        z = i
for i in range(n):
    #print(z, s)
    ans[i] = z
    z, s = nxt[s], nxta[z]
for i in ans:
    print(i, end = ' ')
    


n=int(input())
twoback={}
twofront={}
for i in range(n):
    front,back=[int(x) for x in input().split(' ')]
    twoback[front]=back
    if not back in twoback:
        twoback[back]=-1
    twofront[back]=front

if n%2==0:
    outevens=[]
    outodds=[]
    fromleft=0
    fromright=0
    for i in range(int(n/2)):
        outevens.append(twoback[fromleft])
        fromleft=outevens[-1]
        outodds.append(twofront[fromright])
        fromright=outodds[-1]
    tosendout=""
    for c, d in zip(outevens,reversed(outodds)):
        tosendout+=str(d)+" "+str(c)+" "
    print(tosendout.rstrip(" "))
else:
    fromleft=0
    for key,value in twoback.items():
        if value==-1:
            fromright=key
            break
    outevens=[]
    outodds=[fromright]
    for i in range(int(n/2)):
        outevens.append(twoback[fromleft])
        fromleft=outevens[-1]
        outodds.append(twofront[fromright])
        fromright=outodds[-1]
    for c,d in zip(outevens,reversed(outodds)):
        print(d,c,end=" ")
    print(outodds[0])



#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-12-03 16:10:54
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import os
# print('now')
maxn = 1000010
pre, next, ans, vis = [], [], [], []
for i in range(maxn):
	pre.append(-1)
	next.append(-1)
	ans.append(-1)
	vis.append(0)
n = int(input())
for i in range(n):
	a, b = 	input().split()
	# print('in', a, b)
	x, y = int(a), int(b)
	vis[x] = vis[y] = 1
	pre[y] = x
	next[x] = y
# print('next[2]')
ans[0] = 0
vis[0] = 2
i = 0
j = 2
while next[i] != -1:
	i = next[i]
	if vis[i] == 2:
		break
	ans[j] = i
	j += 2
	vis[i] = 2
j = -1
for i in range(maxn):
	if vis[i] == 1:
		j = i
		break
while pre[j] != -1:
	j = pre[j]
i = j
j = 3
vis[i] = 2
ans[1] = i 
while next[i] != -1:
	i = next[i]
	ans[j] = i
	j += 2
	vis[i] = 2
# print("here")
for i in range(n+2):
	if ans[i] > 0:
		print(ans[i], end = ' ')


n = int(input())
before, after = {}, {}
for i in range(n):
  a, b = list(map(int, input().split()))
  after[a] = b
  before[b] = a
begin = None
for key in list(after.keys()):
  if key not in before:
    begin = key
    break
sequence = n * [None]
for pos, x in [(0, begin), (1, after[0])]:
  while True:
    sequence[pos] = x
    if x not in after or after[x] == 0:
      break
    x = after[x]
    pos += 2
print(' '.join(map(str, sequence)))


n=int(input())
List=[0]*(n+1)
L={}
R={}
l,r=0,0
front=0
for i in range(n):
    l,r=list(map(int,input().split()))
    L.update({l:r})
    R.update({r:l})
List[2]=L[0]
i=2
while i+2<=n:
    List[i+2]=L[List[i]]
    i+=2

for k in L:
    if R.get(k)==None:
        front=k
        break
i,List[1]=1,front
while i+2<=n:
    List[i+2]=L[List[i]]
    i+=2
print(*List[1:])


__author__ = 'Rakshak.R.Hegde'
n = int(input())
m = dict()
s = [0] * (2 * n)
xor2 = 0
ind = 0
for i in range(n):
    a, b = map(int, input().split())
    xor2 ^= a ^ b
    s[ind] = a
    ind += 1
    s[ind] = b
    ind += 1
    m[a] = b
l = r = 0
setBit = xor2 & ~(xor2 - 1)
for i in s:
    if i & setBit:
        l ^= i
    else:
        r ^= i
if l not in m: l = r
a, b = 0, l
ans = str(b)
for i in range(1, n, 2):
    a = m[a]
    ans += ' ' + str(a)
    b = m[b]
    if b != 0:
        ans += ' ' + str(b)
print(ans)

x=int(input())
left=dict()
lset=set()
rset=set()
q=[0]*x
i=0
while i<x:
    l=[int(j) for j in input().split()]
    left[l[0]]=l[1]
    lset.add(l[0])
    rset.add(l[1])
    i+=1
i=lset.difference(rset).pop()
j=0
q[0]=i
k=1
while k<x:
    q[k]=left[j]
    k+=1
    if k<x:
        q[k]=left[i]
    k+=1
    i=left[i]
    j=left[j]
print(" ".join([str(k) for k in q]))


import bisect

n = int(input())
pairs = [list(map(int, input().split())) for _ in range(n)]

front_set = set()
rear_set = set()

for pair in pairs:
    front_set.add(pair[0])
    rear_set.add(pair[1])

students = [-1] * n

pairs.sort()
fronts = [pair[0] for pair in pairs]
rears = [pair[1] for pair in pairs]

pos = 0
students[pos + 1] = rears[pos]
pos += 2

while pos + 1 < len(pairs):
    i = bisect.bisect_left(fronts, students[pos - 1])
    students[pos + 1] = rears[i]
    pos += 2

students[0] = [i for i in front_set - rear_set][0]
pos = 1
while pos + 1< len(pairs):
    i = bisect.bisect_left(fronts, students[pos - 1])
    students[pos + 1] = rears[i]
    pos += 2

print(' '.join(map(str, students)))

a=int(input())
#a1=a//2+1
d={}
#t=[]
dinv={}
for i in range(a):
	b1=input().split()
	b=list(b1)
	d[int(b[0])]=int(b[1]) #так мы создали словарик со всеми данными
#	t+=[int(b[1])]
	dinv[int(b[1])] = int(b[0])

c1=[]
n=0
#n=d.get(0)
#c1+=[n]
for i in range(a):
	n=d.get(n) # d[n]
	if n in d and n!=0:
		c1 += [n]
	#if n in d.values():
	#	c1+=[n] // создали первый список из всех четных студентов
	else:
		c1+=[n]
		break

#print(c1)
c2=[]
for i in d.keys():
	if i not in dinv:# and i!=0:
		k=i
		c2+=[k] #// нашли начало второго списка

for i in range(a):
	k=d.get(k)
	if k in d and k!=0:
		c2+=[k]  #// создали второй список (нечетные)
	else:
		c2+=[k]
		break
#print(c1)
#print(c2)
c1+=[0]
c2+=[0]
for i in range(a):
	if c2[i]!=0:
		print(c2[i], end=' ')
	if c1[i]!=0:
		print(c1[i], end=' ')
	else:
		break