# include # include const int Max = 1001; int ip[Max][4]; int ym[51][4]; int test[Max][4]; int main() { int T, i, j, k, n, m, t, count, exist, flag; int a, b, c, d; scanf("%d", &T); for(t = 1; t <= T; t++){ scanf("%d%d", &n, &m); printf("Case #%d:\n", t); for(i = 0; i < n; i++){ scanf("%d.%d.%d.%d", &ip[i][0], &ip[i][1], &ip[i][2], &ip[i][3]); } for(i = 0; i < m; i++){ scanf("%d.%d.%d.%d", &ym[i][0], &ym[i][1], &ym[i][2], &ym[i][3]); } for(j = 0; j < m; j++){ count = 0; exist = 0; memset(test, 0, sizeof(test)); for(i = 0; i < n; i++){ a = ip[i][0]&ym[j][0]; b = ip[i][1]&ym[j][1]; c = ip[i][2]&ym[j][2]; d = ip[i][3]&ym[j][3]; for(k = 0, flag = 1; k < exist; k++){ if(test[k][0] == a && test[k][1] == b && test[k][2] == c && test[k][3] == d){ flag = 0; break; } } if(flag){ count++; test[exist][0] = a; test[exist][1] = b; test[exist][2] = c; test[exist][3] = d; exist++; } } printf("%d\n", count); } } return 0; }