#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int co[400111],sa[400011],h[400011],rank[400011],nr[400011];
int a[400011],b[400011],bel[400011],p[400011],s[400011];
int n,m,t,i,x,z,q,xzq,j,ll;
int ans[50011],ask[50011][3];
void sort(int *a)
{
int i,mn;
if(ll>n)mn=ll;
else mn=n;
for(i=0;i<=mn;i++)co[i]=0;
for(i=1;i<=n;i++)co[a[i]+1]++;
for(i=1;i<=mn;i++)co[i]+=co[i-1];
for(i=1;i<=n;i++)nr[i]=0;
for(i=1;i<=n;i++){
co[a[sa[i]]]++;
nr[co[a[sa[i]]]]=sa[i];
}
for(i=1;i<=n;i++)sa[i]=nr[i];
}
void getrank()
{
int i;
x=0;
for(i=1;i<=n;i++){
if(i==1||a[sa[i]]!=a[sa[i-1]]||b[sa[i]]!=b[sa[i-1]])x++;
rank[sa[i]]=x;
}
}
void Suffix()
{
int i,j,k,l,last;
for(i=1;i<=n;i++){
sa[i]=i;
a[i]=s[i];
b[i]=0;
}
sort(a);
getrank();
l=1;
while(x!=n){
for(i=1;i<=n;i++){
a[i]=rank[i];
if(i+l<=n)b[i]=rank[i+l];
else b[i]=0;
}
sort(b);
sort(a);
getrank();
l*=2;
}
last=0;
for(i=1;i<=n;i++){
if(last)last--;
if(rank[i]==1)continue;
j=i;
k=sa[rank[i]-1];
while(j+last<=n&&k+last<=n&&s[j+last]==s[k+last])last++;
h[rank[i]]=last;
}
}
void Work()
{
int i,l,r,j;
for(i=1;i<=m;i++){
l=rank[ask[i][2]];
while(h[l]>=ask[i][1])l--;
r=rank[ask[i][2]];
while(h[r]>=ask[i][1])r++;
r--;
xzq=0;
for(j=l;j<=r;j++){
if(bel[sa[j]]>0){
if(p[bel[sa[j]]]!=i){
p[bel[sa[j]]]=i;
xzq++;
ans[bel[sa[j]]]++;
}
}
}
printf("%d\n",xzq);
}
for(i=1;i<t;i++)printf("%d ",ans[i]);
printf("%d",ans[t]);
}
int main()
{
scanf("%d%d",&t,&m);
ll=10011;
for(i=1;i<=t;i++){
scanf("%d",&z);
for(j=1;j<=z;j++){
scanf("%d",&q);
s[++n]=q;
bel[n]=i;
}
s[++n]=++ll;
scanf("%d",&z);
for(j=1;j<=z;j++){
scanf("%d",&q);
s[++n]=q;
bel[n]=i;
}
s[++n]=++ll;
}
for(i=1;i<=m;i++){
scanf("%d",&ask[i][1]);
ask[i][2]=n+1;
for(j=1;j<=ask[i][1];j++){
scanf("%d",&z);
s[++n]=z;
}
if(i!=m)s[++n]=++ll;
}
Suffix();
Work();
}