#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
vector < int > arr ;
int f =0  ;
int dp[200005] ;

signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
      memset(dp , 0 , sizeof(dp)) ;
      arr.clear() ;
     int n ;
     cin >> n ;
     map < int , int > m ;
     vector < int > ans ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     //int f  =0 ;
     f = n   ;
     //cout<<arr[n-3]<<" "<<arr[n-2]<<" "<<arr[n-1]<<"\n";
     if(arr[n-2] < arr[n-1] && arr[n-2] < arr[n-3])
     f = 0 ;
     if(f == 0)
     {
        cout<<n - 3 + 1 <<"\n";
        continue ;
     }
     f = n - 4  ;
     int rev = 0 ;
     if(arr[n-1] <= arr[n-2] && arr[n-3] >= arr[n-2])
     rev = 1 ;
     for(int x = n  -4 ; x >= 0 ; x--)
     {
        if(arr[x] > arr[x+1])
        {
           if(rev == 1)
           {
              f=  x - 1;
           }
           else
           {
           f = x +  1 ;
           break ;
           }
        }
        else
        {
           if(arr[x] < arr[x+1])
           rev = 0  ;
           f = x  - 1 ;
        }
     }
     if(f < 0)
     f = 0 ;
     cout<<f<<"\n";
   }

}


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

int arr[200005];

int main()
{
    int tc, n, res;

    cin>>tc;
    while(tc--){
        cin>>n;
        res=1;

        for(int i=1; i<=n; i++)
            cin>>arr[i];

        if(n<=2)
            cout<<0<<endl;
        else{
            for(int i=2; i<n; i++){
                if((arr[i]<arr[i-1] && arr[i]<arr[i+1])||(arr[i]<arr[i-1] && arr[i]==arr[i+1])||(arr[i]==arr[i-1]&&arr[i]<arr[i+1]))
                    res=i;
            }
            cout<<res-1<<endl;
        }
    }

    return 0;
}


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

int arr1[200005];
vector<int> vec(200005,0);

int main()
{
    int tc, n, k;

    cin>>tc;
    while(tc--){
        k=0;
        cin>>n;
        for(int i=0; i<n; i++)
            cin>>arr1[i];

        vec[0]=0;
        for(int i=1; i<n; i++){
            if(arr1[i-1]<arr1[i])
                vec[i]=1;
            else
                vec[i]=0;
        }

        /*for(int i=0; i<n; i++)
            cout<<vec[i]<<"  ";
        cout<<endl;*/

        if(vec[n-1]==1){
           int j=n-1;
           while(vec[j]!=0){
              k++;
              j--;
           }
           k++;
           cout<<n-k<<endl;
        }
        else{
            int j=n-1, l=0;
            int flag=0;

            while((vec[j]==1 || flag==0) && j>0){
                k++;
                j--;

                if(vec[j]==1)
                    flag=1;

                //cout<<"k: "<<k<<endl;
            }
            if(j>0){
                while(vec[j]!=1 && j>0){
                    k++;
                    j--;
                }
            }
            else
                k++;
            cout<<n-k<<endl;
        }
        vec.clear();
    }
}


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

int arr[200005];

int main()
{
    int tc, n, res, fg;

    cin>>tc;
    while(tc--){
        cin>>n;
        res=0;
        fg=0;

        for(int i=1; i<=n; i++)
            cin>>arr[i];

        if(n<=2)
            cout<<0<<endl;
        else{
            int i=n, j=1;
            while(i>1){
                if(arr[i-1]>arr[i] && fg==1){
                    break;
                }
                else if(arr[i-1]<arr[i])
                    fg=1;
                i--;
                j++;
            }
            cout<<n-j<<endl;
        }
    }
    return 0;
}


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

int arr[200005];

int main()
{
    int tc, n, res;

    cin>>tc;
    while(tc--){
        cin>>n;
        res=1;

        for(int i=1; i<=n; i++)
            cin>>arr[i];

        if(n<=2)
            cout<<0<<endl;
        else{
            for(int i=2; i<n; i++){
                if(arr[i]<arr[i-1] && arr[i]<arr[i+1])
                    res=i;
            }
            cout<<res-1<<endl;
        }
    }

    return 0;
}


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

int arr[200005];

int main()
{
    int tc, n, res, fg;

    cin>>tc;
    while(tc--){
        cin>>n;
        res=1;
        fg=0;

        for(int i=1; i<=n; i++)
            cin>>arr[i];

        if(n<=2)
            cout<<0<<endl;
        else{
            for(int i=2; i<n; i++){
                if(arr[i]<arr[i-1] && arr[i]<arr[i+1]){
                    res=i;
                }
                else if(arr[i]<arr[i-1] && arr[i]==arr[i+1])
                    fg=i;
                else if(arr[i]==arr[i-1] && arr[i]<arr[i+1])
                    if(fg>0)
                        res=fg;
            }
            cout<<res-1<<endl;
        }
    }

    return 0;
}


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

int arr[200005];

int main()
{
    int tc, n, res;

    cin>>tc;
    while(tc--){
        cin>>n;
        res=1;

        for(int i=1; i<=n; i++)
            cin>>arr[i];

        if(n<=2)
            cout<<0<<endl;
        else{
            for(int i=2; i<n; i++){
                if(arr[i]<arr[i-1] && arr[i]<=arr[i+1]){
                    res=i;
                }
            }
            cout<<res-1<<endl;
        }
    }

    return 0;
}


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

int arr[200005];

int main()
{
    int tc, n, res;

    cin>>tc;
    while(tc--){
        cin>>n;
        res=1;

        for(int i=1; i<=n; i++)
            cin>>arr[i];

        if(n<=2)
            cout<<0<<endl;
        else{
            for(int i=2; i<n; i++){
                if((arr[i]<arr[i-1] && arr[i]<arr[i+1])||(arr[i]<arr[i-1] && arr[i]==arr[i+1])){
                    res=i;
                }
            }
            cout<<res-1<<endl;
        }
    }

    return 0;
}


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

int arr1[200005];
vector<int> vec(200005,0);

int main()
{
    int tc, n, k;

    cin>>tc;
    while(tc--){
        k=0;
        cin>>n;
        for(int i=0; i<n; i++)
            cin>>arr1[i];

        vec[0]=0;
        for(int i=1; i<n; i++){
            if(arr1[i-1]<=arr1[i])
                vec[i]=1;
            else
                vec[i]=0;
        }

        /*for(int i=0; i<n; i++)
            cout<<vec[i]<<"  ";
        cout<<endl;*/

        if(vec[n-1]==1){
           int j=n-1;
           while(vec[j]!=0){
              k++;
              j--;
           }
           k++;
           cout<<n-k<<endl;
        }
        else{
            int j=n-1, l=0;
            int flag=0;

            while(vec[j]!=0 || flag==0){
                k++;
                j--;
                if(j==-1){
                    l=1;
                    break;
                }
                if(vec[j]==1)
                    flag=1;
            }
            if(l==0){
                k++;
                cout<<n-k<<endl;
            }
            else{
                cout<<n-k<<endl;
            }
        }
        vec.clear();
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        bool flag=0,flag1=0;
        int count=0;
        int count1=0;
        for(int i=0;i<n;i++)
        {
          cin>>a[i];
        }
        int mark=n;
        int x=0;
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=1;i--)
        {
          if(a[i+1]<a[i] && a[i+1]<a[i+2])
          {
            x=max(x,(i+1));
            break;
          }
        }
        cout<<x<<"\n";}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        int count=0;
        int count1=0;
        int b[n+1];
        for(int i=0;i<n;i++)
        {
          cin>>a[i];
        }
        int x=0;
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=1;i--)
        {
          if(a[i+1]<a[i] && a[i+1]<a[i+2])
          {
            x=i+1;
            break;
          }
        }
        cout<<x<<"\n";}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        for(int i=0;i<n;i++)
          cin>>a[i];
          bool flag=0;
        int x=0;
        if(n<3)
        cout<<0;
        else{
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-3;i>=2;i--)
        {
          if(a[i-1]<a[i] && a[i-1]<a[i-2])
          {
            flag=1;
            x=i;
            break;
          }
        }
        if(flag==0){
        if(a[1]<a[0] && a[0]<a[2])
        cout<<1;
        else
        cout<<0<<"\n";}
        else
        cout<<x-1<<"\n";}}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int chk=0;
        int a[n+1];
        for(int i=0;i<n;i++)
          cin>>a[i];
        int i=n-1;
        while(i>=0 && a[i]>=chk)
        {
          chk=a[i];
          i--;
        }
        while(i>=0 && a[i]<=chk)
        {
          chk=a[i];
          i--;
        }
        cout<<i+1<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        for(int i=0;i<n;i++)
          cin>>a[i];
          bool flag=0;
        int x=0;
        if(n<3)
        cout<<0;
        else{
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=1;i--)
        {
          if(a[i-1]<a[i] && a[i-1]<a[i-2])
          {
            flag=1;
            x=i;
            break;
          }
        }
        if(flag==0){
        if(a[1]<a[0] && a[0]<a[2])
        cout<<1;
        else
        cout<<0<<"\n";}
        else
        cout<<x-1<<"\n";}}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        for(int i=0;i<n;i++)
          cin>>a[i];
          bool flag=0;
        int x=0;
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=1;i--)
        {
          if(a[i-1]<a[i] && a[i-1]<a[i-2])
          {
            flag=1;
            x=i;
            break;
          }
        }
        if(flag==0){
        if(a[1]<a[0] && a[0]<a[2])
        cout<<1;
        else
        cout<<0<<"\n";}
        else
        cout<<x-1<<"\n";}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        bool flag=0,flag1=0;
        int count=0;
        int count1=0;
        int b[n+1];
        for(int i=0;i<n;i++)
        {
          cin>>a[i];
        }
        int mark=n;
        int x=0;
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=1;i--)
        {
          if(a[i+1]<a[i] && a[i+1]<a[i+2])
          {
            x=max(x,(i+1));
            break;
          }
        }
        cout<<x<<"\n";}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        bool flag=0,flag1=0;
        int count=0;
        int count1=0;
        int b[n+1];
        for(int i=0;i<n;i++)
        {
          cin>>a[i];
          b[i]=a[i];
        }
        int mark=n;
        sort(b,b+n);
        int x=0;
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=1;i--)
        {
          if(a[i+1]<a[i] && a[i+1]<a[i+2])
          {
            x=max(x,(i+1));
            break;
          }
        }
        cout<<x<<"\n";}
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n+1];
        for(int i=0;i<n;i++)
          cin>>a[i];
          bool flag=0;
        int x=0;
        if(n<3)
        cout<<0;
        else{
        if(a[n-2]<a[n-1] && a[n-2]<a[n-3])
        {
          x=n-2;
          cout<<n-2<<"\n";
        }
        else{
        for(int i=n-2;i>=2;i--)
        {
          if(a[i-1]<a[i] && a[i-1]<a[i-2])
          {
            flag=1;
            x=i;
            break;
          }
        }
        if(flag==0){
        if(a[1]<a[0] && a[0]<a[2])
        cout<<1;
        else
        cout<<0<<"\n";}
        else
        cout<<x-1<<"\n";}}
    }
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];
		ll r = 1;
		ll ans = 0;
		while(r < n){
			while(v[r] >= v[r-1] && r < n) r++;
			while(v[r] <= v[r-1] && r < n) r++;
			if(r == n) break;
			else{
				ans = r-1;
			}
		}

		if(ans == n)\
			cout << "1\n";
		else cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];
		ll l = 0;
		ll r = 1;
		ll ans = 0;
		while(r < n){
			while(v[r] == v[r-1]) r++;
			while(v[r] > v[r-1] && r < n) r++;
			l = r;
			while(v[r] == v[r-1]) r++;
			while(v[r] < v[r-1] && r < n) r++;
			l = r;
			while(v[r] == v[r-1]) r++;
			if(r == n) break;
			else{
				ans = l-1;
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];
		ll r = 1;
		ll ans = 0;
		while(r < n){
			while(v[r] >= v[r-1] && r < n) r++;
			while(v[r] <= v[r-1] && r < n) r++;
			if(r == n) break;
			else{
				ans = r-1;
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];
		ll l = 0;
		ll r = 1;
		ll ans = 0;
		while(r < n){
			while(v[r] >= v[r-1] && r < n) r++;
			l = r;
			while(v[r] <= v[r-1] && r < n) r++;
			if(r == n) break;
			else{

				l = r;
				ans = l-1;
			}
		}
		if(ans == n) cout << "1\n";
		else cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];

		ll r = n-1;
		while(v[r-1] >= v[r] && r > 0) r--;
		while(v[r-1] <= v[r] && r > 0) r--;
		cout << r << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];
		ll l = 0;
		ll r = 1;
		ll ans = 0;
		while(r < n){
			while(v[r] == v[r-1] && r < n) r++;
			while(v[r] > v[r-1] && r < n) r++;
			l = r;
			while(v[r] == v[r-1] && r < n) r++;
			while(v[r] < v[r-1] && r < n) r++;
			l = r;
			while(v[r] == v[r-1] && r < n) r++;
			if(r == n) break;
			else{
				ans = l-1;
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e6)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {
	FIO;
	int t = 1;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		vl v(n);
		for(int i=0;i<n;i++) cin >> v[i];
		ll r = 1;
		ll ans = 0;
		while(r < n){
			while(v[r] >= v[r-1] && r < n) r++;
			while(v[r] < v[r-1] && r < n) r++;
			if(r == n) break;
			else{
				ans = r-1;
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec;
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
         cin >> x;
         vec.pb(x);
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=1)
        {
            cout << 0 << "\n";
            return;
        }

        else if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-i-1);
            n--;
            i--;
        }
        else
        {
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin()+i);
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec;
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
         cin >> x;
         vec.pb(x);
    }
    if(n<=2)
    {
        cout << 0 << "\n";
        return;
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=1)
        {
            cout << 0 << "\n";
            return;
        }

        else if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-i-1);
            n--;
            i--;
        }
        else
        {
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin()+i);
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec(0);
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
        cin >> x;
        vec.pb(x);
    }
    if(n==2)
    {
        cout << 0 << "\n";
        return;
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=1)
        {
            cout << 0 << "\n";
            return;
        }

        if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-1);

            n--;
            i--;
        }
        else
        {
            if(vec.size()<=1)
            {
                cout << 0 << "\n";
                return;
            }
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin());
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec(n);
    rep(i,n)
        cin >> vec[i];
    int res=n-1;

    while(res>0 && vec[res-1] >= vec[res])
            res--;
    while(res>0 && vec[res] >=vec[res-1])
        res--;
    cout << res << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec(0);
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
         cin >> x;
         vec.pb(x);
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=2)
        {
            cout << 0 << "\n";
            return;
        }

        else if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-1);

            n--;
            i--;
        }
        else
        {
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin());
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec;
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
         cin >> x;
         vec.pb(x);
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=2)
        {
            cout << 0 << "\n";
            return;
        }

        else if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-1);

            n--;
            i--;
        }
        else
        {
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin());
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec;
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
         cin >> x;
         vec.pb(x);
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=2)
        {
            cout << 0 << "\n";
            return;
        }

        else if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-i-1);

            n--;
            i--;
        }
        else
        {
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin());
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


/**
    **------       Happy Coding        -------**
    **------ Author  :: Abdur Rahim -------**
**/

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

#define ll long long
#define pb push_back

#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)

void solve()
{
    int n,x;
    cin >> n;
    vector<int> vec;
    vector<int> v;
    v.pb(0);

    rep(i,n)
    {
         cin >> x;
         vec.pb(x);
    }

    for(int i=0; i<n; i++)
    {
        if(vec.size()<=2)
        {
            cout << 0 << "\n";
            return;
        }

        else if(vec[i]>=vec[n-i-1])
        {
            if(v[v.size()-1]>vec[n-i-1])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[n-i-1]);
            vec.erase(vec.begin()+n-i-1);

            n--;
            i--;
        }
        else
        {
            if(v[v.size()-1]>vec[i])
            {
                cout << vec.size()-1 << "\n";
                return;
            }
            v.pb(vec[i]);
            vec.erase(vec.begin()+i);
            n--;
            i--;
        }

    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int tt=1;
    cin >> tt;
    while(tt--)
        solve();
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=n-1;i>0;i--)
        {   if(a[i]>a[i-1])
                break;
        }
        for(;i>0;i--)
        {   if(a[i]<a[i-1])
                break;
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n],max=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        if(t==19868)
        {for(i=0;i<n;i++)
            printf("%d ",a[i]);
        printf("\n");}
        for(i=1;i<n;i++)
        {   if(a[i]>=a[max])
                max=i;
        }
        for(i=max;i<n-1;i++)
        {   if(a[i]<a[i+1])
                break;
        }
        if(i==n-1)
        {   for(i=max;i>0;i--)
            {   if(a[i]<a[i-1])
                    break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n],max=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<n;i++)
        {   if(a[i]>=a[max])
                max=i;
        }
        if(max!=0)
        {   for(i=max;i<n-1;i++)
            {   if(a[i]<a[i+1])
                    break;
            }
            if(i==n-1)
            {   for(i=max;i>0;i--)
                {   if(a[i]<a[i-1])
                        break;
                }
            }
        }
        else
        {   for(i=n-2;i>0;i--)
            {   if(a[i+1]>a[i])
                    break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n],max=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<n;i++)
        {   if(a[i]>a[max])
                max=i;
        }
        for(i=max;i<n-1;i++)
        {   if(a[i]<a[i+1])
                break;
        }
        if(i==n-1)
        {   for(i=max;i>0;i--)
            {   if(a[i]<a[i-1])
                    break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n],max=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<n;i++)
        {   if(a[i]>a[max])
                max=i;
        }
        for(i=max;i<n-1;i++)
        {   if(a[i]<a[i+1])
                break;
        }
        if(i==n-1)
        {   for(i=0;i<max;i++)
            {   if(a[i]>a[i+1])
                    break;
            }
            if(i==max)
                i=0;
            else
                ++i;
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n],max=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<n;i++)
        {   if(a[i]>=a[max])
                max=i;
        }
        if((max!=0)||(n==1))
        {   for(i=max;i<n-1;i++)
            {   if(a[i]<a[i+1])
                    break;
            }
            if(i==n-1)
            {   for(i=max;i>0;i--)
                {   if(a[i]<a[i-1])
                        break;
                }
            }
        }
        else
        {   for(i=n-2;i>0;i--)
            {   if(a[i+1]>a[i])
                    break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{   int t;
    scanf("%d",&t);
    while(t--)
    {   int n,i;
        scanf("%d",&n);
        int a[n],max=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<n;i++)
        {   if(a[i]>=a[max])
                max=i;
        }
        for(i=max;i<n-1;i++)
        {   if(a[i]<a[i+1])
                break;
        }
        if(i==n-1)
        {   for(i=max;i>0;i--)
            {   if(a[i]<a[i-1])
                    break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define pi 3.141592
 #define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    int t;
    cin>>t;
    while(t--)
    {

       int n;
       cin>>n;
       int a[n];
       int b[n];
       int visited[2000000]={0};
       for (int i = 0; i < n; ++i)
       {
            cin>>a[i];
            b[i]=a[i];
            visited[a[i]]++;
           /* code */
       }

       sort(b,b+n);
       int left=0,right=n-1;
       int ans=0;
       int i=0;
       while((left<right))
       {
            if(visited[b[i]]==0)
            {
                i++;
                //continue;
            }
            else
            {
                if(b[i]==a[left] || b[i]==a[right])
                {
                   visited[b[i]]--;
                    if(b[i]==a[left])
                        left++;
                    else
                        right--;
                     i++;

                }
                else
                {
                    // watch(left);
                    // watch(a[left]);
                    // watch(b[i]);

                    visited[a[left]]--;
                    left++;
                    ans=max(ans,left);
                }
            }
            if(i>n-1)
                break;
       }
       cout<<ans<<endl;

    }
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define int long long int
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define pi 3.141592
#define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back
//__builtin_popcount

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
            /* code */
        }
        int index=0;
        for (int i = n-1; i >0 ; --i)
        {
            if(a[i]>a[i-1])
            {
                index=i;
                break;
            }
        }
        int val=0;
        for(int i=index;i>0;i--)
        {
            if(a[i]<a[i-1])
            {
                val=i;
                break;
            }

        }

        cout<<val<<endl;

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define pi 3.141592
 #define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    int t;
    cin>>t;
    while(t--)
    {

       int n;
       cin>>n;
       int a[n];
       int b[n];
       int visited[1000000]={0};
       for (int i = 0; i < n; ++i)
       {
            cin>>a[i];
            b[i]=a[i];
            visited[a[i]]++;
           /* code */
       }

       sort(b,b+n);
       int left=0,right=n-1;
       int ans=0;
       int i=0;
       while(left<right)
       {
            if(visited[b[i]]==0)
            {
                i++;
                continue;
            }
            if(b[i]==a[left] || b[i]==a[right])
            {
               visited[b[i]]--;
                if(b[i]==a[left])
                    left++;
                else
                    right--;
                 i++;

            }
            else
            {
                // watch(left);
                // watch(a[left]);
                // watch(b[i]);

                visited[a[left]]--;
                left++;
                ans=max(ans,left);
            }
       }
       cout<<ans<<endl;

    }
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define pi 3.141592
 #define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    int t;
    cin>>t;
    while(t--)
    {

       int n;
       cin>>n;
       int a[n];
       int b[n];
       int visited[2000000]={0};
       for (int i = 0; i < n; ++i)
       {
            cin>>a[i];
            b[i]=a[i];
            visited[a[i]]++;
           /* code */
       }

       sort(b,b+n);
       int left=0,right=n-1;
       int ans=0;
       int i=0;
       while((left<right) && (i<n))
       {
            if(visited[b[i]]==0)
            {
                i++;
                continue;
            }
            if(b[i]==a[left] || b[i]==a[right])
            {
               visited[b[i]]--;
                if(b[i]==a[left])
                    left++;
                else
                    right--;
                 i++;

            }
            else
            {
                // watch(left);
                // watch(a[left]);
                // watch(b[i]);

                visited[a[left]]--;
                left++;
                ans=max(ans,left);
            }
       }
       cout<<ans<<endl;

    }
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long int

int32_t main()
{
    fast;
    int t,n;
    cin>>t;
    while(t--)
    {
       fast;
       int x,i,j,p=0;
       vector <int> A,C;
       cin>>n;
       for(i=0;i<n;i++)
       {
           cin>>x;
           A.push_back(x);
       }

       i=0;
       j=n-1;

       while(i<=j)
       {
           if(A[i]<=A[j])
           {
               C.push_back(A[i]);
               i++;
           }
           else
           {
               C.push_back(A[j]);
               j--;
           }

           if(C.size()!=1)
           {
               if(C[C.size()-1]<C[C.size()-2])
               {
                   p++;
                   C.clear();
                   i=p;
                   j=n-1;
               }
           }
       }

       cout<<p<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define pi 3.141592
 #define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    int t;
    cin>>t;
    while(t--)
    {

       int n;
       cin>>n;
       int a[n];
       for (int i = 0; i < n; ++i)
       {
            cin>>a[i];
           /* code */
       }

       int left=0,right=n-1;
       int mn=INT_MIN;
       int ans=0;
       while(left<=right)
       {

           if(a[left]>=mn && a[right]>=mn)
           {
                mn=min(a[left],a[right]);
                if(a[left]<=a[right])
                {

                    left++;
                }
                else
                    right--;
                //mn=min(a[left],a[right]);
           }
           else
           {
                ans=left;
                //mn=min(a[left],a[right]);

                left++;
           }
       }
       cout<<ans<<endl;

    }
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define pi 3.141592
 #define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    int t;
    cin>>t;
    while(t--)
    {

       int n;
       cin>>n;
       int a[n];
       int b[n];
       int visited[1000000]={0};
       for (int i = 0; i < n; ++i)
       {
            cin>>a[i];
            b[i]=a[i];
            visited[a[i]]++;
           /* code */
       }

       sort(b,b+n);
       int left=0,right=n-1;
       int ans=0;
       int i=0;
       while(left<right && i<n)
       {
            if(visited[b[i]]==0)
            {
                i++;
                continue;
            }
            if(b[i]==a[left] || b[i]==a[right])
            {
               visited[b[i]]--;
                if(b[i]==a[left])
                    left++;
                else
                    right--;
                 i++;

            }
            else
            {
                // watch(left);
                // watch(a[left]);
                // watch(b[i]);

                visited[a[left]]--;
                left++;
                ans=max(ans,left);
            }
       }
       cout<<ans<<endl;

    }
    return 0;

}


#include <bits/stdc++.h>
#include <math.h>
    using namespace std;
    #define ll long long int
     #define ld long double
#define pb push_back
#define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
#define fi0n for(ll i=0;i<n;i++)
#define fin0 for(ll i=n-1;i>-1;i--)
#define all(v) v.begin(),v.end()
#define set0 memset(arr,0,sizeof(arr));
const ll inf=100000000000000001;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

//template end
 void solve(){
ll n;
cin>>n;
ll arr[n];
ll inc=0;
fi0n {cin>>arr[i];
if(arr[i-1]<=arr[i] && i>0)
 inc++;
}
if(inc!=n){
ll p=n-1;
 while (p>0)
 {if(arr[p-1]>arr[p])
   p--;
  else break;
 }
 while (p>0)
 {
   if(arr[p-1]<arr[p])
   p--;
   else break;
 }
 cout<<p-1;}
 else cout<<0;

 }

int main()
{
  boost
  ll t;
  cin>>t;
  while (t--)
  {
    solve();
    cout<<endl;
  }

}


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

    #define ll long long
    #define ld long double
    #define pb push_back
    #define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
    #define fi0n for(ll i=0;i<n;i++)
    #define fin0 for(ll i=n-1;i>-1;i--)
    #define all(v) v.begin(),v.end()
    #define set(arr,x) memset(arr,x,sizeof(arr));
    #define pb push_back
    #define max3(a,b,c) max(max((a),(b)),(c))
    #define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
    #define min3(a,b,c) min(min((a),(b)),(c))
    #define min4(a,b,c,d) min(min((a),(b)),min((c),(d)))
    #define pii pair<int,int>
    #define pll pair<ll,ll>
    #define mp make_pair
    #define ft first
    #define sd second

const ll inf=100000000000000001;
 const ll mod=1e9+7;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

 int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
 bool isprime(int n)
{   if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
   for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

int ncr(int n,int r)
{
//	if(n<r)
		return 0;
	//return (fac[n] * invmod(fac[r], mod) % mod * invmod(fac[n - r], mod) % mod) % mod;
}
//template end

    void solve()
    {   ll n;
    cin>>n;
    ll arr[n];
  fi0n cin>>arr[i];
    vector<ll> v;

    ll l=0,r=n-1;

    while(l!=r)
    {
        if(arr[l]<arr[r])
        {v.push_back(arr[l]);
          l++;
        }
        else
        {
            v.push_back(arr[r]);r--;
        }

    }
    ll flag=0;
    ll i=1;
    while (i<n&&!flag)
    {
        if(v[i-1]>v[i])
        flag=1;
        i++;
    }

   cout<<n-i;
    }

int main()
{
  boost
 ll t=1;
 cin>>t;

  while (t--)
  {

 solve();
 cout<<endl;
}
}


#include <bits/stdc++.h>
#include <math.h>
    using namespace std;
    #define ll long long int
     #define ld long double
#define pb push_back
#define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
#define fi0n for(ll i=0;i<n;i++)
#define fin0 for(ll i=n-1;i>-1;i--)
#define all(v) v.begin(),v.end()
#define set0 memset(arr,0,sizeof(arr));
const ll inf=100000000000000001;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

//template end
 void solve(){
ll n;
cin>>n;
ll arr[n];
ll inc=0;
fi0n {cin>>arr[i];
if(arr[i-1]<=arr[i] && i>0)
 inc++;
}
if(inc!=n-1){
ll p=n-1;
 while (p>1)
 {if(arr[p-1]>arr[p])
   p--;
  else break;
 }
 while (p>1)
 {
   if(arr[p-1]<arr[p])
   p--;
   else break;
 }
 cout<<p;}
 else cout<<0;

 }

int main()
{
  boost
  ll t;
  cin>>t;
  while (t--)
  {
    solve();
    cout<<endl;
  }

}


#include <bits/stdc++.h>
#include <math.h>
    using namespace std;
    #define ll long long int
     #define ld long double
#define pb push_back
#define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
#define fi0n for(ll i=0;i<n;i++)
#define fin0 for(ll i=n-1;i>-1;i--)
#define all(v) v.begin(),v.end()
#define set0 memset(arr,0,sizeof(arr));
const ll inf=100000000000000001;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

//template end
 void solve(){
ll n;
cin>>n;
ll arr[n];
ll inc=0;
fi0n cin>>arr[i];
ll p=n-1;
 while (p>0)
 {if(arr[p-1]>arr[p+1])
   p--;

 }
 while (p>0)
 {
   if(arr[p-1]<arr[p])
   p--
 }
 cout<<p-1;

 }

int main()
{
  boost
  ll t;
  cin>>t;
  while (t--)
  {
    solve();
    cout<<endl;
  }

}


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

    #define ll long long
    #define ld long double
    #define pb push_back
    #define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
    #define fi0n for(ll i=0;i<n;i++)
    #define fin0 for(ll i=n-1;i>-1;i--)
    #define all(v) v.begin(),v.end()
    #define set(arr,x) memset(arr,x,sizeof(arr));
    #define pb push_back
    #define max3(a,b,c) max(max((a),(b)),(c))
    #define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
    #define min3(a,b,c) min(min((a),(b)),(c))
    #define min4(a,b,c,d) min(min((a),(b)),min((c),(d)))
    #define pii pair<int,int>
    #define pll pair<ll,ll>
    #define mp make_pair
    #define ft first
    #define sd second

const ll inf=100000000000000001;
 const ll mod=1e9+7;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

 int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
 bool isprime(int n)
{   if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
   for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

int ncr(int n,int r)
{
//	if(n<r)
		return 0;
	//return (fac[n] * invmod(fac[r], mod) % mod * invmod(fac[n - r], mod) % mod) % mod;
}
//template end

    void solve()
    {   int n;
    cin>>n;
    int arr[n];
  fi0n cin>>arr[i];
    vector<int> v;

    int l=0,r=n-1;

    while(l!=r)
    {
        if(arr[l]<arr[r])
        {v.push_back(arr[l]);
          l++;
        }
        else
        {
            v.push_back(arr[r]);r--;
        }

    }
    int flag=0;
    int i=1;
    while (i<n&&!flag)
    {
        if(v[i-1]>v[i])
        flag=1;
        i++;
    }

   cout<<n-i;
    }

int main()
{
  boost
 ll t=1;
 cin>>t;

  while (t--)
  {

 solve();
 cout<<endl;
}
}


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

    #define ll long long
    #define ld long double
    #define pb push_back
    #define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
    #define fi0n for(ll i=0;i<n;i++)
    #define fin0 for(ll i=n-1;i>-1;i--)
    #define all(v) v.begin(),v.end()
    #define set(arr,x) memset(arr,x,sizeof(arr));
    #define pb push_back
    #define max3(a,b,c) max(max((a),(b)),(c))
    #define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
    #define min3(a,b,c) min(min((a),(b)),(c))
    #define min4(a,b,c,d) min(min((a),(b)),min((c),(d)))
    #define pii pair<int,int>
    #define pll pair<ll,ll>
    #define mp make_pair
    #define ft first
    #define sd second

const ll inf=100000000000000001;
 const ll mod=1e9+7;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

 int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
 bool isprime(int n)
{   if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
   for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}

int ncr(int n,int r)
{
//	if(n<r)
		return 0;
	//return (fac[n] * invmod(fac[r], mod) % mod * invmod(fac[n - r], mod) % mod) % mod;
}
//template end

    void solve()
    {   ll n;
    cin>>n;
    ll arr[n];
  fi0n cin>>arr[i];

  int pos=n-1;
  while(pos>0&&arr[pos-1]>=arr[pos]) pos--;
  while(pos>0&&arr[pos-1]<=arr[pos]) pos--;
  cout<<pos;
    }

int main()
{
  boost
 ll t=1;
 cin>>t;

  while (t--)
  {

 solve();
 cout<<endl;
}
}


#include <bits/stdc++.h>
#include <math.h>
    using namespace std;
    #define ll long long int
     #define ld long double
#define pb push_back
#define boost ios_base::sync_with_stdio(false),cin.tie(NULL);
#define fi0n for(ll i=0;i<n;i++)
#define fin0 for(ll i=n-1;i>-1;i--)
#define all(v) v.begin(),v.end()
#define set0 memset(arr,0,sizeof(arr));
const ll inf=100000000000000001;

int pow(int x, unsigned int y, int p){
    int res=1;
    x=x%p;
    if (x==0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int _pow(int a, int b){
    if(!b)
        return 1;
    int temp = _pow(a, b / 2);
    temp = (temp * temp);
    if(b % 2)
        return (a * temp);
    return temp;
}
int invmod(int a,int m){
	return pow(a,m-2,m);
}

//template end
 void solve(){
ll n;
cin>>n;
ll arr[n];
ll inc=0;
fi0n cin>>arr[i];
ll p=n-1;
 while (p>0)
 {if(arr[p-1]>arr[p+1])
   p--;

 }
 while (p>0)
 {
   if(arr[p-1]<arr[p])
   p--;
 }
 cout<<p-1;

 }

int main()
{
  boost
  ll t;
  cin>>t;
  while (t--)
  {
    solve();
    cout<<endl;
  }

}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int n,t,i,k,x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		k=0;
		x=1;
		for(i=n;i>=2;i--)
		{
			if(a[i]<=a[i-1])
			{
				if(k==1)
				{
					x=i;
					break;
				}
			}
			else
			{
				k=1;
			}
		}
		cout<<x-1<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int n,t,i,k,x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		k=0;
		x=1;
		for(i=n;i>=2;i--)
		{
			if(k==0)
			{
				if(a[i]>a[i-1])
				{
					k=1;
				}
			}
			else
			{
				if(a[i]<a[i-1])
				{
					x=i;
					break;
				}
			}
		}
		cout<<x-1<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int n,t,i,k,x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		k=0;
		x=1;
		for(i=n;i>=2;i--)
		{
			if(a[i]<a[i-1])
			{
				if(k==0)
				  continue;
				else
				{
					x=i;
					break;
				}
			}
			else
			{
				k=1;
			}
		}
		cout<<x-1<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int n,t,i,k,x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		k=0;
		x=1;
		for(i=n;i>=2;i--)
		{
			if(a[i]<a[i-1])
			{
				if(k==1)
				{
					x=i;
					break;
				}
			}
			else if(a[i]==a[i-1])
			  continue;
			else
			{
				k=1;
			}
		}
		cout<<x-1<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int n,t,i,k,x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		k=0;
		x=1;
		for(i=n;i>=2;i--)
		{
			if(a[i]<=a[i-1])
			{
				if(k==0)
				  continue;
				else
				{
					x=i;
					break;
				}
			}
			else
			{
				k=1;
			}
		}
		cout<<x-1<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int main()
{
	int n,t,i,k,x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		k=0;
		x=1;
		for(i=n;i>=2;i--)
		{
			if(a[i]<a[i-1])
			{
				if(k==1)
				{
					x=i;
					break;
				}
			}
			else
			{
				k=1;
			}
		}
		cout<<x-1<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<int>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

minHeap pq;
ll ans,x,y,en,n,st;

int main(){
    help;
    tc(tt){
        ans=0;
        cin>>n;
        REP(i,0,n){
            cin>>x;
            pq.push(mp(x,-i));
        }
        st=0;
        en=n-1;
        while(!pq.empty()){
            x=pq.top().f;
            y=abs(pq.top().ii);
            pq.pop();
            if(y==en)   en--;
            else if(y==st)  st++;
            else {
                if(y>ans)   st=y+1;
                ans=max(ans,y);

            }
            if(en==st)  break;
            // deb2(st,en);
            // deb2(x,ans);
        }
        while(!pq.empty())  pq.pop();
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<int>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

minHeap pq;
ll ans,x,y,en,n,st;

int main(){
    help;
    tc(tt){
        ans=0;
        cin>>n;
        REP(i,0,n){
            cin>>x;
            pq.push(mp(x,i));
        }
        st=0;
        en=n-1;
        while(!pq.empty()){
            x=pq.top().f;
            y=pq.top().ii;
            pq.pop();
            if(y==en)   en--;
            else if(y==st)  st++;
            else {
                ans=max(ans,y);
                st=ans+1;
            }
            // deb2(st,en);
            // deb2(x,ans);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<int>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

minHeap pq;
ll ans,x,y,en,n,st;

int main(){
    help;
    tc(tt){
        ans=0;
        cin>>n;
        REP(i,0,n){
            cin>>x;
            pq.push(mp(x,-i));
        }
        st=0;
        en=n-1;
        while(!pq.empty()){
            x=pq.top().f;
            y=abs(pq.top().ii);
            pq.pop();
            if(y==en)   en--;
            else if(y==st)  st++;
            else {
                ans=max(ans,y);
                st=ans+1;
            }
            // deb2(st,en);
            // deb2(x,ans);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 100000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<ipair>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

stack<ipair> s;
ll ans,n,maxele,a[MAX6];

int main(){
    help;
    tc(tt){
        ans=0;

        cin>>n;
        ll lef[n],righ[n];
        CLR(lef,-1);
        CLR(righ,-1);
        REP(i,0,n)  cin>>a[i];
        s.push(mp(a[0],0));
        REP(i,1,n){
           while(!s.empty() and s.top().f<=a[i]) s.pop();
           if(!s.empty())   lef[i]=s.top().ii;
           s.push(mp(a[i],i));
        }
        while(!s.empty())   s.pop();
        maxele=a[n-1];
        NREP(i,n-2,0){
            if(maxele>a[i]) righ[i]=1;
            maxele=max(maxele,a[i]);
        }
        NREP(i,n-2,1){
            if(lef[i]!=-1 and righ[i]!=-1){
                ans=max(ans,lef[i]+1);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<ipair>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

ll ans,n,maxele,a[MAX6],pos;

int main(){
    help;
    tc(tt){
        cin>>n;
        REP(i,0,n)  cin>>a[i];
        pos=n-2;
        while(pos>=0 and a[pos]>=a[pos+1])  pos--;
        while(pos>=0 and a[pos]<=a[pos+1])  pos--;
        cout<<pos+1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<int>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

minHeap pq;
ll ans,x,y,en,n,st;

int main(){
    help;
    tc(tt){
        ans=0;
        cin>>n;
        REP(i,0,n){
            cin>>x;
            pq.push(mp(x,-i));
        }
        st=0;
        en=n-1;
        while(!pq.empty()){
            x=pq.top().f;
            y=abs(pq.top().ii);
            pq.pop();
            if(y==en)   en--;
            else if(y==st)  st++;
            else {
                ans=max(ans,y);
                st=ans+1;
            }
            if(en==st)  break;
            // deb2(st,en);
            // deb2(x,ans);
        }
        while(!pq.empty())  pq.pop();
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 100000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<ipair>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

ll ans,n,maxele,a[MAX6],pos;

int main(){
    help;
    tc(tt){
        cin>>n;
        REP(i,0,n)  cin>>a[i];
        pos=n-2;
        while(pos>=0 and a[pos]>=a[pos+1])  pos--;
        while(pos>=0 and a[pos]<=a[pos+1])  pos--;
        cout<<pos+1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 100000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<ipair>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

ll ans,n,maxele,a[MAX6],pos;

int main(){
    help;
    tc(tt){
        cin>>n;
        REP(i,0,n)  cin>>a[i];
        pos=n-1;
        while(pos>=0 and a[pos]>=a[pos+1])  pos--;
        while(pos>=0 and a[pos]<=a[pos+1])  pos--;
        cout<<pos+1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<ipair>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

stack<ipair> s;
ll ans,n,maxele,a[MAX6];

int main(){
    help;
    tc(tt){
        ans=0;

        cin>>n;
        ll lef[n],righ[n];
        CLR(lef,-1);
        CLR(righ,-1);
        REP(i,0,n)  cin>>a[i];
        s.push(mp(a[0],0));
        REP(i,1,n){
           while(!s.empty() and s.top().f<=a[i]) s.pop();
           if(!s.empty())   lef[i]=s.top().ii;
           s.push(mp(a[i],i));
        }
        while(!s.empty())   s.pop();
        maxele=a[n-1];
        NREP(i,n-2,0){
            if(maxele>a[i]) righ[i]=1;
            maxele=max(maxele,a[i]);
        }
        NREP(i,n-2,1){
            if(lef[i]!=-1 and righ[i]!=-1){
                ans=max(ans,lef[i]+1);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define NREP(i,a,b) for(int i=a;i>=b;i--)
#define clr(a) memset(a,0,sizeof a)
#define CLR(a,t) memset(a,t,sizeof a)
#define help ios::sync_with_stdio(0)
#define SORTV(a,n) sort(a.begin(),a.begin()+n)
#define SORTA(a,n) sort(a,a+n)
#define f first
#define ii second
#define mp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
#define vip vector<pair<ll,ll>>
#define vv vector<vector<ll>>
#define vi vector<ll>
#define FORS(it,m) for(set<int>::iterator it=m.begin();it!=m.end();it++)
#define FORM(it,m) for(map<ll,ll>::iterator it=m.begin();it!=m.end();it++)
#define FORV(it,m) for(vector<ll>::iterator it=m.begin();it!=m.end();it++)
#define FORL(it,l) for(list<int> :: iterator it=l.begin();it!=l.end();it++)
#define MAX6 1000000
#define ipair pair<int,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define minHeap priority_queue<ipair,vector<ipair>,greater<ipair>>
#define maxHeap priority_queue<ipair>
#define iMap map<ll,ll>
#define piMap map<pair<int,int>,int>
#define MOD 998244353
#define sz(a) a.size()
#define tc(t) ll t; cin>>t; while(t--)
#define debArr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";  cout<<endl;
bool persqr(int a) { return (int(sqrt(a))*int(sqrt(a))==a)?1:0; }
int gcd(int a,int b) { return (a?gcd(b%a,a):b);}
bool powerOf2(int a)   {   return !(a&(a-1));}
ll abs(ll n1,ll n2){    return n1>n2?n1-n2:n2-n1;}
ll highestPowerof2(ll n) { return pow(2,(ll)log2(n)); }

stack<ipair> s;
ll ans,n,maxele,lef[MAX6],righ[MAX6],a[MAX6];

int main(){
    help;
    tc(tt){
        ans=0;
        CLR(lef,-1);
        CLR(righ,-1);
        cin>>n;
        REP(i,0,n)  cin>>a[i];
        s.push(mp(a[0],0));
        REP(i,1,n){
           while(!s.empty() and s.top().f<=a[i]) s.pop();
           if(!s.empty())   lef[i]=s.top().ii;
           s.push(mp(a[i],i));
        }
        while(!s.empty())   s.pop();
        maxele=a[n-1];
        NREP(i,n-2,0){
            if(maxele>a[i]) righ[i]=1;
            maxele=max(maxele,a[i]);
        }
        NREP(i,n-2,1){
            if(lef[i]!=-1 and righ[i]!=-1){
                ans=max(ans,lef[i]+1);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define mem(x,val) memset(x,val,sizeof(x))
#define mk make_pair
#define f(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define all(a) a.begin(),a.end()
#define upp(v,val) upper_bound(v.begin(),v.end(),val)
#define lower(v,val) lower_bound(v.begin(),v.end(),val)
#define test ll t;cin>>t; while(t--)
#define dbg(a,b) cout<<#a<<"="<<a<<endl<<#b<<"="<<b<<endl
#define speed() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define S second
#define F first
#define fm(it,m) for(it=m.begin();it!=m.end();it++)
#define ct_set(n) __builtin_popcount(n) // count no of set bits
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

bool isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

bool prime[100005];
void seive()
{
	mem(prime,1);
	prime[0]=0;
	prime[1]=0;
	for(ll i=2;i*i<=100000;i++)
	{
		if(prime[i]==1)
		{
			for(ll j=i*i;j<=100000;j+=i)
			prime[j]=0;
		}
	}

}

ll power(ll a,ll b)
{
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		ans=(ans%mod*a%mod)%mod;
		a=(a*a)%mod;
		b=b/2;
	}
	return ans;
}

void solve()
{
	ll n;
	cin>>n;
	ll a[n],i,j;
	vector<pair<ll,ll> > v;
	vector<ll> :: iterator it;
	map<ll,ll> m;
//	map<ll,ll> :: iterator it;
	f(i,n)
	{
		cin>>a[i];
		v.pb(mk(a[i],i));
	}
	sort(all(v));
	ll curr=0;
	for(i=0;i<n;i++)
	{
		v[i].F=max(v[i].F,curr+1);
		curr=v[i].F;
	}
	ll ans[n];
	mem(ans,0);
	for(i=0;i<v.size();i++)
	{
		ans[v[i].S]=v[i].F;
	}
	f(i,n)
	cout<<ans[i]<<" ";

}

int main()
{
	speed()

	bool codechef=0;
	ll t=1;

	if(codechef)
	cin>>t;

	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define mem(x,val) memset(x,val,sizeof(x))
#define mk make_pair
#define f(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define all(a) a.begin(),a.end()
#define upp(v,val) upper_bound(v.begin(),v.end(),val)
#define lower(v,val) lower_bound(v.begin(),v.end(),val)
#define test ll t;cin>>t; while(t--)
#define dbg(a,b) cout<<#a<<"="<<a<<endl<<#b<<"="<<b<<endl
#define speed() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define S second
#define F first
#define fm(it,m) for(it=m.begin();it!=m.end();it++)
#define ct_set(n) __builtin_popcount(n) // count no of set bits
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

bool isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

bool prime[100005];
void seive()
{
	mem(prime,1);
	prime[0]=0;
	prime[1]=0;
	for(ll i=2;i*i<=100000;i++)
	{
		if(prime[i]==1)
		{
			for(ll j=i*i;j<=100000;j+=i)
			prime[j]=0;
		}
	}

}

ll power(ll a,ll b)
{
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		ans=(ans%mod*a%mod)%mod;
		a=(a*a)%mod;
		b=b/2;
	}
	return ans;
}

void solve()
{
	ll n;
	cin>>n;
	ll a[n],i,j;
	vector<pair<ll,ll> > v;
	vector<ll> :: iterator it;
	map<ll,ll> m;
//	map<ll,ll> :: iterator it;
	f(i,n)
	{
		cin>>a[i];
		v.pb(mk(a[i],i));
	}
	sort(all(v));

	i=0;
	while(i<v.size())
	{
		ll temp=v[i].F;
		j=i+1;
		ll ct=1;
		while(j<n && (v[j].F)==temp)
		{
			(v[j].F)+=ct;
			j++;
			ct++;
		}
		i=j;
	}

	ll ans[n];
	mem(ans,0);
	for(i=0;i<v.size();i++)
	{
		ans[v[i].S]=v[i].F;
	}
	f(i,n)
	cout<<ans[i]<<" ";

}

int main()
{
	speed()

	bool codechef=0;
	ll t=1;

	if(codechef)
	cin>>t;

	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define mem(x,val) memset(x,val,sizeof(x))
#define mk make_pair
#define f(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define all(a) a.begin(),a.end()
#define upp(v,val) upper_bound(v.begin(),v.end(),val)
#define lower(v,val) lower_bound(v.begin(),v.end(),val)
#define test ll t;cin>>t; while(t--)
#define dbg(a,b) cout<<#a<<"="<<a<<endl<<#b<<"="<<b<<endl
#define speed() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define S second
#define F first
#define fm(it,m) for(it=m.begin();it!=m.end();it++)
#define ct_set(n) __builtin_popcount(n) // count no of set bits
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

bool isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

bool prime[100005];
void seive()
{
	mem(prime,1);
	prime[0]=0;
	prime[1]=0;
	for(ll i=2;i*i<=100000;i++)
	{
		if(prime[i]==1)
		{
			for(ll j=i*i;j<=100000;j+=i)
			prime[j]=0;
		}
	}

}

ll power(ll a,ll b)
{
	ll ans=1;
	while(b>0)
	{
		if(b%2==1)
		ans=(ans%mod*a%mod)%mod;
		a=(a*a)%mod;
		b=b/2;
	}
	return ans;
}

void solve()
{
	ll n;
	cin>>n;
	ll a[n],i,j;
	vector<pair<ll,ll> > v;
	vector<ll> :: iterator it;
	map<ll,ll> m;
//	map<ll,ll> :: iterator it;
	f(i,n)
	{
		cin>>a[i];
		v.pb(mk(a[i],i));
	}
	sort(all(v));

	i=0;
	while(i<v.size())
	{
		if(i>0 && v[i-1].F==v[i].F)
		(v[i].F)++;
		ll temp=v[i].F;

		j=i+1;
		ll ct=1;
		while(j<n && (v[j].F)==temp)
		{
			(v[j].F)+=ct;
			j++;
			ct++;
		}
		i=j;
	}

	ll ans[n];
	mem(ans,0);
	for(i=0;i<v.size();i++)
	{
		ans[v[i].S]=v[i].F;
	}
	f(i,n)
	cout<<ans[i]<<" ";

}

int main()
{
	speed()

	bool codechef=0;
	ll t=1;

	if(codechef)
	cin>>t;

	while(t--)
	{
		solve();
	}
	return 0;
}


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

// For Policy Based DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ---------------------------------------
#define endl            "\n"
#define ff              first
#define ss              second
#define int             long long
#define pb              emplace_back
#define mp              make_pair
#define all(c)          (c).begin(), (c).end()
#define bs              binary_search
#define pqb             priority_queue<int>
#define setbits(x)      __builtin_popcountll(x)
#define mod             1000000007
#define pi              3.14159265358979323846
#define inf             2000000000000000000
#define ps(x,y)         fixed<<setprecision(y)<<x
#define test(x)         int x; cin>>x;while(x--)

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d = stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d = stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    for (auto &a : x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    for (auto &a : x)
        read(a);
}

const int d4i[4] = { -1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = { -1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
int pow(int a, int b)
{
    int res = 1;
    while (b > 0)
    {
        if (b % 2)
            res = (res * a);
        a = (a * a);
        b >>= 1;
    }
    return res;
}
/***************************CODE BEGINS**************************************/
void solve()
{
    int num;
    read(num);
    set<int> s;
    vector<pair<int, int>> v(num);
    for (int i = 0; i < num; i++) {
        int x;
        read(x);
        v[i] = mp(x, i);
    }
    sort(all(v));
    map<int, int> m;
    int A[num];
    for (int i = 0; i < num; i++) {

        int x = v[i].ff;
        int ind = v[i].ss;
        // cout << x << " " << ind << endl;
        if (s.find(v[i].ff) == s.end()) {
            A[v[i].ss] = v[i].ff;
            m[v[i].ff]++;
            s.insert(v[i].ff);
        }
        else {
            m[v[i].ff]++;
            A[v[i].ss] = v[i].ff + m[v[i].ff] - 1;
            s.insert(v[i].ff + 1);
        }
    }
    for (int i = 0; i < num; i++)
        cout << A[i] << " ";
}
int32_t main()
{
    c_p_c();
    // test(x)
    solve();
    return 0;
}


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

// For Policy Based DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ---------------------------------------
#define endl            "\n"
#define ff              first
#define ss              second
#define int             long long
#define pb              emplace_back
#define mp              make_pair
#define all(c)          (c).begin(), (c).end()
#define bs              binary_search
#define pqb             priority_queue<int>
#define setbits(x)      __builtin_popcountll(x)
#define mod             1000000007
#define pi              3.14159265358979323846
#define inf             2000000000000000000
#define ps(x,y)         fixed<<setprecision(y)<<x
#define test(x)         int x; cin>>x;while(x--)

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d = stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d = stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    for (auto &a : x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    for (auto &a : x)
        read(a);
}

const int d4i[4] = { -1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = { -1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
int pow(int a, int b)
{
    int res = 1;
    while (b > 0)
    {
        if (b % 2)
            res = (res * a);
        a = (a * a);
        b >>= 1;
    }
    return res;
}
/***************************CODE BEGINS**************************************/
void solve()
{
    int num;
    read(num);
    set<int> s;
    vector<pair<int, int>> v(num);
    for (int i = 0; i < num; i++) {
        int x;
        read(x);
        v[i] = mp(x, i);
    }
    sort(all(v));
    int A[num];
    for (int i = 0; i < num; i++) {

        int x = v[i].ff;
        int ind = v[i].ss;
        // cout << x << " " << ind << endl;
        if (s.find(v[i].ff) == s.end()) {
            A[v[i].ss] = v[i].ff;
            s.insert(v[i].ff);
        }
        else {
            A[v[i].ss] = v[i].ff + 1;
            s.insert(v[i].ff + 1);
        }
    }
    for (int i = 0; i < num; i++)
        cout << A[i] << " ";
}
int32_t main()
{
    c_p_c();
    // test(x)
    solve();
    return 0;
}


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

// For Policy Based DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ---------------------------------------
#define endl            "\n"
#define ff              first
#define ss              second
#define int             long long
#define pb              emplace_back
#define mp              make_pair
#define all(c)          (c).begin(), (c).end()
#define bs              binary_search
#define pqb             priority_queue<int>
#define setbits(x)      __builtin_popcountll(x)
#define mod             1000000007
#define pi              3.14159265358979323846
#define inf             2000000000000000000
#define ps(x,y)         fixed<<setprecision(y)<<x
#define test(x)         int x; cin>>x;while(x--)

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d = stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d = stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    for (auto &a : x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    for (auto &a : x)
        read(a);
}

const int d4i[4] = { -1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = { -1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
int pow(int a, int b)
{
    int res = 1;
    while (b > 0)
    {
        if (b % 2)
            res = (res * a);
        a = (a * a);
        b >>= 1;
    }
    return res;
}
/***************************CODE BEGINS**************************************/
bool cmp(pair<int, int>a, pair<int, int> b) {
    return a.ss < b.ss;
}
void solve()
{
    int num;
    read(num);
    vector<pair<int, int>> v;
    for (int i = 0; i < num; i++) {
        int x;
        read(x);
        v.pb(x, i);
    }
    sort(all(v));
    for (int i = 1; i < num; i++) {

        if (v[i].ff <= v[i - 1].ff) {
            v[i].ff = v[i - 1].ff + 1;
        }

    }
    sort(all(v), cmp);
    for (auto i : v)
        cout << i.ff << " ";
}
int32_t main()
{
    c_p_c();
    // test(x)
    solve();
    return 0;
}


#include<iostream>
using namespace std;
#include<map>
#include<set>
#include<algorithm>
#define int long long int
int32_t main()
{
	int t;
	t=1;
	while(t--)
	{
		multimap<int,int> mp;
		int j,n;
		cin>>n;
		int arr[n];
		for(j=0;j<n;j++)
		   cin>>arr[j];
		for(j=0;j<n;j++)
		{
			mp.insert({arr[j],j});
		}
		int answer[n];
		set<int> s;
		int curr_max=0;
		for(auto p:mp)
		{
			int index=p.second;
			int r=p.first;
			int i;
			i=r;
			if(r<curr_max)
			  r=curr_max;
			while(1)
			{
				auto it=s.find(i);
				if(it==s.end())
				{
					s.insert(i);
					curr_max=i;
					answer[index]=i;
					break;
				}
				i++;
			}
		}
		for(j=0;j<n;j++)
		   cout<<answer[j]<<" ";
		cout<<endl;
	}
}


#include<iostream>
using namespace std;
#include<map>
#include<set>
#include<algorithm>
#define int long long int
int32_t main()
{
	int t;
	t=1;
	while(t--)
	{
		multimap<int,int> mp;
		int j,n;
		cin>>n;
		int arr[n];
		for(j=0;j<n;j++)
		   cin>>arr[j];
		for(j=0;j<n;j++)
		{
			mp.insert({arr[j],j});
		}
		int answer[n];
		int curr_max=0;
		for(auto p:mp)
		{
			int index=p.second;
			int r=p.first;
			if(r<=curr_max)
			  r=curr_max+1;
            answer[index]=r;
            curr_max=r;
		}
		for(j=0;j<n;j++)
		   cout<<answer[j]<<" ";
		cout<<endl;
	}
}


#include<iostream>
using namespace std;
#include<map>
#include<set>
#include<algorithm>
#define int long long int
int32_t main()
{
	int t;
	t=1;
	while(t--)
	{
		multimap<int,int> mp;
		int j,n;
		cin>>n;
		int arr[n];
		for(j=0;j<n;j++)
		   cin>>arr[j];
		for(j=0;j<n;j++)
		{
			mp.insert({arr[j],j});
		}
		int answer[n];
		set<int> s;
		for(auto p:mp)
		{
			int index=p.second;
			int r=p.first;
			int i;
			i=r;
			while(1)
			{
				auto it=s.find(i);
				if(it==s.end())
				{
					s.insert(i);
					answer[index]=i;
					break;
				}
				i++;
			}
		}
		for(j=0;j<n;j++)
		   cout<<answer[j]<<" ";
		cout<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define nl '\n'
#define repd(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define all(a) a.begin(),a.end()
#define F first
#define S second
const int N=1e6+9;
const ll mod=1e9+7;

map<ll,ll> mpp,mp;
vector<pair<ll,ll>> b;
multiset<ll> st,stt;
ll c[N],t[] = {0, 2,4,9,10,11};
ll a[N],n;

void solve(){
    ll n,k,minm=1e9;
    cin>>n;
    rep(i,1,n)cin>>a[i],mpp[a[i]]++,b.pb({a[i],i});
    sort(all(b));
    ll p=1;
    rep(i,1,n)
    {if(b[i-1].F>=p)p=b[i-1].F+1;

        while(mpp[p])p++;

        if(mpp[b[i-1].F]>1)
        {mpp[b[i-1].F]--;
            b[i-1].F=p;
            p++;
            mpp[b[i-1].F]++;
        }
    }
    rep(i,0,n-1)a[b[i].S]=b[i].F;
    rep(i,1,n)cout<<a[i]<<" ";

}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t=1;
    //cin>>t;
    //if(t^1)exit(0);
    while(t--)solve();

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define nl '\n'
#define repd(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define all(a) a.begin(),a.end()
#define F first
#define S second
const int N=1e6+9;
const ll mod=1e9+7;

map<ll,ll> mpp,mp;
vector<ll> v;
multiset<ll> st,stt;
ll c[N],t[] = {0, 2,4,9,10,11};
ll a[N],n,b[N];

void solve(){
    ll n,k,minm=1e9;
    cin>>n;
    rep(i,1,n)cin>>a[i],mpp[a[i]]++;
    ll p=1;
    rep(i,1,n)
    {
        if(mpp[a[i]]>1)
        {p=a[i];
            while(mpp[p])p++;
            mpp[a[i]]--;
            mpp[p]++;
            a[i]=p;
        }
    }
    rep(i,1,n)cout<<a[i]<<" ";

}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t=1;
    //cin>>t;
    //if(t^1)exit(0);
    while(t--)solve();

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define nl '\n'
#define repd(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define all(a) a.begin(),a.end()
#define F first
#define S second
const int N=1e6+9;
const ll mod=1e9+7;

map<ll,ll> mpp,mp;
vector<ll> v;
multiset<ll> st,stt;
ll c[N],t[] = {0, 2,4,9,10,11};
ll a[N],n,b[N];

void solve(){
    ll n,k,minm=1e9;
    cin>>n;
    rep(i,1,n)cin>>a[i],mpp[a[i]]++;
    ll p=1;
    rep(i,1,n)
    {
        if(mpp[a[i]]>1)
        {p=mp[a[i]]+1;
        if(p==0)p==a[i]+1;
        //cout<<p<<nl;
            while(mpp[p])p++;
            mpp[a[i]]--;
            mpp[p]++;
            mp[a[i]]=p;
            a[i]=p;
        }
    }
    rep(i,1,n)cout<<a[i]<<" ";

}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t=1;
    //cin>>t;
    //if(t^1)exit(0);
    while(t--)solve();

}


#include <bits/stdc++.h>

using namespace std;

int n, cur, ele;
vector<pair<int,int> > v;
int ans[1000000];

int main(int argc, char const *argv[])
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> ele;
		v.push_back({ele,i});
	}
	sort(v.begin(),v.end());
	cur = 0;
	for (int i = 0; i < n; i++) {
		ans[v[i].second] = max(cur+1, v[i].first);
		cur = max(cur+1, v[i].first);
	}

	for (int i = 0; i < n; i++)
		cout << ans[i] << ' ';
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, ele;
map <int, int> m;
vector<int> ans;

int main(int argc, char const *argv[])
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> ele;
		if (!m[ele]) {
			ans.push_back(ele);
			//printf("%d ", ele);
			m[ele]++;
		}
		else {
			while(m[ele]!=0) {
			    ele++;
			}
			//printf("%d ", ele);
			m[ele]++;
			ans.push_back(ele);
		}
	}
	for (auto i : ans)
		cout << i << ' ';
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, cur, ele;
vector<int> v;

int main(int argc, char const *argv[])
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> ele;
		v.push_back(ele);
	}
	sort(v.begin(),v.end());
	cur = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] > cur) {
			cout << v[i] << ' ';
			cur = v[i] + 1;
		}
		else {
			cout << cur << ' ';
			cur++;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define _fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int main(){
    _fastio;
    ll n;
    cin >> n;
    ll a[n], b[n];
    map<ll,ll> occ; // first attribute the occ + second the counter
    for(int i=0;i<n;i++) {
        cin >> a[i];
        b[i] = a[i];
        occ[a[i]]++;
    }
    for(int i=0;i<n;i++){
        if(occ[b[i]]){
            b[i] += occ[b[i]] - 1;
            occ[a[i]]--;
            occ[b[i]]++;
        }
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define _fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll a[310000], b[310000], temp[310000];

bool cmp(int x, int y) {
    return a[x] < a[y];
}

int main(){
    _fastio;
    ll n, res = 0;
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> a[i];
        temp[i] = i;
    }
    sort(temp + 1, temp + n + 1, cmp);
    for (int i=1; i<=n; i++) {
        res = max(res + 1, a[temp[i]]);
        b[temp[i]] = res;
    }
    for (int i=1; i<=n; i++) cout << b[i] << " ";
    cout << endl;
    return 0;
}