// In The Name Of God

/*  ki oomade?!  */
#include <bits/stdc++.h>
 
typedef long long ll;

using namespace std;

const int N = 1e6 + 10;

ll t, n, m, a[N], b[N];

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> t;
	while(t --){
		cin >> n >> m;
		ll ans = 0;
		for(int i = 0; i < n; i ++) cin >> a[i];
		for(int i = 0; i < m; i ++) cin >> b[i];
		
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m ;j ++){
				if(a[i] == b[j]) ans ++;
			}
		}

		cout << ans << endl;
	}
}
/*           
 _   _             _               _____       ____                 _     
| | | | ___  _ __ (_)_ __   __ _  |_   _|__   |  _ \ ___  __ _  ___| |__  
| |_| |/ _ \| '_ \| | '_ \ / _` |   | |/ _ \  | |_) / _ \/ _` |/ __| '_ \ 
|  _  | (_) | |_) | | | | | (_| |   | | (_) | |  _ <  __/ (_| | (__| | | |
|_| |_|\___/| .__/|_|_| |_|\__, |   |_|\___/  |_| \_\___|\__,_|\___|_| |_|
            |_|            |___/                                          
 ____                  _       _ _     _   
/ ___| _ __   ___  ___(_) __ _| (_)___| |_ 
\___ \| '_ \ / _ \/ __| |/ _` | | / __| __|
 ___) | |_) |  __/ (__| | (_| | | \__ \ |_ 
|____/| .__/ \___|\___|_|\__,_|_|_|___/\__|
      |_|                                 
                                    							*/   