// hilo

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define cast(type, data) static_cast <type>(data)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

template <class A> void transpose(std::vector <std::vector <A>>& v) { std::vector <std::vector <A>> result((int)v[0].size(), std::vector <A>((int)v.size())); for (int i = 0; i < (int)v.size(); i++) { for (int j = 0; j < (int)v[0].size(); j++) { result[j][i] = v[i][j]; } } v = result; }

std::string to_string(const std::string& s) { return '"' + s + '"'; }
std::string to_string(const char* s) { return to_string((std::string)s); }
std::string to_string(const bool& b) { return (b ? "true" : "false"); }
std::string to_string(const char& c) { return (std::string)"'" + c + "'"; }
std::string to_string(const std::vector <bool>& v) { if (v.empty()) { return "{ }"; } std::string ret = "{" + to_string(v.front()); for (int i = 1; i < cast(int, v.size()); i++) { ret += ", " + to_string(v[i]); } return ret + "}"; }
template <typename A> std::string to_string(A x) { return std::to_string(x); }
template <typename A, typename B> std::string to_string(std::pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> std::string to_string(const std::vector <A>& v) { if (v.empty()) { return "{ }"; } std::string ret = "{" + to_string(v.front()); for (int i = 1; i < cast(int, v.size()); i++) { ret += ", " + to_string(v[i]); } return ret + "}"; }
template <typename A> std::string to_string(const std::set <A>& st) { if (st.empty()) { return "[empty]"; } auto it = st.begin(); std::string ret = "[" + to_string(*it); it++; for (; it != st.end(); it++) { ret += ", " + to_string(*it); } return ret + "]"; }
template <typename A, typename B> std::string to_string(const std::set <A, B>& st) { if (st.empty()) { return "[empty]"; } auto it = st.begin(); std::string ret = "[" + to_string(*it); it++; for (; it != st.end(); it++) { ret += ", " + to_string(*it); } return ret + "]"; }
template <typename A> std::string to_string(const std::unordered_set <A>& st) { if (st.empty()) { return "[empty]"; } auto it = st.begin(); std::string ret = "[" + to_string(*it); it++; for (; it != st.end(); it++) { ret += ", " + to_string(*it); } return ret + "]"; }
template <typename A, typename B> std::string to_string(const std::map <A, B>& mp) { if (mp.empty()) { return "[empty]"; } auto it = mp.begin(); std::string ret = "[(" + to_string((*it).first) + ": " + to_string((*it).second) + ")"; it++; for (; it != mp.end(); it++) { ret += ", (" + to_string((*it).first) + ": " + to_string((*it).second) + ")"; } return ret + "]"; }
template <typename A, typename B, typename C> std::string to_string(const std::map <A, B, C>& mp) { if (mp.empty()) { return "[empty]"; } auto it = mp.begin(); std::string ret = "[(" + to_string((*it).first) + ": " + to_string((*it).second) + ")"; it++; for (; it != mp.end(); it++) { ret += ", (" + to_string((*it).first) + ": " + to_string((*it).second) + ")"; } return ret + "]"; }
template <typename A, typename B> std::string to_string(const std::unordered_map <A, B>& mp) { if (mp.empty()) { return "[empty]"; } auto it = mp.begin(); std::string ret = "[(" + to_string((*it).first) + ": " + to_string((*it).second) + ")"; it++; for (; it != mp.end(); it++) { ret += ", (" + to_string((*it).first) + ": " + to_string((*it).second) + ")"; } return ret + "]"; }
template <typename A> std::string to_string(const std::queue <A>& q) { if (q.empty()) { return "[empty]"; } std::queue <A> bckp = q; std::string ret = "[" + to_string(bckp.front()); bckp.pop(); while (!bckp.empty()) { A tp = bckp.front(); bckp.pop(); ret += ", " + to_string(tp); } return ret + "]"; }
template <typename A> std::string to_string(const std::priority_queue <A>& pq) { if (pq.empty()) { return "[empty]"; } std::priority_queue <A> bckp = pq; std::string ret = "[" + to_string(bckp.top()); bckp.pop(); while (!bckp.empty()) { A tp = bckp.top(); bckp.pop(); ret += ", " + to_string(tp); } return ret + "]"; }
template <typename A, typename B> std::string to_string(const std::priority_queue <A, std::vector <A>, B>& pq) { if (pq.empty()) { return "[empty]"; } std::priority_queue <A, std::vector <A>, B> bckp = pq; std::string ret = "[" + to_string(bckp.top()); bckp.pop(); while (!bckp.empty()) { A tp = bckp.top(); bckp.pop(); ret += ", " + to_string(tp); } return ret + "]"; }
template <typename A> std::string to_string(const std::stack <A>& stck) { if (stck.empty()) { return "[empty]"; } std::stack <A> bckp = stck; std::string ret = "[" + to_string(bckp.top()); bckp.pop(); while (!bckp.empty()) { A tp = bckp.top(); bckp.pop(); ret += ", " + to_string(tp); } return ret + "]"; }
template <typename A> std::string to_string(const std::deque <A>& dq) { if (dq.empty()) { return "[empty]"; } std::deque <A> bckp = dq; std::string ret = "[" + to_string(bckp.front()); bckp.pop_front(); while (!bckp.empty()) { A tp = bckp.front(); bckp.pop_front(); ret += ", " + to_string(tp); } return ret + "]"; }

void debug_out() { std::cerr << std::endl; }
template <typename H, typename... T> void debug_out(H h, T... t) { std::cout << " " << to_string(h); debug_out(t...); }

#ifndef ONLINE_JUDGE
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void solv(const int curt);

const bool isT = true;

int main() {
	std::ios::sync_with_stdio(0); std::cin.tie(0);
	
	int t = 1; if (isT) { std::cin >> t; }
	for (int i = 1; i <= t; i++) { solv(i); }
	
	return 0;
}

void solv(const int curt) {
	
	int n, m; std::cin >> n >> m;
	int ans = 0;
	std::vector <int> a(n);
	for (int& x : a) std::cin >> x;
	for (int i = 0; i < m; i++) {
		int p; std::cin >> p;
		for (int j = 0; j < n; j++) {
			if (a[j] == p) {
				ans++;
				break;
			}
		}
	}
	debug(ans);
	std::cout << ans << "\n";
	
}
