#include <stdio.h>
#include <string>
using namespace std;
struct trie
{
int next[10000][26], cnt;
bool exist[10000];
//
void insert(string s, int l)
{
int p = 0;
for (int i = 0; i < l; i++)
{
int c = s[i] - 'a';
if (!next[p][c])
next[p][c] = ++cnt;
p = next[p][c];
}
exist[p] = 1;
}
//
bool find(string s, int l)
{
int p = 0;
for (int i = 0; i < l; i++)
{
int c = s[i] - 'a';
if (!next[p][c])
return false;
p = next[p][c];
}
return exist[p];
}
} TRIE[1010];
int main()
{
int N;
scanf("%d", &N);
for (int now = 1; now <= N; now++)
{
int num;
scanf("%d ", &num);
for (int i = 0; i < num; i++)
{
char temp;
string temp_1 = "";
int l = 0;
while (true)
{
scanf("%c", &temp);
if (temp == ' ' || temp == '\n')
break;
temp_1 += temp;
l++;
}
TRIE[now].insert(temp_1, l);
}
}
int M;
scanf("%d\n", &M);
while (M--)
{
char temp;
string temp_1 = "";
int l = 0;
while (true)
{
scanf("%c", &temp);
if (temp == ' ' || temp == '\n')
break;
temp_1 += temp;
l++;
}
bool s = true;
for (int i = 1; i <= N; i++)
{
if (s && TRIE[i].find(temp_1, l))
{
printf("%d", i);
s = false;
continue;
}
else if (TRIE[i].find(temp_1, l))
{
printf(" %d", i);
}
}
if (M != 0)
printf("\n");
}
return 0;
}