#include<bits/stdc++.h>

using namespace std;

int main()
{
    
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test;
cin>>test;
while(test--){
   int n,m;
   cin>>n>>m;
   map<int,int> mp;
   int cnt=0;
   int val;
   for(int i=0;i<n;i++){
      cin>>val;
      mp[val]++;

   }
   for(int i=0;i<m;i++){
      cin>>val;
      mp[val]++;
      if(mp[val]>1){
         
         cnt++;
      }
   }
   cout<<cnt<<endl;
   
 
}
   
}