#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } void solve() { int n; cin >> n; vector a(n), b(n); rep(i, 0, n) cin >> a[i]; rep(i, 0, n) cin >> b[i]; rep(i, 0, n) if (a[i] < b[i]) swap(a[i], b[i]); vector ab(n * 2); rep(i, 0, n) { ab[i * 2] = a[i]; ab[i * 2 + 1] = b[i]; } vector v1, v2; auto add_val = [&](int x, vector& v) -> int { auto itr = lower_bound(all(v), x); if (itr == v.end()) { v.push_back(x); return -1; } int res = *itr; *itr = x; return res; }; rep(i, 0, n * 2) { int x = add_val(ab[i], v1); if (x != -1) add_val(x, v2); } cout << v1.size() + v2.size() << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int t = 1; cin >> t; while (t--) solve(); }