#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin >>T;
    while(T--){
     int n,maxx=INT_MIN,minn=INT_MAX,ans,answe,answ,H,L,a,b,g,an;
     cin >> n;
     int A[n];
     for(int i=0;i<n;i++){
         cin>>A[i];
     }
        for(int i=0;i<n;i++){
            if(maxx<A[i]){
            maxx=A[i];
            H=i+1;
            }
            if(minn>A[i]){
                minn=A[i];
                L=i+1;
            }
        }
            a = max(H,L)-min(H,L);
            ans = min(H+a,L+a);
            b   = min(n-L+H+1,n-H+L+1);
            g = min(n-L+a+1,n-H+a+1);
            answ=min(ans,H+L);
            answe=min(answ,b);
            an = min(answe,g);
        
        cout << an <<endl;
        
    }
    return 0;
}