#include <iostream>
#include <bits/stdc++.h>
// #include <iterator>
using namespace std;
#define ll long long
#define readarr(a,n) for (ll i=0; i<n; i++) {cin>>a[i];}
#define printarr(a,n) for (int i=0; i<n; i++) {cout<<a[i]<<' ';}
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define vint vector <int>
#define pb push_back
#define pf push_front
#define endl "\n"
#define fi first
#define se second

#define mod 1000000007ll
vector<ll> fact ;

vector<ll> kthpermutation(ll n, ll k) 
{
	vector<ll> v(n) ;
    ll i; 
    ll key, value;
	set<ll> u ;
	
    for(i=0; i<=n; i++)
    {
    	u.insert(i) ;
    }

    k--; 
    set<ll> ::iterator it ;
    for(i=0; i<n; i++) 
    { 
            key=k/fact[i]; 
            it = u.begin() ;
            std::advance(it,key+1) ;
            value = *it ;
            v[i]=value;
            u.erase(it) ;
            k=k%fact[i]; 
    }
    return v ;
} 
 


signed main() {
	// your code goes here
	ll q = 1 ;
	cin >> q ;
	label:
	while (q--)
	{
		ll n,x,m ;
		cin >> n >> m ;
		// ll a[n] ;
		// readarr(a,n) ;
		map <ll,ll> ma ;
		for (ll i=0; i<n; i++)
		{
			ll x ;
			cin >> x ;
			ma[x]++ ;
		}
		for (ll i=0; i<m; i++)
		{
			ll x ;
			cin >> x ;
			ma[x]++ ;
		}
		ll ans=0 ;
		for (auto i:ma)
		{
			if (i.se==2)
				ans++ ;
		}
		
		
		
		cout << ans << endl;
	}
	return 0;
}