const input = require('fs').readFileSync('dev/stdin').toString().trim().split('\n');
const [n, m, v] = input.shift().split(' ').map(Number);
let graph = [...Array(n + 1)].map((e) => []);
input.forEach((str) => {
const [from, to] = str.split(' ').map(Number);
graph[from].push(to);
graph[to].push(from);
});
graph.forEach((el) => el.sort((a, b) => a - b));
let visited = new Array(n + 1).fill(0);
let answer_dfs = [];
let answer_bfs = [];
DFS(v);
console.log(answer_dfs.join(' '));
visited.fill(0);
BFS(v);
console.log(answer_bfs.join(' '));
function DFS(v) {
if (visited[v]) return;
visited[v] = 1;
answer_dfs.push(v);
for (let i = 0; i < graph[v].length; i++) {
let next = graph[v][i];
if (!visited[next]) DFS(next);
}
}
function BFS(v) {
let queue = [v];
while (queue.length) {
let x = queue.shift();
if (visited[x]) continue;
visited[x] = 1;
answer_bfs.push(x);
for (let i = 0; i < graph[x].length; i++) {
let next = graph[x][i];
if (!visited[next]) {
queue.push(next);
}
}
}
}