    #include<bits/stdc++.h>

    #define repa(i, s, e) for (int i = s; i <= e; i++);
    #define repb(i, s, e) for (int i = e; i >= s; i--);
    #define PI 3.1415926535897932384626433832795

    using namespace std;

    typedef long int li;
    typedef long long int lli;
    typedef pair<li, li> pii;
    typedef vector<li> vi;
    typedef vector<string> vs;
    typedef vector<char> vc;
    typedef vector<pii> vii;
    typedef vector<vi> vvi;
    typedef map<li, li> mpii;
    typedef set<li> seti;

    const int MAX_N = 1e6 + 1;
    const int MOD = 1e9 + 7;
    const int INF = 1e9;
    const lli LINF = 1e18;
    const lli N = 100001;

    void snake_bite()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cout << fixed;
    }

    void read(vi a, li n)
    {
        for (int i = 0; i < n; i++)
            cin >> a[i];
    }

    void display(vi a, li n)
    {
        for (int i = 0; i < n; i++)
            cout << a[i] << " ";
    }

    void solve()
    {
        // sort(a.begin(), a.end(), greater<int>()) -- for reverse sort
        // 
        int n, m;
        cin >> n >> m;
        vi a(n);
        vi b(m);
        for (int i = 0; i < n; i++)
            cin >> a[i];

        for (int i = 0; i < m; i++)
            cin >> b[i];

        sort(a.begin(), a.end());
        sort(b.begin(), b.end());

        int i = 0, j = 0;
        int count = 0;
        while (i < n && j < m)
        {
            if (a[i] == b[j]) count++, i++, j++;
            else if (a[i] > b[j]) j++;
            else i++;
        }

        cout << count;
    }

    int main()
    {
        snake_bite();
        //InverseofNumber(MOD);
        //InverseofFactorial(MOD);
        //factorial(MOD);
        // vector <li> primes;
        // SieveOfEratosthenes(primes);
        
        #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        #endif

        li t;
        cin >> t;
        //t = 1;
        while (t--)
        {
            solve();
            cout << endl;
        }
        //std::cout << "Goodbye World!\n";
    }

