#include #include #include using namespace std; #define N 1005 #define M 55 int a[4][N],A[4][M]; int n,m,i,j,k,ans,root,tot,T,now; bool flag; struct node{ int nex[256]; }t[N*4]; int main(){ scanf("%d",&T); for (int Case=1;Case<=T;Case++){ scanf("%d%d",&n,&m); for (i=1;i<=n;i++){ scanf("%d.%d.%d.%d",&a[0][i],&a[1][i],&a[2][i],&a[3][i]); } for (i=1;i<=m;i++){ scanf("%d.%d.%d.%d",&A[0][i],&A[1][i],&A[2][i],&A[3][i]); } printf("Case #%d:\n",Case); for (i=1;i<=m;i++){ ans=0; tot=0; root=0; memset(t[root].nex,0,sizeof(t[root].nex)); for (j=1;j<=n;j++){ now=root; flag=0; for (k=0;k<4;k++){ if (!t[now].nex[a[k][j]&A[k][i]]){ t[now].nex[a[k][j]&A[k][i]]=++tot; now=tot; memset(t[now].nex,0,sizeof(t[now].nex)); if (!flag){ flag=1; ans++; } } else now=t[now].nex[a[k][j]&A[k][i]]; } } printf("%d\n",ans); } } return 0; }