Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.

In this problem you are given n memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to n.

For each cell i you are given two values:   l_{i} — cell containing previous element for the element in the cell i;  r_{i} — cell containing next element for the element in the cell i. 

If cell i contains information about the element which has no previous element then l_{i} = 0. Similarly, if cell i contains information about the element which has no next element then r_{i} = 0. [Image] Three lists are shown on the picture. 

For example, for the picture above the values of l and r are the following: l_1 = 4, r_1 = 7; l_2 = 5, r_2 = 0; l_3 = 0, r_3 = 0; l_4 = 6, r_4 = 1; l_5 = 0, r_5 = 2; l_6 = 0, r_6 = 4; l_7 = 1, r_7 = 0.

Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values l_{i}, r_{i}.

Any other action, other than joining the beginning of one list to the end of another, can not be performed.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of memory cells where the doubly linked lists are located.

Each of the following n lines contains two integers l_{i}, r_{i} (0 ≤ l_{i}, r_{i} ≤ n) — the cells of the previous and the next element of list for cell i. Value l_{i} = 0 if element in cell i has no previous element in its list. Value r_{i} = 0 if element in cell i has no next element in its list.

It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of n given cells.


-----Output-----

Print n lines, the i-th line must contain two integers l_{i} and r_{i} — the cells of the previous and the next element of list for cell i after all lists from the input are united in a single list. If there are many solutions print any of them.


-----Example-----
Input
7
4 7
5 0
0 0
6 1
0 2
0 4
1 0

Output
4 7
5 6
0 5
6 1
3 2
2 4
1 0

Difficulty: competition

Starter code:

Solutions:
n = int(input())
arr = []
for i in range(n):
    l,r = map(int, input().split())
    arr.append([l,r])
    
lts = []
for i in range(n):
    if arr[i][0] == 0:
        l = i
        j = i
        while arr[j][1] != 0:
            j = arr[j][1] - 1
        r = j
        lts.append([l,r])

for i in range(1, len(lts)):
    arr[lts[i-1][1]][1] = lts[i][0] + 1
    arr[lts[i][0]][0] = lts[i-1][1] + 1
    
for i in range(n):
    print(arr[i][0], arr[i][1])

def run(i):
	nonlocal l
	d[i]=1
	if(l[i][1]==0):
		return i 
	return run(l[i][1])



n=int(input())
l=[]
for u in range(n):	
	l.append(list(map(int,input().split())))
l.insert(0,0)
d={}
ans=0
start=0
for i in range(1,len(l)):
	if(l[i][0] == 0):
		ans = run(i)
		start = i
		break
for i in range(start+1,len(l)):
	if(l[i][0] == 0):
		l[i][0] = ans
		l[ans][1] = i
		ans = run(i)
for val in range(1,len(l)):
	print(l[val][0],l[val][1])






n = int(input())
ll = [list(map(int, input().split())) for _ in range(n)]
ll = [[0,0]] + ll
tail = 0
visited = [False]*(n+1)
cnt = 0
while cnt < n:
    for i in range(1, n+1):
        if ll[i][0] == 0 and not visited[i]:
            head = i
            visited[head] = True
            cnt += 1
            break
    ll[tail][1] = head
    ll[head][0] = tail
    while ll[head][1] != 0:
        head = ll[head][1]
        visited[head] = True
        cnt += 1
    tail = head
for i in range(1, n+1):
    print(ll[i][0], ll[i][1])
            


nodes = []
start_nodes = []

for i in range(int(input().strip())):
    left, right = input().strip().split()
    left = int(left)
    right = int(right)
    current = i + 1
    if left == 0:
        start_nodes.append([left, current, right])
    else:
        nodes.append([left, current, right])
        
lists = []

for node in start_nodes:
    links = [node]
    while True:
        prevlen = len(links)
        for i in range(len(nodes)):
            if links[-1][-1] == nodes[i][1]:
                links.append(nodes[i])
        nextlen = len(links)
        
        if prevlen == nextlen:
            break
    lists.append(links)

flattened = [node for ll in lists for node in ll]

for i in range(1, len(flattened)):
    if flattened[i][0] == 0:
        flattened[i][0] = flattened[i - 1][1]
        flattened[i - 1][2] = flattened[i][1]

sorted_union = sorted(flattened, key = lambda x: x[1])

for node in sorted_union:
    print(node[0], node[2])




l=[]
l2=[]
r2=[]
r=[]
a=[]
u=0
q=0
p=0
n = int(input())
for i in range(n):
 x1,x2 = list(map(int, input().split()))
 l.append(x1)
 r.append(x2)
 if x1==0:
     u=u+1   
for i2 in range(u):
 y=0
 for i in range(p,n):
  if l[i]==0 and y==0:
     a.append(i+1)
     y=1
     q=i
     p=i+1
 while r[q]!=0:
    a.append(r[q])
    q=r[q]-1
for i3 in range(n+1):
 l2.append(0)
for i3 in range(1,n):
 l2[a[i3]]=a[i3-1]
del l2[0]


for i3 in range(n+1):
 r2.append(0)
for i3 in range(0,n-1):
 r2[a[i3]]=a[i3+1]
del r2[0]
for i3 in range(n):
    print(l2[i3],r2[i3])


 






n = int(input())

if n == 1:
    print(0, 0)
    return

a = []
for i in range(n):
    a.append(list(map(int, input().split())))

s = []
i = 0
while i < n:
    if a[i][0] == 0:
        k = i
        s.append([i + 1])
        while a[k][1] != 0:
            k = a[k][1] - 1
            s[-1].append(k + 1)
    i += 1

p = []
for i in s:
    p.extend(i)

for i in range(n):
    if i == 0:
        a[p[i] - 1] = [0, p[1]]
    elif i == n - 1:
        a[p[i] - 1] = [p[i - 1], 0]
    else:
        a[p[i] - 1] = [p[i - 1], p[i + 1]]

for i in a:
    print(i[0], i[1])

k, n = 0, int(input())
t = [list(map(int, input().split())) for j in range(n)]
for m, (l, r) in enumerate(t, 1):
    if not l:
        if k: t[k - 1][1], t[m - 1][0] = m, k
        k = m
        while r: k, r = r, t[r - 1][1]
for l, r in t: print(l, r)

k, n = 0, int(input())
t = [list(map(int, input().split())) for j in range(n)]
for m, (l, r) in enumerate(t, 1):
    if not l:
        if k: t[k - 1][1], t[m - 1][0] = m, k
        k = m
        while r: k, r = r, t[r - 1][1]
for l, r in t: print(l, r)

k, n = 0, int(input())

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

for m, (l, r) in enumerate(t, 1):

    if not l:

        if k: t[k - 1][1], t[m - 1][0] = m, k

        k = m

        while r: k, r = r, t[r - 1][1]

for l, r in t: print(l, r)



# Made By Mostafa_Khaled


n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
start = []
end = []

for i in range(n):
    if A[i][0] == 0:
        start.append(i)
    elif A[i][1] == 0:
        end.append(i)


for curr in range(len(start)):
    x = start[curr]
    #print(curr)
    while A[x][1] != 0:
        #print(x)
        x = A[x][1] - 1
    #print(x)
    if curr != len(start) - 1:
        A[x][1] = start[curr + 1] + 1
        A[A[x][1] - 1][0] = x + 1

for i in range(n):
    print(A[i][0], A[i][1])






class CodeforcesTask847ASolution:
    def __init__(self):
        self.result = ''
        self.n = 0
        self.cells = []

    def read_input(self):
        self.n = int(input())
        for x in range(self.n):
            self.cells.append([int(y) for y in input().split(" ")])

    def process_task(self):
        lists = []
        for x in range(self.n):
            if not self.cells[x][0]:
                nc = [x + 1]
                curr = x
                while self.cells[curr][1]:
                    curr = self.cells[curr][1] - 1
                nc.append(curr + 1)
                lists.append(nc)
        for x in range(len(lists) - 1):
            self.cells[lists[x][1] - 1][1] = lists[x + 1][0]
            self.cells[lists[x + 1][0] - 1][0] = lists[x][1]
        self.result = "\n".join(["{0} {1}".format(*cell) for cell in self.cells])


    def get_result(self):
        return self.result


def __starting_point():
    Solution = CodeforcesTask847ASolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())

__starting_point()