edgeList = []
n, m, v = map(int, input().split())
for _ in range(m):
a, b = map(int, input().split())
edgeList.append((a, b))
edgeList.append((b, a))
def dfs(adjacencyList, root):
stack = [root]
visitedList = []
while stack:
current = stack.pop()
if current not in visitedList:
visitedList.append(current)
temp = adjacencyList[current - 1]
temp.sort(reverse=True)
stack += temp
return visitedList
def bfs(adjacencyList, root):
from collections import deque
queue = deque([root])
visitedList = []
while queue:
current = queue.popleft()
if current not in visitedList:
visitedList.append(current)
temp = adjacencyList[current - 1]
temp.reverse()
queue += temp
return visitedList
adjacencyList = [[] for _ in range(n)]
for edge in edgeList:
adjacencyList[edge[0] - 1].append(edge[1])
print(*dfs(adjacencyList, v))
print(*bfs(adjacencyList, v))