n,m,v = map(int,input().split())
def dfs(graph,start,visited):
visited[start] =True
print(start, end = ' ')
for n in sorted(graph[start]):
if not visited[n]:
dfs(graph,n,visited)
from collections import deque
def bfs(graph,start,visited):
visited[start] = True
dd = deque([start])
while dd:
node = dd.popleft()
print(node, end = ' ')
for n in sorted(graph[node]):
if not visited[n]:
dd.append(n)
visited[n]=True
visited_dfs = [False]*(n+1)
visited_bfs = [False]*(n+1)
graph = {i:[] for i in range(1,n+1)}
for _ in range(m):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)
dfs(graph,v,visited_dfs)
print()
bfs(graph,v,visited_bfs)