#include <stdio.h>
#include <string.h>
#define re register
#define GC getchar()
#define Clean(X,K) memset(X,K,sizeof(X))
int Qread () {
int X = 0 ;
char C = GC ;
while (C > '9' || C < '0') C = GC ;
while (C >='0' && C <='9') {
X = X * 10 + C - '0' ;
C = GC ;
}
return X ;
}
const int Maxn = 500005 , Base = 2;
int T[Maxn][Base] , Tot = 0 , M , N , End[Maxn] , Son[Maxn] , A[Maxn];
void Add (int Now) {
int P = 0 ;
for (re int i = 0 ; i < Now ; ++ i) {
if (!T[P][A[i]]) T[P][A[i]] = ++ Tot;
++ Son[P] ;
P = T[P][A[i]] ;
}
++ End[P] ;
}
int Ask (int Now) {
int P = 0 , Ans = 0;
for (re int i = 0 ; i < Now ; ++ i) {
if (!T[P][A[i]]) return Ans ;
P = T[P][A[i]] ;
Ans += End[P] ;
}
return Ans + Son[P] ;
}
int main () {
M = Qread () , N = Qread () ;
Clean(T , 0) , Clean(End , 0) , Clean (Son , 0) ;
for (re int i = 1 ; i <= M; ++ i) {
int Now = Qread () ;
for (re int j = 0 ; j < Now ; ++ j) A[j] = Qread () ;
Add (Now) ;
}
for (re int i = 1 ; i <= N; ++ i) {
int Now = Qread () ;
for (re int j = 0 ; j < Now ; ++ j) A[j] = Qread () ;
printf ("%d
" , Ask (Now)) ;
}
fclose (stdin) , fclose (stdout) ;
return 0 ;
}