import java.io.*;
import java.util.HashMap;
import java.util.HashSet;

public class Validate {

	public void run() throws FileNotFoundException {
		StrictScanner ss = new StrictScanner(new FileInputStream("numbers.in"));
		int n = ss.nextInt();
		ss.nextLine();
		ensureLimits(n, 1, 100, "count of countries");
		HashMap<String, HashSet<String>> hm = new HashMap<String, HashSet<String>>();
		for (int i = 0; i < n; i++) {
			String code = ss.next();
			int k = ss.nextInt();
			ss.nextLine();
			ensureValid(code, 1, 3, "code of country #" + (i + 1));
			ensureLimits(k, 1, 100, "count of cities in country #" + (i + 1));
			ensure(!hm.containsKey(code), "duplicate country code: " + code);
			hm.put(code, new HashSet<String>());
			for (int j = 0; j < k; j++) {
				String t = ss.next();
				ss.nextLine();
				ensureValid(t, 3, 5, "code of city #" + (j + 1)
						+ " in country # " + (i + 1));
				hm.get(code).add(t);
			}
		}
		int c = ss.nextInt();
		ss.nextLine();
		ensureLimits(c, 1, 10000, "count of numbers");
		for (int i = 0; i < c; i++) {
			String num = ss.next();
			ss.nextLine();
			ensure(num.matches("[0-9]{11}"), "number #" + (i + 1));
			int cnt = 0;
			for (int l1 = 1; l1 < 4; l1++) {
				for (int l2 = 3; l2 < 6; l2++) {
					String country = num.substring(0, l1);
					String city = num.substring(l1, l1 + l2);
					if (hm.containsKey(country)
							&& hm.get(country).contains(city) && num.charAt(l1 + l2) != '0') {
						cnt++;
					}
				}
			}
			ensure(cnt < 2, "number #" + (i + 1) + " is ambiguous");
		}

	}

	private void ensureValid(String s, int l, int r, String name) {
		ensure(s.length() >= l, name + " is too short");
		ensure(s.length() <= r, name + " is too long");
		for (int i = 0; i < s.length(); i++) {
			ensure(s.charAt(i) >= '0' && s.charAt(i) <= '9', name
					+ " has invalid chaacters");
		}
		ensure(s.charAt(0) != '0', name + " has zero at the beginning");
	}

	public static void main(String[] args) throws FileNotFoundException {
		new Validate().run();
	}

	public class StrictScanner {
		private final BufferedReader in;
		private String line = "";
		private int pos;
		private int lineNo;

		public StrictScanner(InputStream source) {
			in = new BufferedReader(new InputStreamReader(source));
			nextLine();
		}

		public void close() {
			ensure(line == null, "Extra data at the end of file");
			try {
				in.close();
			} catch (IOException e) {
				throw new AssertionError("Failed to close with " + e);
			}
		}

		public void nextLine() {
			ensure(line != null, "EOF");
			ensure(pos == line.length(), "Extra characters on line " + lineNo);
			try {
				line = in.readLine();
			} catch (IOException e) {
				throw new AssertionError("Failed to read line with " + e);
			}
			pos = 0;
			lineNo++;
		}

		public String next() {
			ensure(line != null, "EOF");
			ensure(line.length() > 0, "Empty line " + lineNo);
			if (pos == 0)
				ensure(line.charAt(0) > ' ', "Line " + lineNo
						+ " starts with whitespace");
			else {
				ensure(pos < line.length(), "Line " + lineNo + " is over");
				ensure(line.charAt(pos) == ' ', "Wrong whitespace on line "
						+ lineNo);
				pos++;
				ensure(pos < line.length(), "Line " + lineNo + " is over");
				ensure(line.charAt(0) > ' ', "Line " + lineNo
						+ " has double whitespace");
			}
			StringBuilder sb = new StringBuilder();
			while (pos < line.length() && line.charAt(pos) > ' ')
				sb.append(line.charAt(pos++));
			return sb.toString();
		}

		public int nextInt() {
			String s = next();
			ensure(s.length() == 1 || s.charAt(0) != '0',
					"Extra leading zero in number " + s + " on line " + lineNo);
			ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
					+ " on line " + lineNo);
			try {
				return Integer.parseInt(s);
			} catch (NumberFormatException e) {
				throw new AssertionError("Malformed number " + s + " on line "
						+ lineNo);
			}
		}

		public long nextLong() {
			String s = next();
			ensure(s.length() == 1 || s.charAt(0) != '0',
					"Extra leading zero in number " + s + " on line " + lineNo);
			ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
					+ " on line " + lineNo);
			try {
				return Long.parseLong(s);
			} catch (NumberFormatException e) {
				throw new AssertionError("Malformed number " + s + " on line "
						+ lineNo);
			}
		}

		public double nextDouble() {
			String s = next();
			ensure(s.length() == 1 || s.startsWith("0.") || s.charAt(0) != '0',
					"Extra leading zero in number " + s + " on line " + lineNo);
			ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
					+ " on line " + lineNo);
			try {
				return Double.parseDouble(s);
			} catch (NumberFormatException e) {
				throw new AssertionError("Malformed number " + s + " on line "
						+ lineNo);
			}
		}
	}

	void ensure(boolean b, String message) {
		if (!b) {
			throw new AssertionError(message);
		}
	}

	void ensureLimits(long n, long from, long to, String name) {
		ensure(from <= n && n <= to, name + " must be from " + from + " to "
				+ to);
	}

	void ensureLimits(int n, int from, int to, String name) {
		ensure(from <= n && n <= to, name + " must be from " + from + " to "
				+ to);
	}

}
