#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
map<string, set<int>>mptitle, mpauthor, mpkey, mppublish, mpyear;
void acw(map<string, set<int>>&mp, string &str){
if (mp.find(str) == mp.end()) printf("Not Found\n");
else{
for (set<int>::iterator it = mp[str].begin(); it != mp[str].end(); it++)
{
printf("%07d\n", *it);
}
}
}
int main(){
int n,id;
string title, author, key, pub, year;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &id);
char c = getchar();
getline(cin, title);
mptitle[title].insert(id);
getline(cin, author);
mpauthor[author].insert(id);
while (cin >> key){
mpkey[key].insert(id);
c = getchar();
if (c == '\n') break;
}
getline(cin, pub);
mppublish[pub].insert(id);
getline(cin, year);
mpyear[year].insert(id);
}
int m,num;
string wstr;
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d: ", &num);
getline(cin, wstr);
cout << num<<": " << wstr<<endl;
if (num == 1) acw(mptitle, wstr);
else if (num == 2) acw(mpauthor, wstr);
else if (num == 3) acw(mpkey, wstr);
else if (num == 4) acw(mppublish, wstr);
else acw(mpyear, wstr);
}
return 0;
}