#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
const string ENDFLAG="@END@";
const string DEFAULT="what?";
int main()
{
//ios::sync_with_stdio(false);
string key,value;
int n,pos;
unordered_map<string,string> um;
while(getline(cin,key))
{
if(key==ENDFLAG)
{break;}
pos=key.find("]");
value=key.substr(pos+2);
key=key.substr(0,pos+1);
um[key]=value;
um[value]=key;
}
cin>>n;
getchar();
while(0<n--)
{
getline(cin,key);
value=um[key];
if(value=="")
{value=DEFAULT;}
else if(value[0]=='[')
{value=value.substr(1,value.size()-2);}
cout<<value<<endl;
}
return 0;
}