


import java.io.*;
import java.util.*;
public class Main{
static int N;
static int M;
static StringBuilder sb;
public static void main(String[] args) throws IOException {
sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
HashMap<Integer,List> graph = new HashMap<Integer,List>();
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int key = Integer.parseInt(st.nextToken());
int val = Integer.parseInt(st.nextToken());
if(graph.keySet().contains(key)) {
graph.get(key).add(val);
}else {
List<Integer>temp = new LinkedList<Integer>();
temp.add(val);
graph.put(key, temp);
}
//양방향이라서
if(graph.keySet().contains(val)) {
graph.get(val).add(key);
}else {
List<Integer>temp = new LinkedList<Integer>();
temp.add(key);
graph.put(val, temp);
}
}
//숫자가 작은 정점부터 방문하기 위해 오름차순 정렬했습니다.
for (List<Integer> g: graph.values()) {
Collections.sort(g);
}
//System.out.println(graph.toString());
if(M==0) {//예외1. 아무런 연결이 없을 때
return;
}else if(graph.get(V)==null) {//예외2. 시작점에서 연결된 노드가 없을 때
System.out.println(V);
System.out.println(V);
}else{
DFS(V,new boolean[N+1],graph);
sb.append("\n");
BFS(V,graph);
System.out.println(sb.toString());
}
}
public static void DFS(int V,boolean[] v,HashMap<Integer,List>graph) {
v[V] = true;
sb.append(V+" ");
List<Integer> lst = graph.get(V);
for (int i = 0; i < lst.size(); i++) {
if(!v[lst.get(i)]) {
DFS(lst.get(i),v,graph);
}
}
}
public static void BFS(int V,HashMap<Integer,List>graph) {
boolean[] v = new boolean[N+1];
Queue<Integer> q = new LinkedList<Integer>();
v[V] = true;
q.add(V);
while(!q.isEmpty()) {
int current = q.poll();
sb.append(current+" ");
List<Integer> lst = graph.get(current);
for (int i = 0; i < lst.size(); i++) {
if(!v[lst.get(i)]) {
q.add(lst.get(i));
v[lst.get(i)] = true;
}
}
}
}
}