#include <algorithm>
#include <iostream>
using namespace std;
 
int binarySearch(int arr[], int l, int r, int x);
 

int printIntersection(int arr1[], int arr2[], int m, int n)
{
   
    int count=0;
    if (m > n) {
        int* tempp = arr1;
        arr1 = arr2;
        arr2 = tempp;
 
        int temp = m;
        m = n;
        n = temp;
    }
 
   
    sort(arr1, arr1 + m);
 
   
    for (int i = 0; i < n; i++)
        if (binarySearch(arr1, 0, m - 1, arr2[i]) != -1)
            //cout << arr2[i] << " ";
            count++;
        
            
    return count;    
}
 
int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;
 
       
        if (arr[mid] == x)
            return mid;
 
       
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
 
       
        return binarySearch(arr, mid + 1, r, x);
    }
 
    
    return -1;
}
 
/* Driver program to test above function */
int main()
{
   
   	int t;cin>>t;
	int arr1[104];
	int arr2[104];
	while(t--){
		int m,n;
		cin>>m>>n;
		for(int i=0;i<m;i++){
			cin>>arr1[i];
		}
		for(int i=0;i<n;i++){
			cin>>arr2[i];
		}
		
		
		sort(arr1, arr1 + m);
		sort(arr2, arr2 + n);
		//cout << "nIntersection of two arrays is \n";
    	cout<<printIntersection(arr1, arr2, m, n);
    	cout<<endl;
		
	}
    

    return 0;
}