#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vl vector<ll> 
#define pb push_back
#define For(i,n) for(ll i=0;i<n;i++)
#define MOD 1000000007
#define MAX 100005
#define pll pair <ll,ll>
#define fi first
#define se second
using namespace std;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int tc=1;
	cin>>tc;
	For(ti,tc){
		int n,m;
		cin>>n>>m;
		set<int> s;
		For(i,n){
			int x;
			cin>>x;
			s.insert(x);
		}
		int cnt=0;
		For(i,m){
			int x;
			cin>>x;
			if(s.find(x)!=s.end()){
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}	
}