#include <bits/stdc++.h>
const int N = 500000;
int n, m, cnt = 1;
struct node {
bool isend;
int son[26];
std::vector<int>ans;
} t[N];
void Insert (char *s, int x) {
int now = 0;
for (int i = 0; s[i]; i++) {
int ch = s[i] - 'a';
if (!t[now].son[ch])
t[now].son[ch] = cnt++;
now = t[now].son[ch];
}
t[now].ans.push_back(x);
t[now].isend = true;
}
void Find (char *s) {
int now = 0;
for (int i = 0; s[i]; i++) {
int ch = s[i] - 'a';
if (!t[now].son[ch]) {
puts("");
return;
}
now = t[now].son[ch];
}
if (!t[now].isend) {
puts("");
return;
}
for (auto i : t[now].ans)
printf ("%d ", i);
puts("");
}
int main (void) {
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
int l;
scanf ("%d", &l);
for (int j = 1; j <= l; j++) {
char s[21];
scanf ("%s", s);
Insert(s, i);
}
}
scanf ("%d", &m);
for (int i = 1; i <= m; i++) {
char s[21];
scanf ("%s", s);
Find(s);
}
return 0;
}