#include #include #include using namespace std; int m, n; int a[1001][5]; int b[5]; __int64 ans[1001]; int main(){ int t,cas=1; scanf("%d", &t); while (t--){ scanf("%d%d", &n, &m); printf("Case #%d:\n", cas++); for (int i = 1; i <= n; i++) scanf("%d.%d.%d.%d", &a[i][1], &a[i][2], &a[i][3], &a[i][4]); for (int i = 1; i <= m; i++){ memset(ans, 0, sizeof(ans)); scanf("%d.%d.%d.%d", &b[1], &b[2], &b[3], &b[4]); for (int j = 1; j <= n; j++){ int x = 1; for (int p = 1; p <= 4; p++){ ans[j] += x*(a[j][p] & b[p]); x *= 256; } } sort(ans + 1, ans + 1 + n); int res = 1; for (int j = 2; j <= n; j++) if (ans[j] != ans[j - 1]) res++; printf("%d\n", res); } } }