#include <bits/stdc++.h>
using namespace std;
void solve(){
	int n,m,x;
	scanf("%d%d",&n,&m);
	map<int,int>mp;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		mp[x]++;
	}
	int ret=0;
	for(int  i=1;i<=m;i++){
		scanf("%d",&x);
		ret += mp.count(x);
	}
	printf("%d\n",ret);
}
int main(){
/*	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	int tc;
	scanf("%d",&tc);
	while(tc--){
		solve();
	}
}