#include <iostream>
#include <map>
#include <string>
#include <cstdio>
using namespace std;

map<string, string> dict;

int main() {
    while (true) {
        char line[200];
        fgets(line, 200, stdin); //输入一行
        string linestr = line; //c->c++
        linestr.pop_back(); //去掉换行
        if (linestr == "@END@") {
            break;
        }
        string word = linestr.substr(0, linestr.find(']') + 1);
        string info = linestr.substr(linestr.find(']') + 2);
        //printf("%s\n%s\n", word.c_str(), info.c_str());
        dict[info] = word;
        dict[word] = info;
    }

    int n;
    scanf("%d", &n);
    getchar(); //去掉换行,坑!
    for (int i = 0; i < n; i++) {
        char line[200];
        fgets(line, 200, stdin);
        string linestr = line;
        linestr.pop_back();

        if (dict.find(linestr) != dict.end()) {
            if (linestr.find("[") != -1) { //有[,为魔咒
                printf("%s\n", dict[linestr].c_str());

            } else {
                printf("%s\n", dict[linestr].substr(1, dict[linestr].size() - 2).c_str());
            }
        } else {
            printf("what?\n");
        }
    }


}