#include #include #include #include #include #include #include #include #include #define maxn 1010 #define maxm 60 using namespace std; int a[maxn][4],b[4]; struct node { int x,y,c,z; }c[maxn*maxm]; bool cmp( node x,node y ) { if( x.x == y.x ) { if( x.y == y.y ) { if( x.c == y.c ) { return x.z < y.z; } return x.c < y.c; } return x.y < y.y; } return x.x < y.x; } int main() { int n,m,i,j,k,l,o,t; scanf("%d",&t); for( l=1;l<=t;l++ ) { printf("Case #%d:\n",l); scanf("%d%d",&n,&m); for( i=1;i<=n;i++ ) scanf("%d.%d.%d.%d",&a[i][0],&a[i][1],&a[i][2],&a[i][3]); for( i=1;i<=m;i++ ) { scanf("%d.%d.%d.%d",&b[0],&b[1],&b[2],&b[3]); for( j=1;j<=n;j++ ) { c[j].x = a[j][0]&b[0]; c[j].y = a[j][1]&b[1]; c[j].c = a[j][2]&b[2]; c[j].z = a[j][3]&b[3]; } sort( c+1,c+n+1,cmp ); k = 0; c[0].x = -1; for( j=1;j<=n;j++ ) { if( c[j].x != c[j-1].x || c[j].y != c[j-1].y || c[j].c != c[j-1].c || c[j].z != c[j-1].z ) k++; } printf("%d\n",k); } } return 0; }