#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, tot = 1, cnt[N], tree[N][30], m;
struct IOI{
int num;
int a[1010];
}val[N];
void trie(){
int kkk = 0;
while(n--){
int x;
++kkk;
cin >> x;
for(int j = 1; j <= x; j++){
string st;
cin >> st;
int p = 1;
for(int i = 0; i < st.size(); i++){
int y = st[i] - 'a';
if(!tree[p][y])
tree[p][y] = ++tot;
p = tree[p][y];
}
val[p].num++;
val[p].a[val[p].num] = kkk;
}
}
}
void find(){
while(m--){
string st;
cin >> st;
int p = 1;
for(int i = 0; i < st .size(); i++){
int x = st[i] - 'a';
if(!tree[p][x]){
cout << endl;
break;
}
p = tree[p][x];
}
for(int i = 1; i <= val[p].num; i++){
if(val[p].a[i] != val[p].a[i - 1]){
cout << val[p].a[i] << " ";
}
}
cout << endl;
}
}
int main(){
cin >> n;
trie();
cin >> m;
find();
return 0;
}