#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+2e4+11;
const int dep = 666;
const int len = 30;
struct AAA{
string str;
int cnt;
}a[maxn];
bool cmp(AAA a,AAA b){
if(a.cnt!=b.cnt) return a.cnt>b.cnt;
// return a.str<b.str;
return lexicographical_compare(a.str.begin(),a.str.end(),b.str.begin(),b.str.end());
}
struct TRIE{
int son[dep][len];
bool edp[maxn];//is endPoint?
vector<int> vec[maxn];//idx of str
int tot,root;
int node(){
for(int i = 0; i < len; i++) son[tot][i]=-1;
edp[tot]=0;
return tot++;
}
void init(){
memset(son,-1,sizeof son);
memset(vec,0,sizeof vec);
tot=0;
root=node();
}
void insert(char str[]){
int now=root;
int llen = strlen(str);
for(int i = 0; i < llen; i++){
int idx=str[i]-'a';
if(son[now][idx]==-1) son[now][idx]=node();
now=son[now][idx];
}
edp[now]=1;
}
// bool asprefix(char str[]){
// int llen=strlen(str);
// int now=root;
// bool reach=0;
// for(int i = 0; i < llen; i++){
// int idx=str[i]-'a';
// if(son[now][idx]==-1&&reach) return 1;
// if(son[now][idx]==-1&&!reach) return 0;
// now=son[now][idx];
// if(edp[now]) reach=1;
// }
// return reach;
// }
void insert(string str,int k){
int now=root;
int llen=str.length();
for(int i = 0; i < llen; i++){
int idx = str[i]-'a';
if(son[now][idx]==-1) break;
now=son[now][idx];
if(edp[now]&&vec[now].size()<10) vec[now].push_back(k);
}
}
int getid(char str[]){
int now=root;
int llen=strlen(str);
for(int i = 0; i < llen; i++){
int idx=str[i]-'a';
now=son[now][idx];
}
return now;
}
}trie;
char s[maxn][30];
int main(){
ios::sync_with_stdio(false);
int n,m;
while(cin>>n){
for(int i = 1; i <= n; i++){cin>>a[i].str>>a[i].cnt;}
cin>>m;
trie.init();
for(int i = 1; i <= m; i++){
cin>>s[i];
trie.insert(s[i]);
}
sort(a+1,a+n,cmp);
for(int i = 1; i <= n; i++){
trie.insert(a[i].str,i);
}
for(int i = 1; i <= m; i++){
int id=trie.getid(s[i]);
if(i>1) cout<<endl;
for(int j = 0; j < trie.vec[id].size(); j++){
cout<<a[trie.vec[id][j]].str<<endl;
}
}
}
return 0;
}