#includeusingnamespace std;constint N =1e6+10;structNode{int next[2];Node(){memset(next,0,sizeof next);}} z[N];int ans[N], cnt[N], nd, cnt2[N];
vector <int> a[N], b[N];voidcls(){
nd =0;for(int i =0; i < N; i ++)
z[i]=Node();memset(cnt,0,sizeof cnt);}voidinsert(vector <int>&a){int len = a.size(), now =0;for(int i =0; i < len; i ++){if(!z[now].next[a[i]])
z[now].next[a[i]]=++ nd, now = nd;else
now = z[now].next[a[i]];
cnt2[now]++;}
cnt[now]++;}intquery(vector <int>&a){int len = a.size(), now =0, ans =0;for(int i =0; i < len; i ++){if(!z[now].next[a[i]])return ans;else
now = z[now].next[a[i]];
ans += cnt[now];}return ans - cnt[now]+ cnt2[now];}intmain(){int n, m;
cin >> n >> m;cls();for(int i =1; i <= n; i ++){int tot;
cin >> tot;for(int j =1; j <= tot; j ++){int x;
cin >> x;
a[i].push_back(x);}insert(a[i]);}for(int i =1; i <= m; i ++){int tot;
cin >> tot;for(int j =1; j <= tot; j ++){int x;
cin >> x;
b[i].push_back(x);}
ans[i]=query(b[i]);}for(int i =1; i <= m; i ++)
cout << ans[i]<<'\n';return0;}