#include<bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for(int (i) = (a); (i) <= (b); (i)++)
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0' ,ch = getchar();
return x * f;
}
void write(ll x)
{
if (x < 0) putchar('-') , x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
return;
}
unordered_map<string, array<bool, 1000>> mp;
ll n, m, k;
string s;
int main()
{
n = read();
rep (i, 1, n)
{
k = read();
rep (j, 1, k)
{
cin >> s;
mp[s][i] = true;
}
}
m = read();
rep (i, 1, m)
{
cin >> s;
rep (j, 1, n)
{
if (mp[s][j]) write(j), putchar(' ');
}
putchar('\n');
}
return 0;
}
rt