#include<bits/stdc++.h>
#define test int t;cin>>t;while(t--)
#define all(x)((x).begin()),((x).end())
#define len(x)(x.length())
#define ll long long
#define pb push_back
using namespace std;
int main()
{
    test
    {
        int n;
        cin>>n;
        vector<int>v(n);
        for(int &i:v)cin>>i;
        int straight,reverse,L,R;
        int mn=*min_element(all(v)),mx=*max_element(all(v));
        bool firstLeft=true,firstRight=true;
        for(int i=0,j=n-1; i<n; i++,j--)
        {
            if(v[i]==mn||v[i]==mx)
            {
                straight=i+1;
                if(firstRight)
                {
                    R=straight;
                    firstRight=false;
                }
            }
            if(v[j]==mn||v[j]==mx)
            {
                reverse=n-j;
                if(firstLeft)
                {
                    L=reverse;
                    firstLeft=false;
                }
            }
        }
        cout<<min(min(L+R,reverse),straight)<<endl;
    }
}
