#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 10010;
map<string, set<int> > mp_title;
map<string, set<int> > mp_author;
map<string, set<int> > mp_publisher;
map<string, set<int> > mp_key;
map<string, set<int> > mp_year;
void print(map<string, set<int> > &m, string &str) {
if(m.find(str) != m.end()) {
set<int>::iterator it;
for(it = m[str].begin(); it != m[str].end(); it++) {
printf("%07d\n", *it);
}
} else {
printf("Not Found\n");
}
}
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
string tit, aut, pub, ke, ye, a;
int id;
scanf("%d\n", &id);
getline(cin, tit);
mp_title[tit].insert(id);
getline(cin, aut);
mp_author[aut].insert(id);
while(cin >> ke) {
mp_key[ke].insert(id);
char c = getchar();
if(c == '\n') break;
}
getline(cin, pub);
mp_publisher[pub].insert(id);
getline(cin, ye);
mp_year[ye].insert(id);
//cout << id << endl << tit << endl << aut << endl << ke <<endl << pub << endl << ye << endl;
}
int k;
scanf("%d", &k);
for(int i = 0; i < k; i++) {
int query;
scanf("%d: ", &query);
string temp;
getline(cin, temp);
cout << query << ": " << temp << endl;
if(query == 1) print(mp_title, temp);
else if(query == 2) print(mp_author, temp);
else if(query == 3) print(mp_key, temp);
else if(query == 4) print(mp_publisher, temp);
else if(query == 5) print(mp_year, temp);
}
return 0;
}