#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int MAX = 522133279; const double PI = 3.1415926535897; int T; int N, M; struct IP { public: int a, b, c, d; bool operator <( const IP &other )const { if ( a != other.a ) return a < other.a; if ( b != other.b ) return b < other.b; if ( c != other.c ) return c < other.c; return d < other.d; } bool operator ==( const IP &other )const { return a == other.a && b == other.b && c == other.c && d == other.d; } }; vector ipList, maskList; int main( ) { scanf( "%d", &T ); for ( int cs = 1; cs <= T; cs++ ) { ipList.clear( ); maskList.clear( ); scanf( "%d %d", &N, &M ); for ( int i = 0; i < N; i++ ) { IP ip; scanf( "%d.%d.%d.%d", &ip.a, &ip.b, &ip.c, &ip.d ); ipList.push_back( ip ); } printf( "Case #%d:\n", cs ); for ( int i = 0; i < M; i++ ) { IP ip; scanf( "%d.%d.%d.%d", &ip.a, &ip.b, &ip.c, &ip.d ); vector res; for ( int i = 0; i < N; i++ ) { IP rip; rip.a = ipList[i].a & ip.a; rip.b = ipList[i].b & ip.b; rip.c = ipList[i].c & ip.c; rip.d = ipList[i].d & ip.d; res.push_back( rip ); } int cnt=res.size( ); sort( res.begin( ), res.end( ) ); for ( int i = 1; i < res.size( ); i++ ) { if ( res[i] == res[i - 1] ) cnt--; } printf( "%d\n", cnt ); } } return 0; }