import java.io.*;
import java.util.*;

public class squirell_sp implements Runnable {

	public static void main(String[] args) {
		new Thread(new squirell_sp()).run();
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			File f = new File(FILENAME + ".in");
			myAssert(f.length() <= 1000000, "Wrong file size");
			br = new BufferedReader(new FileReader(FILENAME + ".in"));
			out = new PrintWriter(FILENAME + ".out");
			solve();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	BufferedReader br;

	PrintWriter out;

	StringTokenizer st;

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	void myAssert(boolean e, String s) {
		assert e;
		if (!e) {
			throw new Error("Assertion failure " + s);
		}
	}

	private static final String FILENAME = "squirell";

	public void solve() throws IOException {
		int n = nextInt();
		HashMap<String, HashSet<Integer>> hm = new HashMap<String, HashSet<Integer>>();
		HashMap<String, Integer> min = new HashMap<String, Integer>();
		for (int i = 0; i < n; i++) {
			String s = nextToken();
			int j = 0;
			while (Character.isLetter(s.charAt(j))) {
				j++;
			}
			String name = s.substring(0, j);
			int number = Integer.parseInt(s.substring(j));
			myAssert(1 <= number && number <= 1e9, "Wrong number");
			if (!hm.containsKey(name)) {
				hm.put(name, new HashSet<Integer>());
				min.put(name, 1);
			}
			hm.get(name).add(number);
		}
		int k = nextInt();
		for (int i = 0; i < k; i++) {
			String s = nextToken();
			if (!hm.containsKey(s)) {
				hm.put(s, new HashSet<Integer>());
				min.put(s, 1);
			}
			int t = min.get(s);
			HashSet<Integer> hs = hm.get(s);
			while (hs.contains(t)) {
				t++;
			}
			out.println(t);
			hs.add(t);
			min.put(s, t);
		}
	}
}
