#include <stdio.h>
#include <stdlib.h>

int intcmp(const void *a, const void *b){
	return *(int*)a - *(int*)b;
}

int T,N,M,A[20000],B[20000];

int main(){
	scanf("%d",&T);
	while (T--){
		scanf("%d %d",&N,&M);
		for (int i=0; i<N; i++) scanf("%d",&A[i]);
		for (int i=0; i<M; i++) scanf("%d",&B[i]);
		qsort(A,N,sizeof(int),intcmp);
		qsort(B,M,sizeof(int),intcmp);

		int total = 0;
		while (N>0 && M>0){
			if (A[N-1] > B[M-1])
				total += M, N--;
			else
				M--;
		}
		printf("%d\n",total);
	}
}
