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

void run_case() {
    int n, m;
    cin >> n >> m;
    set<int> a;
    int x;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        a.insert(x);
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
        cin >> x;
        if (a.find(x) != a.end())
            ++ans;
    }
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(0);cin.tie(0);
    int _t;
    cin >> _t;
    while (_t--)
        run_case();       
}