#include<cstdio> #include<vector> #include<iostream> using std::cin; using std::vector; constint N = 5e5 + 10; int m, n, f[N]; int idx, cnt[N], tr[N][2]; intdfs(int x) { if (f[x]) return f[x]; int res = cnt[x]; for (int i = 0; i < 2; i++) if (tr[x][i]) res += dfs(tr[x][i]); return f[x] = res; } voidinsert(vector<bool> str) { int p = 0; for (int i = 0; i < str.size(); i++) { int t = str[i]; if (!tr[p][t]) tr[p][t] = ++idx; p = tr[p][t]; } cnt[p]++; } intquery(vector<bool> str) { int res = 0; int p = 0; for (int i = 0; i < str.size(); i++) { res += cnt[p]; int t = str[i]; if (!tr[p][t]) return res; p = tr[p][t]; } return res + dfs(p); } intmain() { scanf("%d%d", &m, &n); for (int i = 1, a, b; i <= m; i++) { vector<bool> str; scanf("%d", &a); while (a--) { scanf("%d", &b); str.push_back(b); } insert(str); } for (int i = 1, a, b; i <= n; i++) { vector<bool> str; scanf("%d", &a); while (a--) { scanf("%d", &b); str.push_back(b); } printf("%d\n", query(str)); } return0; }