#include<stdio.h>
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,d;
		scanf("%d",&n);
		int a[n];
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		int max=-9999,min=999999;
		int p,s;
		for(i=0;i<n;i++)
		{
			if(a[i]>max)
			{
				max=a[i];
				p=i;
			}
			if(a[i]<min)
			{
				min=a[i];
				s=i;
			}
		}
		if(p>=(n/2)&&s>=(n/2))
		{
			if(p<s)
				printf("%d\n",n-p);
			else
				printf("%d\n",n-s);
		}
		else if(p<(n/2)&&s<(n/2))
		{
			if(p>s)
				printf("%d\n",p+1);
			else
				printf("%d\n",s+1);
		}
		else
		{
			if(p>s){
				d=p-s;
				int r1=(s+1)+d;
				int r2=(n-p)+d;
				int r3=(s+1)+(n-p);
				if(r1<r2&&r1<r3)
					printf("%d\n",r1);
				else if(r2<r3)
					printf("%d\n",r2);
				else
					printf("%d\n",r3);
			}	
			else
			{
				d=s-p;
				int r1=(p+1)+d;
				int r2=(n-s)+d;
				int r3=(p+1)+(n-s);
				if(r1<r2&&r1<r3)
					printf("%d\n",r1);
				else if(r2<r3)
					printf("%d\n",r2);
				else
					printf("%d\n",r3);
			}
			
			
		}
	}
}

