#include <bits/stdc++.h>
using namespace std;
#define MOD 2147483647
#define MAXN 10000
int _map[MAXN][MAXN] = {0};
int _cnt[MAXN] = {0};
int n = 0,m = 0;
int homohash(string a)
{
int ret = 0;
for (int i = 0; i < a.size(); i++)
{
ret = (ret + a[i] * (1 << (i + 1)));
}
return ret;
}
void Core()
{
cin >> n;
int L;
for (int i = 1; i <= n; i++)
{
cin >> L;
_cnt[i] = L;
for (int j = 1; j <= L; j++)
{
string a;
cin >> a;
_map[i][j] = homohash(a);
}
}
cin >> m;
for(int i = 1;i <= m;i++){
string a;
int hashkey,ans;
cin >> a;
hashkey = homohash(a);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= _cnt[i];j++){
if(hashkey == _map[i][j]){
cout << i << " ";
j = 0x7fffff;
}
}
}
cout << endl;
}
}
int main()
{
Core();
return 0;
}