#include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef unsigned long long ULL; const int MaxN = 1e6; int T, n, m; int a[MaxN + 5], b[MaxN + 5], loc1[MaxN + 5], loc2[MaxN + 5]; int main() { scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); loc1[a[i]] = loc1[a[i] - 1] + 1; } int ans = 0; for(int i = 1; i <= m; i++) { scanf("%d", &b[i]); loc2[b[i]] = loc2[b[i] - 1] + 1; ans = max(ans, min(loc1[b[i]], loc2[b[i]])); } printf("%d\n", ans); for(int i = 1; i <= n; i++) loc1[a[i]] = 0; for(int i = 1; i <= m; i++) loc2[b[i]] = 0; } }