#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
using namespace std;

#define file "o"
#define ff(i, a, b) for(auto i=(a); i<=(b); ++i)
#define ffr(i, b, a) for(auto i=(b); i>=(a); --i)
#define nl "\n"
#define ss " "
#define pb emplace_back
#define fi first
#define se second
#define sz(s) (int)s.size()
#define all(s) (s).begin(), (s).end()
#define ms(a,x) memset(a, x, sizeof (a))
#define cn continue
#define re exit(0)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll ran(ll l, ll r)
{
    return uniform_int_distribution<ll> (l, r)(rng);
}

inline void rf()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    if(fopen(file".inp","r"))
    {
        freopen(file".inp","r",stdin);
        freopen(file".out","w",stdout);
    }
}

const int mod=998244353;
const int maxn=3e5+15;
const ll inf=1e18;

template<typename T> inline void add(T &x, const T &y)
{
    x+=y;
    if(x>=mod) x-=mod;
    if(x<0) x+=mod;
}

template<typename T> inline bool maxi(T &a, T b)
{
    if(a>=b) return 0;
    a=b; return 1;
}

template<typename T> inline bool mini(T &a, T b)
{
    if(a<=b) return 0;
    a=b; return 1;
}

int n, a[maxn], pmin[maxn], pmax[maxn], smin[maxn], smax[maxn];
bool nxt[maxn];

void solve()
{
    cin>>n;
    ff(i, 1, n) cin>>a[i];
    pmin[0]=mod; pmax[0]=-mod; smin[0]=mod; smax[0]=-mod;
    pmin[n+1]=mod; pmax[n+1]=-mod; smin[n+1]=mod; smax[n+1]=-mod;
    ff(i, 1, n) pmin[i]=min(pmin[i-1], a[i]), pmax[i]=max(pmax[i-1], a[i]);
    ffr(i, n, 1) smin[i]=min(smin[i+1], a[i]), smax[i]=max(smax[i+1], a[i]);
    
    vi q; q.pb(1);
    int ans=0;
    
    ff(k, 1, n)
    {
        vi nq;
        for(int l : q)
        {
            int r=n-k+l-1, low=mod, high=-mod;
            if(l>1) low=pmin[l-1], high=pmax[l-1];
            if(r<n) mini(low, smin[r+1]), maxi(high, smax[r+1]);
            
            if(a[l]<low || a[l]>high)
            {
                if(!nxt[l+1]) { nxt[l+1]=1; nq.pb(l+1); }
            }
            if(l<r && (a[r]<low || a[r]>high))
            {
                if(!nxt[l]) { nxt[l]=1; nq.pb(l); }
            }
        }
        if(nq.empty()) break;
        ans=k;
        q = move(nq);
        for(int l : q) nxt[l]=0; 
    }
    cout<<ans<<nl;
}

signed main()
{
    rf();
    int tt; cin>>tt;
    while(tt--) solve();
    re;
}