#include<cstdio>
#include<map>
#include<string>
using namespace std;
int main() {
map<string, string> moMap;
map<string, string> zhouMap;
//构建魔咒词典
while (true) {
char mo[200];
fgets(mo, sizeof(mo), stdin); //输入一行
string mostr = mo;
mostr.pop_back();//c--->c++去掉末尾换行
if (mostr == "@END@") {
break;
}
int i = mostr.find("]");
string word = mostr.substr(0, i + 1);
string word2 = mostr.substr(i + 2);
// printf("%s %s\n",word.c_str(),word2.c_str());
moMap.insert(pair<string, string> (word, word2));
zhouMap.insert(pair<string, string> (word2, word));
}
int n ;
scanf("%d", &n);
getchar();
for (int i = 0 ; i < n ; ++i) {
char line[100];
fgets(line, sizeof(line), stdin);
string linestr = line;
linestr.pop_back();
if (moMap.find(linestr) != moMap.end() ||
zhouMap.find(linestr) != zhouMap.end()) {
//在魔法map中
if (linestr[0] == '[') {
//mo中
printf("%s\n", moMap[linestr].c_str());
} else {
//zhou里找
string out = zhouMap[linestr];
printf("%s\n", out.substr(1, out.size() - 2).c_str());
}
} else {
printf("what?\n");
}
}
}