#include<bits/stdc++.h> 
using namespace std; 
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define ll long long int
#define rep(p,a,b) for(int p=a;p<b;p++)
      //kishor_e
int main()
{
    IOS;
    int t; cin>>t;
    while(t--)
    {
        int n,m,cnt=0;
        cin>>n>>m;
        vector<int>a(n);
        vector<int>b(m);
        rep(i,0,n){
            cin>>a[i];
        }
        rep(i,0,m){
            cin>>b[i];
        }
        rep(i,0,n)
        {
            rep(j,0,m)
            {
                if(a[i]==b[j])
                {
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
        a.clear();
        b.clear();
    }
}