#include #include #include #include #include using namespace std; const int MaxN = 1e6; int n, m, T; int a[MaxN + 5], b[MaxN + 5]; int IA[MaxN + 5], IB[MaxN + 5]; int main() { scanf("%d", &T); for (int cas = 1; cas <= T; cas++) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); IA[a[i]] = IA[a[i] + 1] = 0; IB[a[i]] = IB[a[i] + 1] = 0; } for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); IA[b[i]] = IA[b[i] + 1] = 0; IB[b[i]] = IB[b[i] + 1] = 0; } for (int i = n; i >= 1; i--) IA[a[i]] = max(IA[a[i]], IA[a[i] + 1] + 1); for (int i = m; i >= 1; i--) IB[b[i]] = max(IB[b[i]], IB[b[i] + 1] + 1); int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, min(IA[a[i]], IB[a[i]])); printf("%d\n", ans); } }