// Arup Guha
// 12/4/2017
// Solution to 2017 NCPC Problem I: Import Spaghetti

import java.util.*;
import java.io.*;

public class i {

	public static int n;
	public static String[] names;
	public static HashMap<String,Integer> map;
	public static int[][] adj;
	public static int[][] path;

	public static void main(String[] args) throws Exception {

		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(stdin.readLine());

		// Read in all the file names.
		map = new HashMap<String,Integer>();
		names = new String[n];
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		for (int i=0; i<n; i++) {
			names[i] = tok.nextToken();
			map.put(names[i], i);
		}

		// Set up initially.
		adj = new int[n][n];
		for (int i=0; i<n; i++) Arrays.fill(adj[i], n+1);
		path = new int[n][n];
		for (int i=0; i<n; i++) Arrays.fill(path[i], n+1);

		// Add edges
		for (int i=0; i<n; i++) {

			// Get which var.
			tok = new StringTokenizer(stdin.readLine());
			int v = map.get(tok.nextToken());
			int items = Integer.parseInt(tok.nextToken());

			// Run through import statements - set dependency edges to 1.
			for (int j=0; j<items; j++) {
				tok = new StringTokenizer(stdin.readLine().substring(7),", ");

				// Get each item in this import statement.
				while (tok.hasMoreTokens()) {
					int w = map.get(tok.nextToken());
					adj[v][w] = 1;
					path[v][w] = v;
				}
			}
		}

		// Run Floyd's.
		for (int k=0; k<n; k++) {
			for (int i=0; i<n; i++) {
				for (int j=0; j<n; j++) {

					// Got a better path.
					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];
					}
				}
			}
		}

		// Find shortest cycle.
		int best = adj[0][0], besti = 0;
		for (int i=1; i<n; i++) {
			if (adj[i][i] < best) {
				best = adj[i][i];
				besti = i;
			}
		}

		// No cycles!
		if (best == n+1)
			System.out.println("SHIP IT");

		// Build cycle.
		else {

			int cur = path[besti][besti];
			String result = names[cur];

			// Build backwards.
			while (cur != besti) {

				// Get next previous item.
				int next = path[besti][cur];
				result = names[next] + " " + result;

				cur = next;
			}

			// Here is our cycle.
			System.out.println(result);
		}
	}
}