from collections import deque, defaultdict
import sys
read = sys.stdin.readline
def dfs(info, v, visited):
if visited[v] == True:
return
visited[v] = True
print(v, end=' ')
for next_ in info[v]:
if not visited[next_]:
dfs(info, next_, visited)
def bfs(info, start, visited):
dq = deque([start])
visited[start] = True
while dq:
v = dq.popleft()
print(v, end=' ')
for next_ in info[v]:
if not visited[next_]:
dq.append(next_)
visited[next_] = True
N, M, V = list(map(int, read().strip().split()))
info = defaultdict(list)
for _ in range(M):
t1, t2 = list(map(int, read().strip().split()))
info[t1].append(t2)
info[t2].append(t1)
for x in info.keys():
info[x] = sorted(info[x])
visited = [False for _ in range(N+1)]
dfs(info, V, visited)
print()
visited = [False for _ in range(N+1)]
bfs(info, V, visited)