// Arup Guha
// Started on 10/23/2014
// Was a non-working solution to 1994 UCF HS Contest Problem: Nothin' But Net

/*** Rediscovered on 7/15/2026 and fixed to work. I had written and fixed a different
     version on 7/14/2026 and 7/15/2026, once I verified that one worked, I decided to
	 fix this one. I had a few errors that I would have found back then, had I spent 
	 time on it. Today I finally had that time to put in (and also watch Argentina
	 get back to the World Cup final =)) I had an off by one error reading in queries
	 and was accessing my path array for reconstruction incorrectly.
	 But it works now! This one uses Floyd's and my new one uses BFS...
***/

import java.util.*;

public class nothin {

	final public static int NOEDGE = 1000000;
	final public static String[] PATH_LOWER = {"","off the","through the","around"};
	final public static String[] PATH_UPPER = {"","Off the","Through the","Around"};

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		while (stdin.hasNext()) {

			// Read in objects.
			int n = Integer.parseInt(stdin.nextLine().trim());
			String[] objects = new String[n-1];
			for (int i=0; i<n-1; i++)
				objects[i] = stdin.nextLine();

			// And the adjacency matrix here...
			int[][] inputM = new int[n-1][n];
			int[][] adj = new int[n][n];
			for (int i=0; i<n-1; i++) {
				StringTokenizer tok = new StringTokenizer(stdin.nextLine());
				for (int j=0; j<n; j++) {
					inputM[i][j] = Integer.parseInt(tok.nextToken());
					adj[i][j] = inputM[i][j] != 0 ? 1 : NOEDGE;
				}
				adj[i][i] = 0;
			}

			// Annoying last row.
			for (int j=0; j<n-1; j++)
				adj[n-1][j] = NOEDGE;
			adj[n-1][n-1] = 0;

			// Set up path array for path reconstrucution.
			int[][] path = new int[n][n];
			for (int i=0; i<n; i++)
				for (int j=0; j<n; j++)
					path[i][j] = adj[i][j] != 0 ? i : -1;
			for (int i=0; i<n; i++)
				path[i][i] = i;

			// Run Floyd's with path reconstruction.
			for (int k=0; k<n; k++) {
				for (int i=0; i<n; i++) {
					for (int j=0; j<n; j++) {
						if (adj[i][k] + adj[k][j] < adj[i][j]) {
							adj[i][j] = adj[i][k] + adj[k][j];
							path[i][j] = path[k][j];
						}
					}
				}
			}

			// Process each request.
			int source = Integer.parseInt(stdin.nextLine().trim())-1;
			while (source != -1) {
				
				// Special case.
				if (source == n-1) {
					System.out.println("Nothin' but net.");
				}
				
				// Something to build.
				else {

					// Build path backwards.
					ArrayList<Integer> myPath = new ArrayList<Integer>();
					int end = n-1;
					while (end != source) {
						myPath.add(end);
						end = path[source][end];
					}
					myPath.add(end);

					// Fix it.
					Collections.reverse(myPath);

					// First string with special capitalization.
					System.out.print(PATH_UPPER[inputM[myPath.get(0)][myPath.get(1)]]+" "+objects[myPath.get(0)]+", ");

					// Middle portion of the path.
					for (int i=1; i<myPath.size()-1; i++)
						System.out.print(PATH_LOWER[inputM[myPath.get(i)][myPath.get(i+1)]]+" "+objects[myPath.get(i)]+", ");

					// Finally..
					System.out.println("nothin' but net.");
				}
				
				// I am not doing between cases, just after each output line.
				System.out.println();

				// Get next source.
				source = Integer.parseInt(stdin.nextLine().trim())-1;
			}
		}
	}
}
