#include<bits/stdc++.h> #define int long long usingnamespace std; int a[2][200005], sp[2][200005], sm[2][200005]; bool vis[2][200005]; signedmain() { int n, _; scanf("%lld", &_); while (_--) { scanf("%lld", &n); for (int o = 0; o < 2; o++) { for (int i = 1; i <= n; i++) scanf("%lld", &a[o][i]); } a[0][1] = -1; for (int o = 0; o < 2; o++) { sp[o][n] = a[o][n] + n; for (int i = n - 1; i; i--) sp[o][i] = max(a[o][i] + i, sp[o][i + 1]); } for (int o = 0; o < 2; o++) { sm[o][n] = a[o][n] - n; for (int i = n - 1; i; i--) sm[o][i] = max(a[o][i] - i, sm[o][i + 1]); } int res = 1e18, now = 0, x = 0, y = 1; for (int o = 0; o < 2; o++) { for (int i = 1; i <= n; i++) vis[o][i] = 0; } vis[0][1] = 1; for (int sum = 1;; sum++) { if (sum == (n << 1)) { res = min(res, now); break; } int cur = now + (n - y); if (y < n) cur = max(cur, sm[x][y] + 1 + n);
cur = max(cur + 1, a[x ^ 1][n] + 1);
int t = y; if (vis[x ^ 1][y]) t++;
cur = max(cur + n - t, sp[x ^ 1][t] + 1 - t); res = min(cur, res);
if (!vis[x ^ 1][y]) x ^= 1; else y++; vis[x][y] = 1; now = max(now + 1, a[x][y] + 1); } printf("%lld\n", res); } return0; }