1 #include<cstdio>
2
3 #include<cstdlib>
4
5 #include<cmath>
6
7 #include<cstring>
8
9 #include<algorithm>
10
11 #include<iostream>
12
13 #include<vector>
14
15 #include<map>
16
17 #include<set>
18
19 #include<queue>
20
21 #include<string>
22
23 #define inf 1000000000
24
25 #define maxn 1000000+1000
26
27 #define maxm 500+100
28
29 #define eps 1e-10
30
31 #define ll long long
32
33 #define pa pair<int,int>
34
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42
43 #define mod 1000000007
44
45 using namespace std;
46
47 inline int read()
48
49 {
50
51 int x=0,f=1;char ch=getchar();
52
53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
54
55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
56
57 return x*f;
58
59 }
60 set<int>s;
61 set<int>::iterator it;
62 int a[maxn],b[maxn],v[maxn],ans[maxn],rank[maxn],h[maxn],sa[maxn],len[maxn],pos[maxn],wr[maxn],wa[maxn],wb[maxn];
63 int n,m,cnt,tot,mid;
64 inline bool cmp(int *r,int a,int b,int l)
65 {
66 return r[a]==r[b]&&r[a+l]==r[b+l];
67 }
68 void da(int n,int m)
69 {
70 int *x=wa,*y=wb,*t;
71 for0(i,m)wr[i]=0;
72 for0(i,n)wr[x[i]=a[i]]++;
73 for1(i,m)wr[i]+=wr[i-1];
74 for3(i,n,0)sa[--wr[x[i]]]=i;
75 for(int p=1,j=1;p<=n;j<<=1,m=p)
76 {
77 p=0;
78 for2(i,n-j+1,n)y[p++]=i;
79 for0(i,n)if(sa[i]>=j)y[p++]=sa[i]-j;
80 for0(i,m)wr[i]=0;
81 for0(i,n)wr[x[y[i]]]++;
82 for1(i,m)wr[i]+=wr[i-1];
83 for3(i,n,0)sa[--wr[x[y[i]]]]=y[i];
84 int i;
85 for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<=n;i++)
86 x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
87 }
88 }
89 void geth(int n)
90 {
91 for1(i,n)rank[sa[i]]=i;
92 for(int i=0,k=0,j;i<n;h[rank[i++]]=k)
93 for(k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k];k++);
94 }
95
96 int main()
97
98 {
99
100 freopen("input.txt","r",stdin);
101
102 freopen("output.txt","w",stdout);
103
104 n=read();m=read();tot=-1;
105 for1(i,n)
106 {
107 int x=read();
108 for1(j,x)a[++tot]=read()+1,b[tot]=i;
109 x=read();
110 a[++tot]=0;
111 for1(j,x)a[++tot]=read()+1,b[tot]=i;
112 a[++tot]=0;
113 }
114 a[++tot]=0;mid=tot;
115 for1(i,m)
116 {
117 len[i]=read();
118 pos[i]=tot+1;
119 for1(j,len[i])a[++tot]=read()+1;
120 a[++tot]=0;
121 }
122 cnt=0;
123 for0(i,tot)if(!v[a[i]])a[i]=v[a[i]]=++cnt;else a[i]=v[a[i]];
124 a[++tot]=0;
125 da(tot,cnt);
126 geth(tot);
127 for1(i,m)
128 {
129 s.clear();
130 int x=rank[pos[i]],y=len[i];
131 for2(j,x+1,tot)
132 {
133 if(h[j]<y)break;
134 if(sa[j]>=mid)continue;
135 if(b[sa[j]])s.insert(b[sa[j]]);
136 }
137 for3(j,x,1)
138 {
139 if(h[j]<y)break;
140 if(sa[j-1]>=mid)continue;
141 if(b[sa[j-1]])s.insert(b[sa[j-1]]);
142 }
143 for(it=s.begin();it!=s.end();it++)ans[*it]++;
144 printf("%d\n",s.size());
145 }
146 for1(i,n-1)printf("%d ",ans[i]);printf("%d\n",ans[n]);
147
148 return 0;
149
150 }