#include #include #include #include #include #define LL long long using namespace std;; const int N = 1010; struct node { int A, B, C, D; bool operator < (const node& x) const { if (A != x.A) return A < x.A; if (B != x.B) return B < x.B; if (C != x.C) return C < x.C; return D < x.D; } }a[N]; int main() { int T, kase = 1; scanf("%d", &T); while (T--) { int n, q; scanf("%d%d", &n, &q); for (int i = 0; i < n; ++i) scanf("%d.%d.%d.%d", &a[i].A, &a[i].B, &a[i].C, &a[i].D); printf("Case #%d:\n", kase++); while (q--) { set s; int A, B, C, D; scanf("%d.%d.%d.%d", &A, &B, &C, &D); int ans = 0; for (int i = 0; i < n; ++i) { node t; t.A = A & a[i].A, t.B = B & a[i].B; t.C = C & a[i].C, t.D = D & a[i].D; if (s.find(t) == s.end()) { s.insert(t); ++ans; } } printf("%d\n", ans); } } return 0; }