// Arup Guha
// 11/20/2013
// Solution to 2013 Pacific NorthWest Problem L: Languages

import java.util.*;

public class l {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = Integer.parseInt(stdin.nextLine());

		String[] langs = new String[n];
		HashSet<String>[] words = new HashSet[n];

		// Go through each language.
		for (int i=0; i<n; i++) {

			// Store language name.
			StringTokenizer tok = new StringTokenizer(stdin.nextLine());
			String lang = tok.nextToken();
			langs[i] = lang;

			// Store its words.
			words[i] = new HashSet<String>();
			while (tok.hasMoreTokens())
				words[i].add(tok.nextToken().toLowerCase());
		}

		// Stupid blank line...
		stdin.nextLine();

		// Go through each sample text.
		while (stdin.hasNext()) {

			StringTokenizer tok = new StringTokenizer(stdin.nextLine(), " ,.!;?()");
			String lang = null;

			// Go through each word.
			while (tok.hasMoreTokens()) {
				String s = tok.nextToken().toLowerCase();

				// If we find a match in here, get out!
				for (int i=0; i<n; i++) {
					if (words[i].contains(s)) {
						lang = langs[i];
						break;
					}
				}
				if (lang != null) break;
			}

			// This is our language.
			System.out.println(lang);
		}
	}
}