import java.util.*;

public class morse_hamerly {

    public static void check(boolean condition) throws Exception {
        if (!condition)
            throw new Exception("Input validation failed");
    }

    public static void main(String args[]) throws Exception {
        Scanner input = new Scanner(System.in);

        String morse[] = new String[26];
        for (int i = 0; i < 26; ++i) {
            String tokens[] = input.nextLine().split(" ");
            morse[i] = tokens[1];
        }

        Map<String, String> dictionary = new HashMap<String, String>();
        int dictSize = Integer.parseInt(input.nextLine());
        check(0 <= dictSize && dictSize <= 100);
        for (int i = 0; i < dictSize; ++i) {
            String word = input.nextLine();
            check(1 <= word.length() && word.length() <= 100);
            String coded = "";
            for (char c : word.toCharArray()) {
                coded += morse[c - 'A'];
            }
            dictionary.put(coded, word);
        }

        while (true) {
            int numCodes = Integer.parseInt(input.nextLine());
            if (numCodes == 0) {
                break;
            }
            String phrase[] = new String[numCodes];
            String notInDict = null;
            for (int j = 0; j < numCodes; ++j) {
                String code = input.nextLine();
                phrase[j] = dictionary.get(code);
                if (phrase[j] == null && notInDict == null) {
                    notInDict = code;
                }
            }

            if (notInDict == null) {
                boolean first = true;
                for (String s : phrase) {
                    if (!first)
                        System.out.print(" ");
                    System.out.print(s);
                    first = false;
                }
                System.out.println();
            } else {
                System.out.println(notInDict + " not in dictionary.");
            }
        }
    }
}