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

#define ll long long int
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
    for ( \
        auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
        blockTime.second; \
        debug("%s: %d ms\n", d, (int)chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false \
    )

void solve()
{
	int n;
	cin >> n;
	vector<int> v(n);
	for(auto &x:v)
	{
		cin >> x;
	}

	int maxpos = max_element(v.begin(),v.end())-v.begin();
	int minpos = min_element(v.begin(),v.end())-v.begin();

	if(minpos>maxpos)
	{
		int temp = minpos;
		minpos=maxpos;
		maxpos=temp;
	}
	int ans;
	ans = minpos+1+(n-maxpos);
	ans = min(ans,maxpos+1);
	ans = min(ans,n-minpos);
	cout<<ans<<"\n";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	cin >> t;
	time__("this time"){
		while(t--)
		{

			solve();
		}
	}
	
	
	return 0;
}

