#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define endl '\n'
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;

const int N = 2e5 + 100;
const ll INF = 0x3f3f3f3f3f3f3f3f;

pair<int, int> a[N];

void solve() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++ i) {
		int x;
		scanf("%d", &x);
		a[i].first = x, a[i].second = i;
	}
	sort(a + 1, a + 1 + n);
	int xb1 = a[1].second, xb2 = a[n].second;
	int ans = min(max(xb1, xb2), n - min(xb1, xb2) + 1);
	ans = min(ans, min(xb1, xb2) + n - max(xb1, xb2) + 1);
	printf("%d\n", ans);
}

int main(){
	int t;
	scanf("%d", &t);
	while(t --) {
		solve();
	}
    return 0;
}
/*
3 3
3 3
*/


// 3 5   2
// 6 2   1 - 3
// 6 5	  2 - 3	
// 1 6	  1 - 2 
// 1 7	  1
// 1 1	  0 

