#include<bits/stdc++.h>
using namespace std;
struct trie{
int c[25];
}t[10005];
int n,m,T,cnt,x[5001][5000],root1;
string s;
int g(char x){
if(x>='A'&&x<='Z'){
return x-'A';
}else if(x>='a'&&x<='z'){
return x-'a'+26;
}else{
return x-'0'+52;
}
}
void ins(string s,int k){
int root=0;
for(int i=0;i<s.size();i++){
int ch=g(s[i]);
if(t[root].c[ch]==0){
t[root].c[ch]=cnt++;
}
root=t[root].c[ch];
}
x[root][k]=1;
}
void find(string s){
int root=0;
for(int i=0;i<s.size();i++){
int ch=g(s[i]);
if(!t[root].c[ch]){
cout<<'\n';
return;
}
root=t[root].c[ch];
}
for(int i=1;i<=n;i++){
if(x[root][i]){
cout<<i<<' ';
}
}
cout<<'\n';
return;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>T;
for(int j=1;j<=T;j++){
cin>>s;
ins(s,i);
}
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>s;
find(s);
}
return 0;
}