#define _CRT_SECURE_NO_WANRNINGS
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int n,m, res = 0;
    cin >> n >> m;

    vector<bool> t(101);

    for (int j = 0; j < n; j++ ) {
      int pos;
      cin >> pos;

      t[pos] = true;
    } 
    for (int j = 0; j < m; j++) {
      int pos;
      cin >> pos;

      if (t[pos] == true) {
        res++;
      }
    }

    cout << res << endl;
  }
}