#include<iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--){
        int n,m, c = 0;
        cin >> n >> m;
        int btm[n], left[m];

        for(int i = 0; i < n; i++)
            cin >> btm[i];

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

        int l = 0, b = 0;

        while((b < n) && (l < m)){
            if(btm[b] == left[l]){
                c++;
                b++;
                l++;
            }
            else if(btm[b] > left[l])
                l++;
            else
                b++;
        }

        cout << c << endl;
    }
}
