#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sll;
typedef set<string> sstr;
typedef map<int, int> mi;
typedef map<long long, long long> mll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef string str;
#define INF 1000000000
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define fastio                                                                 \
    ios_base::sync_with_stdio(0);                                              \
    cin.tie(0);                                                                \
    cout.tie(0)

template <typename T> istream &operator>>(istream &is, vector<T> &a_input) {
    for (int i = 0; i < a_input.size(); ++i)
        is >> a_input[i];
    return is;
}

std::mt19937
    generator(std::chrono::steady_clock::now().time_since_epoch().count());

inline int getrand(int l, int r) {
    return std::uniform_int_distribution<int>(l, r)(generator);
}

str tolowerstr(str a) {
    str ans = a;
    for (int i = 0; i < int(a.size()); ++i)
        ans[i] = tolower(ans[i]);
    return ans;
}

str toupperstr(str a) {
    str ans = a;
    for (int i = 0; i < int(a.size()); ++i)
        ans[i] = toupper(ans[i]);
    return ans;
}

str toBinary(int n) {
    bitset<32> a(n);
    return a.to_string();
}

int main() {
    fastio;
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i) cin >> a[i];
        int mn = INF, mnindex = 0, mx = 0, mxindex = 0;
        for (int i = 0; i < n; ++i) {
            if (mn > a[i])
                mn = a[i], mnindex = i;
            if (mx < a[i])
                mx = a[i], mxindex = i;
        }
        if (mxindex < mnindex)
            swap(mxindex, mnindex);
        int ans = min({mxindex + 1, mnindex + 1 + (n - mxindex), (n - mnindex)});
        cout << ans << "\n";
    }
    return 0;
}
