#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define se second
#define ld long double

using namespace std;

const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

string s;
ll n , m , cnt  ,a , b ,c , sum;
vector<ll>v;
int main(){
    ios_base::sync_with_stdio(0);
    ll T;
    cin >> T;
    while( T -- ){
        cin >> n >> m;
        cnt = a = 0;
        map<int , int>mp;
        for( int i = 0;i < n;i ++ )
            cin >> a , mp[a] ++;
        for( int i = 0;i < m;i ++ ){
            cin >> b;
            if( mp[b] )
                cnt ++;
        }
        cout << cnt << endl;
    }
}
