import sys
N = int(input())
class Node:
def __init__(self, data, L, R):
self.data = data
self.l = L
self.r = R
def preorder(node):
print(node.data, end='')
if node.l != '.':
preorder(tree[node.l])
if node.r != '.':
preorder(tree[node.r])
def inorder(node):
if node.l != '.':
inorder(tree[node.l])
print(node.data, end='')
if node.r != '.':
inorder(tree[node.r])
def postorder(node):
if node.l != '.':
postorder(tree[node.l])
if node.r != '.':
postorder(tree[node.r])
print(node.data, end='')
tree = {}
for i in range(N):
node, L, R = input().split()
tree[node] = Node(node,L,R)
preorder(tree['A'])
print()
inorder(tree['A'])
print()
postorder(tree['A'])
print()
import sys
sys.setrecursionlimit(1000000)
n = int(sys.stdin.readline())
tree = dict()
root = 'A'
class node:
def __init__(self):
self.data = None
self.left = None
self.right = None
def preorder(node):
sys.stdout.write(node.data)
if node.left is not None:
preorder(tree[node.left])
if node.right is not None:
preorder(tree[node.right])
def inorder(node):
if node.left is not None:
inorder(tree[node.left])
sys.stdout.write(node.data)
if node.right is not None:
inorder(tree[node.right])
def postorder(node):
if node.left is not None:
postorder(tree[node.left])
if node.right is not None:
postorder(tree[node.right])
sys.stdout.write(node.data)
for i in range(n):
a, b, c = sys.stdin.readline().split()
if a == root:
tree[root] = node()
tree[root].data = a
if b != '.':
tree[root].left = b
tree[b] = node()
tree[b].data = b
if c!= '.':
tree[root].right = c
tree[c] = node()
tree[c].data = c
else:
if b!= '.':
tree[a].left = b
tree[b] = node()
tree[b].data = b
if c!= '.':
tree[a].right = c
tree[c] = node()
tree[c].data = c
preorder(tree[root])
sys.stdout.write('\n')
inorder(tree[root])
sys.stdout.write('\n')
postorder(tree[root])