#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define base 261
#define mod 114514
int n, l, m;
typedef long long LL;
char tmp[50];
vector <int> a[mod + 2];
void insert(int num) {
LL hash = 1;
for (int i = 0; tmp[i]; i++) {
hash = (hash * 111 * base + tmp[i]) % mod;
}
int n = num;
for (int i = 0; i < a[hash].size(); i++) {
if (a[hash][i] == n) {
return;
}
}
a[hash].push_back(n);
}
void decode() {
LL hash = 1;
for (int i = 0; tmp[i]; i++) {
hash = (hash * 111 * base + tmp[i]) % mod;
}
LL l = a[hash].size();
for (int i = 0; i < l; i++) {
cout << a[hash][i] << (i == l-1 ? "" : " ");
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l;
while (l--) {
cin >> tmp;
insert(i);
}
}
cin >> m;
while (m--) {
cin >> tmp;
decode();
cout << (m==0?"":"\n");
}
}