#include <bits/stdc++.h>
using namespace std;



int main (){
	int t;
	cin>>t;
	while(t--) {
	    int n, m;
	    cin>>n>>m;
	    int up[n];
	    int right[m];
	    for (int i=0; i<n; ++i){
	    	cin>>up[i];
	    }
	    int count = 0;
	    for (int i=0; i<m; ++i){
	    	cin>>right[i];
	    	for (int j=0; j<n; ++j){
	    		if (right[i]==up[j]){
	    			count++;
	    			break;
	    		}
	    	}
	    }

	    cout<<count<<"\n";


	    
	}

	return 0;
}