// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <cstring>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 10e-7
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define E(i) (*it)[i]
// mylittlepony
using namespace std;
int main() {
// freopen("gift.in","r",stdin);
int T;
scanf(" %d",&T);
for(int t =0; t < T; t++) {
int N,Q;
cin >> N >> Q;
vector< vector<int> > A(N);
for(int i =0; i < N; i++) for(int j =0; j < 5; j++) {
double d;
scanf("%lf",&d);
A[i].push_back((int)(d*1000));}
map<int,int> M[5];
for(int i =0; i < N; i++) for(int j =0; j < 5; j++)
M[j][A[i][j]] =0;
for(int i =0; i < 5; i++) {
int a =0;
ALL_THE(M[i],it) {it->ss =a; a++;}}
int S[M[0].size()+1][M[1].size()+1][M[2].size()+1][M[3].size()+1][M[4].size()+1];
memset(S,0,sizeof(S));
for(int i =0; i < N; i++) {
for(int j =0; j < 5; j++) A[i][j] =M[j][A[i][j]];
S[A[i][0]+1][A[i][1]+1][A[i][2]+1][A[i][3]+1][A[i][4]+1]++;}
int z[5]; int y[5];
for(y[0] =1; y[0] <= M[0].size(); y[0]++)
for(y[1] =1; y[1] <= M[1].size(); y[1]++)
for(y[2] =1; y[2] <= M[2].size(); y[2]++)
for(y[3] =1; y[3] <= M[3].size(); y[3]++)
for(y[4] =1; y[4] <= M[4].size(); y[4]++)
for(int x =1; x < (1<<5); x++) {
int k =0;
for(int i =0; i < 5; i++) {
z[i] =y[i]-((x>>i)&1);
k +=((x>>i)&1);}
int a =S[z[0]][z[1]][z[2]][z[3]][z[4]];
S[y[0]][y[1]][y[2]][y[3]][y[4]] +=(2*(k%2)-1)*a;}
printf("Case %d:\n",t+1);
for(int q =0; q < Q; q++) {
int P[2][5];
int R[2][5];
for(int i =0; i < 10; i++) {
double d;
scanf(" %lf",&d);
P[i/5][i%5] =(int)(d*1000);
if(i/5 == 0) P[0][i%5]--;
R[i/5][i%5] =0;
ALL_THE(M[i%5],it) if(it->ff <= P[i/5][i%5]) R[i/5][i%5] =it->ss+1;
}
int ans =0;
for(int i =0; i < (1<<5); i++) {
int x =0;
vector<int> V(5,0);
for(int j =0; j < 5; j++) {
if((i&(1<<j)) != 0) {
V[j] =R[1][j];
x++;}
else V[j] =R[0][j];}
ans +=(2*(x%2)-1)*S[V[0]][V[1]][V[2]][V[3]][V[4]];}
printf("%d\n",ans);}
}
return 0;}
// look at my code
// my code is amazing