import java.util.*;
import java.io.*;

public class Graph {

	public static final int INFINITY = Integer.MAX_VALUE;
	
	private Map<String, Vertex> vertexMap = new HashMap<String, Vertex>();
	
	public Vertex getVertex(String name) {
		Vertex v = vertexMap.get(name);
		if (v == null) {
			v = new Vertex(name);
			vertexMap.put(name, v);
		}
		return v;
	}
	
	public void addEdge( String source, String dest) {
		Vertex v = getVertex(source);
		Vertex w = getVertex(dest);
		v.adjacentList.add( new Edge(w));
	}
	
	public void shortestPaths(String nodeName) {
		LinkedList<Vertex> queue = new LinkedList<Vertex>();
		Vertex S = getVertex(nodeName);
		S.distance = 0;
		queue.offer(S);
		while (queue.size() > 0	) {
			Vertex X = queue.poll();
			for (Edge e: X.adjacentList) {
				Vertex Y = e.destination;
				if (Y.distance == INFINITY) {
					Y.distance = X.distance + 1;
					Y.previous = X;
					queue.offer(Y);
				}
			}
		}
	}
	
	public void PrintPath(String nodeName) {
		Stack<Vertex>stack = new Stack<Vertex>();
		Vertex v = getVertex(nodeName);
		stack.push(v);
		while (v.previous != null) {
			v = v.previous;
			stack.push(v);
		}
		while (!stack.empty() ) {
			Vertex t = stack.pop();
			System.out.printf( "%s ", t);
			if (!stack.empty())
				System.out.printf( "-> ");
		}
	}
	
	public void PrintAllPaths() {
		Set<String> keys = vertexMap.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String s = it.next();
			Vertex v = vertexMap.get(s);
			System.out.printf( "%s: ", v);
			PrintPath(s);
			System.out.println();
		}
	}
	
	void reset() {
		Set<String> keys = vertexMap.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String s = it.next();
			Vertex v = vertexMap.get(s);
			v.distance = INFINITY;
			v.previous = null;
		}
	}
		
	public static void main(String[] args) {
		String source="";
		Graph G = new Graph();
		try {
			Scanner input = new Scanner( new FileReader("graph.txt"));
			while (input.hasNextLine()) {
				String line = input.nextLine();
				String[] names = line.split( " ");
				if (names.length == 2) {
					G.addEdge(names[0], names[1]);
				}
				else
					 source = names[0];
			}
			G.shortestPaths(source);
			G.PrintAllPaths();
		}
		catch (IOException e) {
			System.out.println(e);
		}
			
	}

}
