/**
 * Author : redhooded
 * Created : 04/12/2020
 * Time : 14:52:17
 * */

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp make_pair
#define Max(a, b, c) max(a, max(b, c))
#define Min(a, b, c) min(a, min(b, c))
#define si set<int>
#define sll set<long, long>
#define mpii map<int, int>
#define mpll map<ll, ll>
#define allout(a) for(auto i : a) cout << i << " "; cout << '\n';

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c>  d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

void solve(void) {
    int n, m;
    cin >> n >> m;


    int ans = 0;
    set<int> st;

    for(int i = 0; i<n; i++) {
    	int temp;
    	cin >> temp;
    	st.insert(temp);
    }

    for(int i = 0; i<m; i++) {
    	int temp;
    	cin >> temp;
    	if(st.find(temp) != st.end()){
    		ans++;
    	}
    }

    cout << ans << '\n';

}

int main(void) {
    ios_base :: sync_with_stdio(false);
    cout.tie(NULL);
    cin.tie(NULL);

    int t = 1;
    cin >> t;

    while(t--) {
        solve();
    }

    return 0;
}