#include<bits/stdc++.h>
using namespace std;
int n,m;
int idx;
inline int su(char a){
return (int)(a-'a');
}
bool have[500002][110];
struct T{
int to[26];
}trie[500002];
void insert(char str[],int ms){
int len=strlen(str);
int now=0;
for(int i=0;i<len;i++){
int tos=su(str[i]);
if(!trie[now].to[tos]){
trie[now].to[tos]=++idx;
}
now=trie[now].to[tos];
}
have[now][ms]=1;
}
void find(char str[]){
int len=strlen(str);
int now=0;
for(int i=0;i<len;i++){
int tos=su(str[i]);
if(!trie[now].to[tos]){
cout<<'\n';
return;
}
now=trie[now].to[tos];
}
for(int i=1;i<=n;i++){
if(have[now][i]){
cout<<i<<' ';
}
}
cout<<'\n';
return;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int l;
scanf("%d",&l);
char str[23];
while(l--){
scanf("%s",str);
insert(str,i);
}
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
char str[23];
scanf("%s",str);
find(str);
}
return 0;
}