#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t--)
   {
       int n,m;
       int count=0;
       cin>>n>>m;
       map <int, int > ma;
       int a[n],b[m];
       for(int i=0;i<n;i++)
       {
           cin>>a[i];
           ma[a[i]]++;
       }
       for(int i=0;i<m;i++)
       {
           cin>>b[i];
           ma[b[i]]++;
       }
       for(auto i=ma.begin();i!=ma.end();i++)
       {
           if(i->second>1)
           {
               count=count+2;
           }
       }
       cout<<count/2<<"\n";
   }

    return 0;
}