#include <array>
#include <cassert>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>

using namespace std;

map<string, string> dictionary;

void build_dictionary() {
    array<string, 26> letters;

    for (int i = 0; i < 26; i++) {
        char letter;
        cin >> letter;
        assert(letter >= 'A' && letter <= 'Z');
        cin >> letters[letter - 'A'];
    }

    int num_words;
    cin >> num_words;
    for (int i = 0; i < num_words; i++) {
        string word;
        cin >> word;
        string word_morse;
        for (char c : word) {
            assert(c >= 'A' && c <= 'Z');
            word_morse.append(letters[c - 'A']);
        }
        dictionary[word_morse] = word;
    }
}

void handle_case(int num_words) {
    vector<string> words(num_words);
    for (string& word : words) {
        cin >> word;
    }
    for (string& word : words) {
        try {
            word = dictionary.at(word);
        } catch (out_of_range) {
            cout << word << " not in dictionary.\n";
            return;
        }
    }
    bool first = true;
    for (string& word : words) {
        if (!first) cout << ' ';
        cout << word;
        first = false;
    }
    cout << '\n';
}

int main() {
    build_dictionary();
    int num_words;
    while (cin >> num_words && num_words)
        handle_case(num_words);
}