import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		
		
		if (!sc.hasNextInt()) return;
		
		int vertices = sc.nextInt();
		int edges = sc.nextInt();
		
		// Initialize Adjacency List 
		List<List<Integer>> adj = new ArrayList<>();
		for (int i = 0; i < vertices; i++) {
			adj.add(new ArrayList<>());
		}
		
		// Read all graph edges from standard input
		for (int i = 0; i < edges; i++) {
			int u = sc.nextInt();
			int v = sc.nextInt();
			
			// Build an undirected graph
			adj.get(u).add(v);
			adj.get(v).add(u);
		}
		
	
		int startNode = sc.nextInt();
		
		System.out.print("BFS Traversal starting from node " + startNode + ": ");
		bfs(startNode, vertices, adj);
	}

	public static void bfs(int startNode, int vertices, List<List<Integer>> adj) {
		Queue<Integer> queue = new LinkedList<>();
		boolean[] visited = new boolean[vertices];

		
		visited[startNode] = true;
		queue.add(startNode);

		while (!queue.isEmpty()) {
			int curr = queue.poll();
			System.out.print(curr + " ");

			
			for (int neighbor : adj.get(curr)) {
				if (!visited[neighbor]) {
					visited[neighbor] = true;
					queue.add(neighbor);
				}
			}
		}
		System.out.println();
	}
}
