public class SparseArrays {
    // https://www.hackerrank.com/challenges/sparse-arrays/problem

    public SparseArrays() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        HashMap map = new HashMap();
        for (int i = 0; i < n; i++) {
            String str = scanner.nextLine();
            if (!map.containsKey(str))
                map.put(str, 1);
            else {
                int count = map.get(str);
                map.put(str, ++count);
            }
        }


        int q = scanner.nextInt();
        scanner.nextLine();
        for (int i = 0; i < q; i++) {
            String str = scanner.nextLine();
            if (map.containsKey(str))
                System.out.println(map.get(str));
            else
                System.out.println("0");
        }
    }
}