/**************************************************************** 
 * Description: 
 * Author: Alex Li
 * Date: 2024-01-28 23:35:48
 * LastEditTime: 2024-01-29 00:07:36
****************************************************************/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
char s[10010]; //用于存储输入的单词。
int nex[500010][26],n,cnt=0;
bool b[500010][110];
/*
s一个二维数组，用于构建字典树（Trie），存储单词的结构。
nex：一个二维数组，用于构建字典树（Trie），存储单词的结构。
n：文章的数量。
cnt：用于字典树节点编号。
b：一个二维布尔数组，用于记录每个单词在哪些文章中出现。
*/

//当输入是一串包含数字的字符时，read函数能够从中读取并返回数字部分，忽略掉非数字的字符。
inline int read(){
    int k=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		k=k*10+ch-'0';
		ch=getchar();
	}
    return k*f;
}

//insert函数读取一个单词并将其插入到字典树中。参数x表示当前处理的文章编号。
inline void insert(int x){
    scanf("%s",s+1);
	int l=strlen(s+1);
    int now=0;
    for(int i=1;i<=l;i++)
	{
        int p=s[i]-'a';
        if(!nex[now][p])         
			nex[now][p]=++cnt;  
        now=nex[now][p];          
    }
    b[now][x]=1;              
}

//check函数用于检查一个查询的单词在哪些文章中出现。它遍历字典树来找到单词，然后使用布尔数组b来确定这个单词出现在哪些文章中。
inline void check()
{
    scanf("%s",s+1);
	int l=strlen(s+1);
    int now=0,flag=1;
    for(int i=1;i<=l;i++){
        int p=s[i]-'a';
        if(!nex[now][p]){
			flag=0;
			break;
		}
        now=nex[now][p];         
    }
    if(flag)
		for(int i=1;i<=n;i++)     
			if(b[now][i])
				printf("%d ",i);  
    puts("");                
}
int main(){
    n=read(); //读取文章数量n
    for(int i=1;i<=n;i++){
        int x=read();
        for(int j=1;j<=x;j++)   
			insert(i);//读取单词数量并调用insert函数。
    }
    int m=read();//读取查询数量m，
    for(int i=1;i<=m;i++)
		check();//对于每个查询调用check函数。
    return 0;
}