#include #include using namespace std; const int INF = INT_MAX / 2; void chmax(int& a, int b){ if(a < b) a = b; } using T = array, 2>; constexpr T e(){ return T{0, -INF, -INF, 0}; } constexpr T none(T, T){ return e(); } T op(T a, T b){ T ans = e(); for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) chmax(ans[i][k], a[i][j] + b[j][k]); return ans; } void solve(){ int n; cin >> n; atcoder::lazy_segtree seg(n - 1); for(int i = 0; i < n; i++){ int p; cin >> p; p--; seg.apply(0, p, T{0, 1, -INF, 0}); seg.apply(p, n - 1, T{0, -INF, 1, 0}); } int ans = 0; for(int i = 0; i < n - 1; i++){ const T x = seg.get(i); for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) chmax(ans, x[i][j]); } cout << ans << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t; cin >> t; while(t--) solve(); }