import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static ArrayList<Integer>[] adj;
static boolean[] visit;
static StringBuilder sb = new StringBuilder();
static void dfs(int x) {
visit[x] = true;
sb.append(x).append(' ');
for(int y: adj[x]) {
if(visit[y]) continue;
dfs(y);
}
}
static void bfs(int x) {
Queue<Integer> queue = new LinkedList<>();
queue.add(x);
visit[x] = true;
while(!queue.isEmpty()) {
x = queue.poll();
sb.append(x).append(' ');
for(int y:adj[x]) {
if(visit[y]) continue;
queue.add(y);
visit[y] = true;
}
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int N = kb.nextInt();
int M = kb.nextInt();
int V = kb.nextInt();
adj = new ArrayList[N+1];
for(int i=1; i<=N; i++) {
adj[i] = new ArrayList<Integer>();
}
for(int i=1; i<=M; i++) {
int x= kb.nextInt();
int y = kb.nextInt();
adj[x].add(y);
adj[y].add(x);
}
// 번호가 작은 것부터 방문하기 위해 정렬
for(int i=1; i<=N; i++) {
Collections.sort(adj[i]);
}
visit = new boolean[N+1]; // 배열 방문 초기화
dfs(V);
sb.append('\n');
for(int i=1; i<=N; i++) {
visit[i] = false;
}
bfs(V);
System.out.println(sb);
}
}