import sys
from collections import deque
# dfs method
def dfs(n):
print(n, end=' ')
# mark visited
visited[n] = True
for i in graph[n]:
if not visited[i]:
dfs(i)
# bfs method
def bfs(n):
dq = deque([n])
visited[n] = True
while dq:
val = dq.popleft()
print(val, end=" ")
for i in graph[val]:
if not visited[i]:
dq.append(i)
visited[i] = True
if __name__ == "__main__":
n,m,v = map(int,sys.stdin.readline().split())
# make graph by adjacency list
graph = [[] for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
# to visit the number with the lowest number first
for i in range(len(graph)):
graph[i].sort()
# make list to check whether visited
visited = [False for _ in range(n + 1)]
dfs(v)
# reset visited for bfs
visited = [False for _ in range(n + 1)]
print()
bfs(v)