import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb = new StringBuilder();
static List<Integer>[] adjList;
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
adjList = new ArrayList[N + 1];
for (int i = 0; i < N + 1; i++) {
adjList[i] = new ArrayList<Integer>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
adjList[from].add(to);
adjList[to].add(from);
}
boolean[] visited = new boolean[N+1];
dfs(V, visited);
sb.append("\n");
bfs(V);
System.out.println(sb);
br.close();
}
private static void dfs(int curr, boolean[] visited) {
visited[curr] = true;
sb.append(curr).append(" ");
Collections.sort(adjList[curr]);
for (int next : adjList[curr]) {
if(!visited[next]) {
dfs(next, visited);
}
}
}
private static void bfs(int n) {
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[N+1];
queue.offer(n);
visited[n] = true;
while (!queue.isEmpty()) {
int curr = queue.poll();
sb.append(curr).append(" ");
Collections.sort(adjList[curr]);
for (int next : adjList[curr]) {
if (!visited[next]) {
visited[next] = true;
queue.offer(next);
}
}
}
}
}