while True:
try:
input_list = [int(i) for i in input().split()]
# 链表长度,链表第一个元素,链表需要删除到元素
lenth = input_list[0]
num_list = [input_list[1]]
drop_no = input_list[-1]
# 链表到子、父节点
son_list = input_list[2:-2:2]
father_list = input_list[3:-1:2]
for i in range(len(father_list)):
# 要往列表中插入到索引位置和值
loc = num_list.index(father_list[i]) + 1
v = son_list[i]
num_list.insert(loc, v)
num_list.remove(drop_no)
print(' '.join([str(i) for i in num_list]))
except:
break
try:
input_list = [int(i) for i in input().split()]
# 链表长度,链表第一个元素,链表需要删除到元素
lenth = input_list[0]
num_list = [input_list[1]]
drop_no = input_list[-1]
# 链表到子、父节点
son_list = input_list[2:-2:2]
father_list = input_list[3:-1:2]
for i in range(len(father_list)):
# 要往列表中插入到索引位置和值
loc = num_list.index(father_list[i]) + 1
v = son_list[i]
num_list.insert(loc, v)
num_list.remove(drop_no)
print(' '.join([str(i) for i in num_list]))
except:
break