1 type
2 point=^node;
3 node=record
4 tt,ex:longint;
5 next:array[0..1] of point;
6 end;
7 var
8 i,j,k,l,m,n:longint;
9 head:point;
10 s1:ansistring;
11 function newp:point;
12 var p:point;
13 begin
14 new(p);p^.tt:=0;p^.ex:=0;
15 p^.next[0]:=nil;p^.next[1]:=nil;
16 exit(p);
17 end;
18 procedure insert(s1:ansistring);
19 var i:longint;p:point;
20 begin
21 p:=head;
22 for i:=1 to length(s1) do
23 begin
24 if p^.next[ord(s1[i])-48]=nil then
25 p^.next[ord(s1[i])-48]:=newp;
26 p:=p^.next[ord(s1[i])-48];
27 inc(p^.tt);
28 end;
29 inc(p^.ex);
30 end;
31 function num(s1:ansistring):longint;
32 var i,j,k,l:longint;p:point;
33 begin
34 p:=head;j:=0;k:=0;l:=0;
35 for i:=1 to length(s1) do
36 begin
37 if p^.next[ord(s1[i])-48]=nil then break;
38 p:=p^.next[ord(s1[i])-48];
39 k:=k+j;
40 j:=p^.ex;
41 l:=i;
42 end;
43 if l<>i then num:=k+j else num:=k+p^.tt;
44 end;
45 begin
46 readln(n,m);
47 head:=newp;
48 for i:=1 to n do
49 begin
50 read(l);s1:=‘‘;
51 for j:=1 to l do
52 begin
53 read(k);
54 s1:=s1+chr(48+k);
55 end;
56 readln;
57 insert(s1);
58 end;
59 for i:=1 to m do
60 begin
61 read(l);s1:=‘‘;
62 for j:=1 to l do
63 begin
64 read(k);
65 s1:=s1+chr(48+k);
66 end;
67 readln;
68 writeln(num(s1));
69 end;
70 readln;
71 end.