import sys
N, M, V = map(int,sys.stdin.readline().split())
nodes = []
for node in range(N+1):
nodes.append([])
for _ in range(M):
[v1, v2] = map(int,sys.stdin.readline().split())
nodes[v1].append(v2)
nodes[v2].append(v1)
for subList in nodes:
subList.sort()
dfsCheck = [0]*(N+1)
def dfs(ans, start):
if dfsCheck[start] == 1:
return
dfsCheck[start] = 1
ans.append(start)
for i in nodes[start]:
if dfsCheck[i] == 0:
dfs(ans, i)
dfsAnswer = []
dfs(dfsAnswer, V)
print(*dfsAnswer)
bfsCheck = [0]*(N+1)
def bfs(ans, start):
que = []
que.append(start)
while len(que) > 0:
index = que.pop(0)
bfsCheck[index] = 1
ans.append(index)
for node in nodes[index]:
if bfsCheck[node] == 1:
continue
else:
bfsCheck[node] = 1
que.append(node)
bfsAnswer = []
bfs(bfsAnswer, V)
print(*bfsAnswer)