#include<algorithm>
#include<iostream>
#include<typeinfo>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<cmath>
#include<list>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
namespace fast_IO
{
template <typename T> inline void write(T x)
{
if ( x < 0 ) putchar('-'), x = -x;
if ( x == 0 )
{
putchar('0');
return;
}
if ( x > 9 ) write(x/10);
putchar(x % 10 + '0');
return;
}
template <typename T> inline void read(T &x)
{
x = 0; int w = 1; char ch = getchar();
while ( !isdigit(ch) )
{
if ( ch == '-' ) w = -1; ch = getchar();
}
while ( isdigit(ch) )
x = (x<<1) + (x<<3) + (ch^48), ch = getchar();
x *= w;
return;
}
}
using namespace fast_IO;
using namespace std;
using ll = long long;
#define writel(x) write(x),putchar('\n')
#define writes(x) write(x),putchar(' ')
const int MAXN = 5e3+5;
int n, m, cnt;
bool a[1005][MAXN];
unordered_map<string, int> unp;
int main()
{
read(n);
for (int i=1; i<=n; i++)
{
int l;
read(l);
string s;
for (int j=1; j<=l; j++)
{
cin >> s;
if (unp[s] == 0) unp[s] = ++cnt;
if (unp[s] != 0) a[i][unp[s]] = true;
}
}
read(m);
for (int i=1; i<=m; i++)
{
string s;
cin >> s;
for (int i=1; i<=n; i++)
{
if (a[i][unp[s]])
{
writes(i);
}
}
puts("");
}
return 0;
}