Polycarp is playing a new computer game. This game has $n$ stones in a row. The stone on the position $i$ has integer power $a_i$. The powers of all stones are distinct.

Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.

Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.

For example, if $n = 5$ and $a = [1, 5, 4, 3, 2]$, then Polycarp could make the following moves:

Destroy the leftmost stone. After this move $a = [5, 4, 3, 2]$;

Destroy the rightmost stone. After this move $a = [5, 4, 3]$;

Destroy the leftmost stone. After this move $a = [4, 3]$. Polycarp destroyed the stones with the greatest and least power, so he can end the game.

Please note that in the example above, you can complete the game in two steps. For example:

Destroy the leftmost stone. After this move $a = [5, 4, 3, 2]$;

Destroy the leftmost stone. After this move $a = [4, 3, 2]$. Polycarp destroyed the stones with the greatest and least power, so he can end the game.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 100$). Then $t$ test cases follow.

The first line of each test case contains one integer $n$ ($2 \le n \le 100$) — the number of stones.

The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the power of the stones.


-----Output-----

For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power.


-----Examples-----

Input
5
5
1 5 4 3 2
8
2 1 3 4 5 6 8 7
8
4 2 3 1 8 6 7 5
4
3 4 2 1
4
2 3 1 4
Output
2
4
5
3
2


-----Note-----

None

Source: codeforces | Difficulty: EASY
Tags: Dynamic programming

Complete search

Greedy algorithms


Solutions:
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mi = min(a)
	ma = max(a)
	if a.index(mi) + a.index(ma) > a[::-1].index(mi) + a[::-1].index(ma):
		a = a[::-1]
	if abs(a.index(mi) - a.index(ma)) == 1:
		print(max(a.index(mi), a.index(ma)) + 1)
	else:
		print(min(max(a.index(mi), a.index(ma)) + 1, min(a.index(mi), a.index(ma)) + 1 + n - max(a.index(mi), a.index(ma))))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pmin = 0
	pmax = 0
	for i in range(n):
		if a[pmin] < a[i]:
			pmin = i
		if a[pmax] > a[i]:
			pmax = i
	left = max(pmin, pmax)
	right = max(n - pmin - 1, n - pmax - 1)
	left_right = min(pmin + n - pmax, pmax + n - pmin)
	print(min(left, right, left_right) + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = l.index(max(l))
	b = l.index(min(l))
	diff = abs(a - b)
	a_n = n - a
	b_n = n - b
	a += 1
	b += 1
	print(min(max(a, b), max(a_n, b_n), a + b_n, a_n + b, a + diff, b + diff, a_n + diff, b_n + diff))


from sys import stdin, stdout, setrecursionlimit as srl
from threading import stack_size
from collections import deque, defaultdict as dd
from math import gcd, ceil
input = stdin.readline
flush = stdout.flush

def ub(arr, lo, hi, val):
	while lo < hi:
		mid = (lo + hi) // 2
		if arr[mid] > val:
			hi = mid - 1
		elif arr[mid] <= val:
			lo = mid + 1
	if lo == len(arr) - 1 and arr[-1] <= val:
		lo = len(arr)
	return lo

def lb(arr, lo, hi, val):
	while lo < hi:
		mid = (lo + hi) // 2
		if arr[mid] < val:
			lo = mid + 1
		elif arr[mid] >= val:
			hi = mid - 1
	if lo == len(arr) - 1 and arr[-1] < val:
		lo = len(arr)
	return lo
tc = 1
tc = int(input())
for _ in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	mx = max(a)
	mn = min(a)
	(mxs, mns) = (-1, -1)
	for i in range(n):
		if a[i] == mx and mxs == -1:
			mxs = i
		if a[i] == mn and mns == -1:
			mns = i
	(mxe, mne) = (-1, -1)
	for i in range(n - 1, -1, -1):
		if a[i] == mx and mxe == -1:
			mxe = i
		if a[i] == mn and mne == -1:
			mne = i
	ans = min(max(mxs, mns) + 1, max(n - 1 - mxe, n - 1 - mne) + 1, 2 + mxs + n - 1 - mne, 2 + mns + n - 1 - mxe)
	print(ans)


for i in range(int(input())):
	n = int(input())
	k = [int(i) for i in input().split()]
	x1 = k.index(min(k)) + 1
	x2 = k.index(max(k)) + 1
	a1 = min(x1, x2)
	a2 = max(x1, x2)
	print(min(a2, n - a1 + 1, a1 + n - a2 + 1))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mi = 1000
	ma = 0
	ii = -1
	j = -1
	for i in range(n):
		if l[i] < mi:
			mi = l[i]
			ii = i
		if l[i] > ma:
			ma = l[i]
			j = i
	if ii < j:
		print(min(j + 1, n - j + (ii + 1), n - ii))
	else:
		print(min(ii + 1, n - ii + j + 1, n - j))


for _ in ' ' * int(input()):
	n = int(input())
	(*a,) = map(int, input().split())
	min_a = a.index(min(a))
	max_a = a.index(max(a))
	left_left = max(min_a, max_a) + 1
	left_right = min_a + n - max_a + 1
	right_left = n - min_a + max_a + 1
	right_right = max(n - min_a, n - max_a)
	print(min(left_left, left_right, right_left, right_right))


t = int(input())
for t1 in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	s = x.index(1)
	s1 = x.index(n)
	ans = min(min(s + 1, s1 + 1, n - s, n - s1) + abs(s - s1), n + min(s, s1) - max(s, s1) + 1)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	min_val = min(arr)
	max_val = max(arr)
	min_index = arr.index(min_val) + 1
	max_index = arr.index(max_val) + 1
	moves1 = n - min(min_index, max_index) + 1
	moves2 = max(min_index, max_index)
	moves3 = min(min_index, max_index) + n - max(min_index, max_index) + 1
	print(min(moves1, moves2, moves3))


for t in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	mni = 0
	mxi = 0
	for i in range(n):
		if l[i] == 1:
			mni = i
		if l[i] == n:
			mxi = i
	a = min(mni, mxi)
	b = max(mni, mxi)
	p1 = b + 1
	p2 = n - a
	p3 = a + 1 + (n - b)
	print(min(p1, p2, p3))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = a.index(1)
	y = a.index(n)
	r = min(min(x + 1, y + 1, n - x, n - y) + abs(x - y), n + min(x, y) - max(x, y) + 1)
	print(r)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	a = 0
	a1 = 0
	for i in range(len(arr)):
		if arr[i] == min(arr):
			a += i
		elif arr[i] == max(arr):
			a1 += i
	arr1 = arr[::-1]
	b = 0
	b1 = 0
	for i in range(len(arr1)):
		if arr1[i] == min(arr1):
			b += i
		elif arr1[i] == max(arr1):
			b1 += i
	ans = []
	ans.append(max(a, a1) + 1)
	ans.append(max(b, b1) + 1)
	ans.append(min(a, b) + 1 + min(a1, b1) + 1)
	print(min(ans))


from math import ceil, floor
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = min(arr)
	mx = max(arr)
	i = arr.index(mn)
	j = arr.index(mx)
	print(min(abs(j - i) + min(n - i, n - j, i + 1, j + 1), n - abs(i - j) + 1))


def cf725A(n: int, numbers: list):
	max_left = max_index = numbers.index(n)
	max_right = n - max_left - 1
	min_left = min_index = numbers.index(1)
	min_right = n - min_left - 1
	curr = n
	temp = max_left
	if 0 <= min_index <= max_left:
		curr = min(curr, temp + 1)
	else:
		curr = min(curr, temp + min_right + 2, min_left + 1)
	temp = max_right
	if max_index <= min_index <= n:
		curr = min(curr, temp + 1)
	else:
		curr = min(curr, temp + min_left + 2, min_right + 1)
	return curr
for _ in range(int(input())):
	n = int(input())
	numbers = list(map(int, input().split()))
	print(cf725A(n, numbers))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	i1 = 0
	i2 = 0
	for j in range(n):
		if a[j] >= a[i2]:
			i2 = j
		if a[j] <= a[i1]:
			i1 = j
	al = min(max(i1, i2) + 1, n - min(i1, i2), n + i1 + 1 - i2, n + 1 + i2 - i1)
	print(al)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(i) for i in input().split()]
		mx = float('-inf')
		mn = float('inf')
		pmx = pmn = -1
		for i in range(n):
			if a[i] > mx:
				mx = a[i]
				pmx = i
			if a[i] < mn:
				mn = a[i]
				pmn = i
		res = max(pmn, pmx) + 1
		res = min(res, max(n - pmn, n - pmx))
		res = min(res, pmn + 1 + n - pmx)
		res = min(res, pmx + 1 + n - pmn)
		print(res)
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(mn, mx) = (l.index(min(l)), l.index(max(l)))
	(x, y) = (min(mn, mx), max(mn, mx))
	print(min(x + 1 + n - y, n - x, y + 1))


n = int(input())
stones = []

def stone(a):
	n1 = len(a)
	min_ind = a.index(1)
	max_ind = a.index(n1)
	k = max(min_ind, max_ind) + 1
	k1 = len(a) - min(min_ind, max_ind)
	if min_ind > max_ind:
		k2 = max_ind + 1 + len(a) - min_ind
	else:
		k2 = min_ind + 1 + len(a) - max_ind
	stones.append(min(k, k1, k2))
for i in range(n):
	n1 = int(input())
	a = input().split()
	a = [int(i) for i in a]
	stone(a)
for i in range(n):
	print(stones[i])


for _ in range(int(input())):
	input()
	a = list(map(int, input().split()))
	c = 0
	s = a.index(min(a))
	l = a.index(max(a))
	print(min(max(s, l) + 1, len(a) - min(s, l), 1 + min(s, l) + len(a) - max(s, l)))


for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	k = list1.index(1)
	l = list1.index(n)
	list1 = []
	maxi = max(k, l)
	mini = min(k, l)
	maxi += 1
	mini += 1
	list1.append(mini + (n - maxi + 1))
	list1.append(maxi)
	list1.append(n - mini + 1)
	print(min(list1))


t = int(input())
while t != 0:
	t -= 1
	n = int(input()) - 1
	a = list(map(int, input().split()))
	le = min(a.index(max(a)), a.index(min(a)))
	ri = max(a.index(max(a)), a.index(min(a)))
	mid = ri - le - 1
	ri = n - ri
	print(sum([le, ri, mid]) - max(le, ri, mid) + 2)


ans = []
for _ in range(int(input())):
	n = int(input())
	(*s,) = map(int, input().split())
	mn = s.index(min(s))
	mx = s.index(max(s))
	left = min(mx, mn)
	right = max(mx, mn)
	go_left = n - left
	go_right = right + 1
	two_side = left + 1 + n - right
	ans += [min(go_left, go_right, two_side)]
print('\n'.join(map(str, ans)))


t = int(input())
for i in range(t):
	n = int(input())
	ar = [int(i) for i in input().split()]
	mini = 1000000000.0
	maxi = 0
	min_index = 0
	max_index = 0
	for i in range(n):
		if ar[i] < mini:
			mini = ar[i]
			min_index = i
		if ar[i] > maxi:
			maxi = ar[i]
			max_index = i
	left = max(min_index + 1, max_index + 1)
	right = max(n - min_index, n - max_index)
	left_right = min_index + 1 + n - max_index
	right_left = max_index + 1 + n - min_index
	print(min(left, right, left_right, right_left))


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	minn = min(arr)
	maxx = max(arr)
	ans = 0
	minind = arr.index(minn)
	maxind = arr.index(maxx)
	a = min(maxind, minind)
	b = max(maxind, minind)
	x = min(a, n - 1 - b) + 1
	steps = x + min(b - a, max(a, n - 1 - b) + 1)
	print(steps)


for t in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	(mn, mx) = (arr.index(min(arr)), arr.index(max(arr)))
	print(min(max(mn, mx) + 1, N - min(mn, mx), mn + N + 1 - mx, mx + N + 1 - mn))


t = int(input())
while t != 0:
	n = int(input())
	arr = [int(k) for k in input().split()]
	ma = max(arr)
	mi = min(arr)
	x = arr.index(ma) + 1
	y = arr.index(mi) + 1
	a = max(x, y)
	b = max(n - x, n - y) + 1
	c = min(x, y) + n - max(x, y) + 1
	print(min(a, min(b, c)))
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mi = 100
	imin = -1
	ma = 0
	imax = -1
	for i in range(n):
		if a[i] < mi:
			mi = a[i]
			imin = i
		if a[i] > ma:
			ma = a[i]
			imax = i
	(x, y) = (min(imin, imax), max(imin, imax))
	z = y - x
	y = n - y
	x += 1
	print(min(x + y, x + z, y + z))


m = int(input())
while m != 0:
	n = int(input())
	a = list(map(int, input().split()))
	mx = max(a)
	mx = a.index(mx)
	mn = min(a)
	mn = a.index(mn)
	ans = min(max(mx, mn) + 1, n - 1 - min(mx, mn) + 1, n - 1 - mx + mn + 2, n - 1 - mn + mx + 2)
	print(ans)
	m -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	assert len(a) == n
	mina = a[0]
	maxa = a[0]
	minai = 0
	maxai = 0
	for (i, v) in enumerate(a):
		if v < mina:
			mina = v
			minai = i
		if v > maxa:
			maxa = v
			maxai = i
	print(min(max(minai, maxai) + 1, n - min(minai, maxai), min(minai, maxai) + 1 + n - max(minai, maxai)))


def main():
	testCases = int(input())
	for i in range(testCases):
		n = int(input())
		stones = list(map(int, input().split()))
		indexMax = stones.index(max(stones))
		indexMin = stones.index(min(stones))
		a = indexMax * 1
		b = indexMin * 1
		c = n - 1 - indexMax
		d = n - 1 - indexMin
		start = min(a, b, c, d)
		ans = 0
		if start == a:
			ans += a + 1 + min(abs(indexMax - indexMin), b, d)
			if min(abs(indexMax - indexMin), b, d) != abs(indexMax - indexMin):
				ans += 1
		elif start == b:
			ans += b + 1 + min(abs(indexMax - indexMin), a, c)
			if min(abs(indexMax - indexMin), a, c) != abs(indexMax - indexMin):
				ans += 1
		elif start == c:
			ans += c + 1 + min(abs(indexMax - indexMin), b, d)
			if min(abs(indexMax - indexMin), b, d) != abs(indexMax - indexMin):
				ans += 1
		elif start == d:
			ans += d + 1 + min(abs(indexMax - indexMin), a, c)
			if min(abs(indexMax - indexMin), a, c) != abs(indexMax - indexMin):
				ans += 1
		print(ans)
main()


t = int(input())
res = []
for i in range(t):
	n = int(input())
	mas = list(map(int, input().split()))
	min = 101
	max = 0
	min_k = 0
	max_k = 0
	for j in range(n):
		if min > mas[j]:
			min = mas[j]
			min_k = j
		if max < mas[j]:
			max = mas[j]
			max_k = j
	cnt_1 = min_k + 1 + abs(min_k - max_k)
	cnt_2 = max_k + 1 + abs(min_k - max_k)
	cnt_3 = n - max_k + abs(min_k - max_k)
	cnt_4 = n - min_k + abs(min_k - max_k)
	cnt_5 = n - min_k + max_k + 1
	cnt_6 = n - max_k + min_k + 1
	cnt = [cnt_1, cnt_2, cnt_3, cnt_4, cnt_5, cnt_6]
	result = 110
	for s in range(6):
		if result > cnt[s] and cnt[s] > 0:
			result = cnt[s]
	res.append(result)
for y in res:
	print(y)


t = int(input())
for tc in range(t):
	n = int(input())
	arr = [int(z) for z in input().split()]
	mx = arr.index(max(arr))
	mn = arr.index(min(arr))
	(f, l) = (min(mn, mx), max(mn, mx))
	if n - l > f + 1:
		m = f + 1
		m += min(n - l, l - f)
	else:
		m = n - l
		m += min(f + 1, l - f)
	print(m)


t = int(input())
for _t in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min_idx = a.index(min(a)) + 1
	max_idx = a.index(max(a)) + 1
	mn = min(min_idx, max_idx)
	mx = max(min_idx, max_idx)
	print(min(mx, n - mn + 1, mn + (n - mx) + 1))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	m = []
	k = 0
	pos1 = a.index(min(a))
	pos2 = a.index(max(a))
	if pos1 < pos2:
		k += len(a[0:pos1 + 1])
		k += len(a[pos1 + 1:pos2 + 1])
	else:
		k += len(a[0:pos2 + 1])
		k += len(a[pos2 + 1:pos1 + 1])
	m.append(k)
	k = 0
	s = a[::-1]
	pos1 = s.index(min(a))
	pos2 = s.index(max(a))
	if pos1 < pos2:
		k += len(a[0:pos1 + 1])
		k += len(a[pos1 + 1:pos2 + 1])
	else:
		k += len(a[0:pos2 + 1])
		k += len(a[pos2 + 1:pos1 + 1])
	m.append(k)
	k = 0
	pos1 = a.index(min(a))
	pos2 = a.index(max(a))
	if pos1 < pos2:
		k += len(a[0:pos1 + 1])
		k += len(a[pos2:n])
	else:
		k += len(a[0:pos2 + 1])
		k += len(a[pos1:n])
	m.append(k)
	print(min(m))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	minumum = min(arr)
	maxm = max(arr)
	ind_min = 0
	ind_max = 0
	for i in range(n):
		if arr[i] == minumum:
			ind_min = i
		if arr[i] == maxm:
			ind_max = i
	if ind_max > ind_min:
		print(min(n - ind_max + ind_min + 1, ind_max + 1, n - ind_min))
	else:
		print(min(n - ind_min + ind_max + 1, ind_min + 1, n - ind_max))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mi = a.index(min(a))
	ma = a.index(max(a))
	if ma < mi:
		(ma, mi) = (mi, ma)
	print(min(mi + 1 + ma - mi, n - mi, mi + 1 + n - ma))


n = int(input())
for i in range(n):
	k = int(input())
	arr = list(map(int, input().split()))
	max_idx = arr.index(max(arr))
	min_idx = arr.index(min(arr))
	less = min(max_idx, min_idx)
	maxx = max(max_idx, min_idx)
	diff = maxx - less + 1
	diff_right = len(arr) - 1 - maxx
	res = min(diff_right + less + 2, diff + diff_right, diff + less)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	mn = lst.index(min(lst)) + 1
	mx = lst.index(max(lst)) + 1
	n2 = []
	x = []
	n2.append(mn - 0)
	n2.append(len(lst) - mn + 1)
	x.append(mx - 0)
	x.append(len(lst) - mx + 1)
	if min(n2) == n2[0] and min(x) == x[0]:
		ans = max(min(n2), min(x))
	elif min(n2) == n2[1] and min(x) == x[1]:
		ans = max(min(n2), min(x))
	else:
		ans = min(min(n2) + min(x), max(n2), max(x))
	print(ans)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	minn = 101
	maxx = 0
	for i in range(0, n):
		minn = min(minn, a[i])
		maxx = max(maxx, a[i])
	pos1 = -1
	pos2 = -1
	for i in range(0, n):
		if a[i] == minn:
			pos1 = i
		if a[i] == maxx:
			pos2 = i
	summ = 0
	for i in range(0, n):
		summ = summ + 1
	ans = min(max(pos1 + 1, pos2 + 1), min(max(n - pos1, n - pos2), min(pos1 + n - pos2 + 1, pos2 + n - pos1 + 1)))
	print(ans)


t = int(input())
for _ in range(t):
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	minI = l.index(min(l)) + 1
	maxI = l.index(max(l)) + 1
	print(min(max(minI, maxI), min(max(n - minI, n - maxI) + 1, min(minI, maxI) + n - max(minI, maxI) + 1)))


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	a1 = max(x)
	b1 = min(x)
	a = x.index(a1)
	b = x.index(b1)
	if abs(a - b) == 1:
		if b > a:
			ans1 = b - 0 + 1
			ans2 = n - b + 1
		else:
			ans1 = a - 0 + 1
			ans2 = n - a + 1
		print(min(ans1, ans2))
	else:
		ele = abs(b - a) - 1
		ans1 = n - ele
		if b > a:
			ans2 = b - 0 + 1
			ans3 = n - a
		else:
			ans2 = a - 0 + 1
			ans3 = n - b
		print(min(ans1, ans2, ans3))


t = int(input())

def mini(a, b):
	if a <= b:
		return a
	else:
		return b

def maxi(a, b):
	if a > b:
		return a
	else:
		return b

def q(l, n):
	l0 = min(l)
	h = max(l)
	i1 = l.index(l0)
	i2 = l.index(h)
	if i1 < i2:
		c1 = i1 + 1 + n - i2
		c2 = i2 + 1
		c3 = n - i1
		return mini(mini(c1, c2), c3)
	else:
		c1 = i2 + 1 + n - i1
		c2 = i1 + 1
		c3 = n - i2
		return mini(mini(c1, c2), c3)
for i in range(t):
	n = int(input())
	j = input().split()
	l = list(map(int, j))
	print(q(l, n))


for t in range(int(input())):
	n = int(input())
	c = list(map(int, input().split()))
	k = c.index(1) + 1
	j = c.index(n) + 1
	(k, j) = (min(k, j), max(k, j))
	print(min(j, n - k + 1, k + n - j + 1))


R = lambda : list(map(int, input().split()))
for _ in ' ' * int(input()):
	n = R()[0]
	a = R()
	(mn, mx) = (min(a), max(a))
	(mni, mxi) = (-1, -1)
	for i in range(n):
		if a[i] == mn:
			mni = i
		if a[i] == mx:
			mxi = i
	if mni > mxi:
		(mni, mxi) = (mxi, mni)
	print(min(mxi + 1, n - mni, mni + 1 + n - mxi))


import math

def main():

	def solve(N, H):
		min_idx = H.index(min(H))
		max_idx = H.index(max(H))
		mid_idx = N // 2
		if min_idx == mid_idx or max_idx == mid_idx:
			if N % 2 == 1:
				return N - mid_idx
			elif min_idx < mid_idx or max_idx < mid_idx:
				return mid_idx + 1
			else:
				return N - mid_idx
		elif min_idx < mid_idx and max_idx < mid_idx:
			return max(min_idx, max_idx) + 1
		elif min_idx > mid_idx and max_idx > mid_idx:
			return N - min(min_idx, max_idx)
		else:
			if min_idx < mid_idx:
				left_side = max_idx + 1
				right_side = N - min_idx
				both_side = min_idx + 1 + N - max_idx
			else:
				left_side = min_idx + 1
				right_side = N - max_idx
				both_side = max_idx + 1 + N - min_idx
			return min(left_side, right_side, both_side)

	def read_input():
		number_tests = int(input())
		for i in range(number_tests):
			N = int(input())
			H = input().split()
			H = list(map(lambda x: int(x), H))
			print(solve(N, H))
	read_input()
main()


for x in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	i = a.index(1)
	j = a.index(n)
	min1 = min(i, j)
	max1 = max(i, j)
	min1 += 1
	max1 += 1
	a = []
	a.append(min1 + (n - max1 + 1))
	a.append(max1)
	a.append(n - min1 + 1)
	print(min(a))


T = int(input())
while T > 0:
	n = int(input())
	list1 = [int(x) for x in input().split()]
	i = 0
	j = n - 1
	mv = 0
	l = 0
	r = 0
	c = -1
	num = -1
	while i <= j:
		if list1[i] == 1 or list1[i] == n:
			l += 1
			c = 1
			num = list1[i]
			break
		if list1[j] == 1 or list1[j] == n:
			r += 1
			c = 2
			num = list1[j]
			break
		i += 1
		j -= 1
		l += 1
		r += 1
	if num == 1:
		rem = n
	else:
		rem = 1
	if c == 1:
		mv += l
		list1 = list1[l:]
		mv1 = list1.index(rem) + 1
		mv2 = len(list1) - mv1 + 1
		mv += min(mv1, mv2)
		print(mv)
	elif c == 2:
		mv += r
		list1 = list1[:n - r]
		mv1 = list1.index(rem) + 1
		mv2 = len(list1) - mv1 + 1
		mv += min(mv1, mv2)
		print(mv)
	T -= 1


import os, sys, math
from io import BytesIO, IOBase
from collections import defaultdict, deque, OrderedDict
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')

def I():
	return int(input())

def In():
	return map(int, input().split())

def ln():
	return list(map(int, input().split()))

def Sn():
	return input().strip()
BUFSIZE = 8192

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return len(a)

def solve():
	n = I()
	l = list(In())
	d = {}
	z = []
	for i in range(n):
		d[l[i]] = i
		z.append(l[i])
	z.sort(reverse=True)
	ans = min(max(d[z[0]] + 1, d[z[-1]] + 1), max(n - d[z[0]], n - d[z[-1]]))
	(t1, t2) = (d[z[0]], d[z[-1]])
	if t1 > t2:
		(t2, t1) = (t1, t2)
	temp = abs(t2 - t1 - 1)
	ans = min(ans, n - temp)
	print(ans)
	pass

def main():
	T = I()
	for i in range(T):
		solve()
M = 998244353
P = 1000000007

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
b = []
vis = []
tot = 0

def dfs(root):
	global tot, vis, gp
	for child in gp[root]:
		if vis[child] == 0:
			tot += 1
			vis[child] = 1
			dfs(child)
pre = [[] for i in range(3)]

def hnbhai(tc):
	n = sb()
	a = sd()
	ind1 = a.index(max(a))
	ind2 = a.index(min(a))
	print(min(max(ind1, ind2) + 1, ind1 + (n - 1 - ind2) + 2, n - 1 - ind1 + ind2 + 2, n - 1 - min(ind1, ind2) + 1))
for _ in range(sb()):
	hnbhai(_ + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = [a.index(min(a)) + 1, a.index(max(a)) + 1]
	p.sort()
	print(min(p[0] + n - p[1] + 1, p[1], n - p[0] + 1))


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	brr = [int(i) for i in arr]
	brr.sort()
	(s, m) = (brr[0], brr[-1])
	inds = arr.index(s) + 1
	indm = arr.index(m) + 1
	print(min(max(inds, indm), max(n - inds + 1, n - indm + 1), inds + (n - indm) + 1, n - inds + indm + 1, n - inds + (n - indm) + 1))


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	mi = ar.index(min(ar))
	Mi = ar.index(max(ar))
	a = max(mi, Mi)
	b = min(mi, Mi)
	m = min(a + 1, n - b, b + 1 + n - a)
	print(m)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	sm = min(lst)
	lm = max(lst)
	sl = lst.index(sm) + 1
	sr = n - lst.index(sm)
	ll = lst.index(lm) + 1
	lr = n - lst.index(lm)
	if lst.index(sm) < lst.index(lm):
		x = min(sl + lr, ll, sr)
	else:
		x = min(ll + sr, sl, lr)
	print(x)


n = int(input())
for i in range(n):
	y = int(input())
	x = list(map(int, input().split()))
	a = min(x)
	b = max(x)
	m1 = x.index(a) + 1
	m2 = len(x) - x.index(b)
	m3 = x.index(b) + 1
	m4 = len(x) - x.index(a)
	m5 = x.index(a) + abs(x.index(a) - x.index(b)) + 1
	m7 = x.index(b) + abs(x.index(a) - x.index(b)) + 1
	x.reverse()
	m6 = x.index(a) + abs(x.index(a) - x.index(b)) + 1
	m8 = x.index(b) + abs(x.index(a) - x.index(b)) + 1
	print(min(m1 + m2, m3 + m4, m5, m7, m6, m8))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	mni = a.index(min(a))
	mxi = a.index(max(a))
	d = abs(mxi - mni)
	if mni < n / 2 and mxi < n / 2:
		print(max(mni, mxi) + 1)
	elif mni >= n / 2 and mxi >= n / 2:
		print(n - min(mni, mxi))
	else:
		print(min(min(n - mni, mni + 1) + min(n - mxi, mxi + 1), min(n - mni, mni + 1) + d, min(n - mxi, mxi + 1) + d))


for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a + []
	a.sort()
	mi = a[0]
	ma = a[-1]
	pmi = [b.index(mi) + 1, n - b.index(mi)]
	pma = [b.index(ma) + 1, n - b.index(ma)]
	x = 0
	if n % 2 == 0:
		nn = n / 2
	else:
		nn = n // 2 + 1
	if min(pmi) < min(pma):
		if pmi[0] < pmi[1]:
			x += pmi[0]
			b = b[pmi[0]:]
		else:
			x += pmi[1]
			b = b[0:pmi[0] - 1]
		pma = [b.index(ma) + 1, len(b) - b.index(ma)]
		if pma[0] < pma[1]:
			x += pma[0]
		else:
			x += pma[1]
	else:
		if pma[0] < pma[1]:
			x += pma[0]
			b = b[pma[0]:]
		else:
			x += pma[1]
			b = b[0:pma[0] - 1]
		pmi = [b.index(mi) + 1, len(b) - b.index(mi)]
		if pmi[0] < pmi[1]:
			x += pmi[0]
		else:
			x += pmi[1]
	print(x)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	p2 = -1
	p1 = -1
	for i in range(n):
		if l[i] == n:
			p2 = i
		elif l[i] == 1:
			p1 = i
	lp1 = p1 + 1
	lp2 = p2 + 1
	rp1 = n - p1
	rp2 = n - p2
	ans1 = min(lp1 + rp2, lp2 + rp1)
	ans2 = max(lp1, lp2)
	ans3 = max(rp1, rp2)
	ans = min(ans2, min(ans1, ans3))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b1 = 0
	b2 = 0
	r = 0
	for i in range(n):
		if r == 2:
			break
		else:
			if a[i] == 1:
				r += 1
				b1 = i
			if a[i] == n:
				r += 1
				b2 = i
	(b1, b2) = sorted([b1, b2])
	print(min([b2 + 1, n - b1, b1 + 1 + n - b2]))


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(0, n):
		if arr[i] == 1:
			mi = i + 1
		elif arr[i] == n:
			mx = i + 1
	x = min(mi, n + 1 - mi)
	y = min(mx, n + 1 - mx)
	diff = abs(mi - mx)
	print(min(x + y, diff + min(x, y)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p1 = a.index(min(a))
	q1 = a.index(max(a))
	p = min(p1, q1)
	q = max(p1, q1)
	print(min(q + 1, n - p, p + 1 + n - q))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	n = iinp()
	arr = lmp()
	i1 = arr.index(max(arr))
	i2 = arr.index(min(arr))
	(i1, i2) = sorted([i1, i2])
	i1 += 1
	i2 += 1
	print(min(i2, n - i1 + 1, i1 + n - i2 + 1))


for _ in range(int(input())):
	row = int(input())
	data = list(map(int, input().split()))
	mi = min(data.index(min(data)), data.index(max(data)))
	ma = max(data.index(min(data)), data.index(max(data)))
	n1 = mi + 1
	n2 = ma - mi
	n3 = row - ma
	print(min(n1 + n3, n1 + n2, n3 + n2))


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	minpos = lst.index(min(lst)) + 1
	maxpos = lst.index(max(lst)) + 1
	if minpos > maxpos:
		(minpos, maxpos) = (maxpos, minpos)
	ans = min(maxpos, min(n - minpos + 1, minpos + n - maxpos + 1))
	print(ans)


def test():
	n = input()
	a = input()
	a = list(map(int, a.split()))
	min_ = min(a)
	max_ = max(a)
	min_ind = a.index(min_)
	max_ind = a.index(max_)
	(min_ind, max_ind) = (min(min_ind, max_ind), max(min_ind, max_ind))
	min_dist = min_ind + 1
	max_dist = len(a) - max_ind
	(min_dist, max_dist) = (min(min_dist, max_dist), max(min_dist, max_dist))
	dist = abs(min_ind - max_ind)
	res = min_dist
	if dist <= max_dist:
		res += dist
	else:
		res += max_dist
	print(res)
for _ in range(int(input())):
	test()


testcase = int(input())
for _ in range(testcase):
	n = int(input())
	li = list(map(int, input().split()))
	rev = []
	for i in range(len(li) - 1, -1, -1):
		rev.append(li[i])
	lowest = min(li)
	largest = max(li)
	minmA = li.index(lowest)
	maxmA = li.index(largest)
	minmB = rev.index(lowest)
	maxmB = rev.index(largest)
	ans = []
	ans.append(max(maxmA, minmA) + 1)
	ans.append(max(maxmB, minmB) + 1)
	ans.append(maxmB + minmA + 2)
	ans.append(maxmA + minmB + 2)
	print(min(ans))


t = int(input())
for i in range(0, t):
	n = int(input())
	m = list(map(int, input().split()))
	a = m.index(max(m))
	b = m.index(min(m))
	i = min(a + 1, n - a)
	j = min(b + 1, n - b)
	if i > j:
		k = min(abs(a - b), i)
		print(j + k)
	else:
		k = min(abs(b - a), j)
		print(i + k)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i1 = a.index(min(a))
	i2 = a.index(max(a))
	c = 0
	if i2 > i1:
		(i1, i2) = (i2, i1)
	if i1 >= n // 2:
		if i2 >= n // 2:
			c += n - i2
		elif i2 + 1 <= n - i1:
			c += i2 + 1
			if n - i1 > i1 - i2:
				c += i1 - i2
			else:
				c += n - i1
		else:
			c += n - i1
			if i2 + 1 > i1 - i2:
				c += i1 - i2
			else:
				c += i2 + 1
	else:
		c += i1 + 1
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	max_num = min_num = l[0]
	max_index = min_index = 0
	for i in range(n):
		if l[i] > max_num:
			max_num = l[i]
			max_index = i
		if l[i] < min_num:
			min_num = l[i]
			min_index = i
	print(min(max(min_index, max_index) + 1, max(n - min_index, n - max_index), min(min_index + 1, n - min_index) + min(max_index + 1, n - max_index)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	mx = max(a)
	mini = a.index(mn)
	maxi = a.index(mx)
	ans = 0
	if mini + 1 <= n - mini:
		ans += mini + 1
		if maxi > mini:
			ans += min(maxi - mini, n - maxi)
	else:
		ans += n - mini
		if maxi < mini:
			ans += min(mini - maxi, maxi + 1)
	ans1 = 0
	if maxi + 1 <= n - maxi:
		ans1 += maxi + 1
		if mini > maxi:
			ans1 += min(mini - maxi, n - mini)
	else:
		ans1 += n - maxi
		if mini < maxi:
			ans1 += min(maxi - mini, mini + 1)
	print(min(ans, ans1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	max_a = max(a)
	min_a = min(a)
	max_a_idx = a.index(max_a)
	min_a_idx = a.index(min_a)
	min_pos = min(min_a_idx, max_a_idx)
	max_pos = max(min_a_idx, max_a_idx)
	left_right = min(max_pos, n - 1 - min_pos) + 1
	both = min_pos + (n - 1 - max_pos) + 2
	moves = min(left_right, both)
	print(moves)


for _ in range(int(input())):
	n = int(input())
	a = [int(_) for _ in input().split()]
	print(min(min(a.index(min(a)), a.index(max(a))) + 1 + n - max(a.index(min(a)), a.index(max(a))), n - min(a.index(max(a)), a.index(min(a))), 1 + max(a.index(max(a)), a.index(min(a)))))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i1 = a.index(min(a))
	i2 = a.index(max(a))
	mini = min(i1, i2)
	maxi = max(i1, i2)
	c1 = maxi + 1
	c2 = n - mini
	c3 = mini + 1 + (n - maxi)
	print(min(c1, c2, c3))


from sys import stdin
input = stdin.readline
from itertools import accumulate

def solve():
	n = int(input())
	nums = [int(char) for char in input().split()]
	(_min, _max) = (min(nums), max(nums))
	(min_pos, max_pos) = (0, 0)
	for (idx, num) in enumerate(nums):
		if num == _min:
			min_pos = idx + 1
		if num == _max:
			max_pos = idx + 1
	(i, j) = (min(min_pos, max_pos), max(min_pos, max_pos))
	from_left = j
	from_right = n - (i - 1)
	both_sides = i + n - (j - 1)
	return min(from_left, from_right, both_sides)
for _ in range(int(input())):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mini = arr.index(min(arr))
	maxi = arr.index(max(arr))
	k1 = min(mini - 0, n - mini - 1)
	k2 = min(maxi - 0, n - maxi - 1)
	ans = 0
	if k1 < k2:
		ans += k1 + 1
		if abs(mini - maxi) <= k2:
			ans += abs(mini - maxi)
		else:
			ans += k2 + 1
	else:
		ans += k2 + 1
		if abs(mini - maxi) <= k1:
			ans += abs(mini - maxi)
		else:
			ans += k1 + 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(1)
	else:
		pmx = 0
		pmn = 0
		mx = l[0]
		mn = l[0]
		for i in range(1, n):
			if l[i] > mx:
				mx = l[i]
				pmx = i
			if l[i] < mn:
				mn = l[i]
				pmn = i
		if pmx <= n // 2 and pmn <= n // 2:
			print(max(pmx, pmn) + 1)
		elif pmx > n // 2 and pmn > n // 2:
			print(n - min(pmx, pmn))
		else:
			if pmx <= n // 2:
				m1 = pmx + 1 + n - pmn
				m2 = min(pmn + 1, n - pmx)
			else:
				m1 = pmn + 1 + n - pmx
				m2 = min(pmx + 1, n - pmn)
			print(min(m1, m2))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(mini, maxi) = (l.index(min(l)), l.index(max(l)))
	(a, b) = (min(mini, maxi), max(mini, maxi))
	print(min(b + 1, a + n - b + 1, n - a))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	minim = a[0]
	maxim = a[0]
	maxI = 0
	minI = 0
	for i in range(n):
		if a[i] > maxim:
			maxim = a[i]
			maxI = i
		if a[i] < minim:
			minim = a[i]
			minI = i
	k = min(maxI, minI) + 1
	t = n - max(maxI, minI)
	l = abs(maxI - minI)
	print(min(k + t, k + l, t + l))


def amain():
	n = int(input())
	a = [int(x) for x in input().split()]
	m = min(a)
	M = max(a)
	mi = a.index(m)
	Mi = a.index(M)
	r = max(mi, Mi)
	l = min(mi, Mi)
	print(min(r + 1, n - l, l + 1 + n - r))
	return

def main():
	t = int(input())
	for i in range(1, 1 + t):
		amain()
	return
main()


import sys
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	l = list(map(int, sys.stdin.readline().split()))
	x = l.index(max(l))
	y = l.index(min(l))
	a = max(x, y) + 1
	b = n - min(x, y)
	s = min(x, y) + 1 + n - max(x, y)
	print(min(a, b, s))


for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	(i, j) = sorted(map(a.index, (1, (n := len(a)))))
	print(min(i + 1 + n - j, j + 1, n - i))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	min_index = arr.index(min(arr))
	max_index = arr.index(max(arr))
	d = abs(min_index - max_index)
	total = 0
	if min(min_index, max_index) + 1 <= len(arr) - max(min_index, max_index) and d <= len(arr) - max(min_index, max_index):
		total += min(min_index, max_index) + 1 + d
	elif min(min_index, max_index) + 1 <= len(arr) - max(min_index, max_index) and d > len(arr) - max(min_index, max_index):
		total += min(min_index, max_index) + 1 + len(arr) - max(min_index, max_index)
	elif min(min_index, max_index) + 1 > len(arr) - max(min_index, max_index) and d <= min(min_index, max_index) + 1:
		total += len(arr) - max(min_index, max_index) + d
	else:
		total += len(arr) - max(min_index, max_index) + min(min_index, max_index) + 1
	print(total)


from sys import stdin
_input = stdin.readline
(_int, _max, _range, _min) = (int, max, range, min)

def solution():
	for _ in _range(_int(_input())):
		n = _int(_input())
		arr = [_int(i) for i in _input().split()]
		mn = arr.index(_min(arr))
		mx = arr.index(_max(arr))
		ans = _min(_max(mn, mx) + 1, n - _min(mn, mx), n - _max(mn, mx) + _min(mn, mx) + 1)
		print(ans)
solution()


test = int(input())
for _ in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	(m, M) = (min(a), max(a))
	(i1, i2) = (a.index(m), a.index(M))
	a = sorted([i1, i2])
	answer = min(a[1] + 1, n - a[0], a[0] + 1 + n - a[1])
	print(answer)


r = int(input())
while r > 0:
	k = int(input())
	l = list(map(int, input().split()))[0:k]
	p = l.index(max(l))
	q = l.index(min(l))
	print(min(max(p + 1, q + 1), min(max(k - p, k - q), min(k - p + q + 1, k - q + p + 1))))
	r -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ma = l.index(max(l))
	mi = l.index(min(l))
	maxn = max(ma, mi)
	minn = min(ma, mi)
	print(min(maxn + 1, minn + n + 1 - maxn, n - minn))


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	x = 0
	y = 0
	for i in range(n):
		if l[i] == 1:
			x = i
		if l[i] == n:
			y = i
	a = min(x, y)
	b = max(x, y)
	p = b + 1
	q = n - a
	r = a + 1 + (n - b)
	print(min(p, q, r))


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	steps = 0
	a = min(arr.index(max(arr)), arr.index(min(arr)))
	b = max(arr.index(max(arr)), arr.index(min(arr)))
	x = min(a, n - 1 - b) + 1
	steps = x + min(b - a, max(a, n - 1 - b) + 1)
	print(steps)


from collections import Counter

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	n1 = l.index(1)
	n2 = l.index(n)
	if n1 > n2:
		(n2, n1) = (n1, n2)
	ans = min(n2 + 1, n - n1, n1 + 1 + n - n2)
	print(ans)
for _ in range(int(input())):
	solve()


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
py = lambda : print('YES')
pn = lambda : print('NO')
t = sInt()
for _ in range(t):
	n = sInt()
	a = lInt()
	b = sorted(a)
	ind = a.index(b[-1])
	ind1 = a.index(b[0])
	ans = []
	ans.append(max(ind, ind1) + 1)
	ans.append(ind + 1 + n - ind1)
	ans.append(ind1 + 1 + n - ind)
	ans.append(max(n - ind, n - ind1))
	print(min(ans))


t = int(input())
natija = []
for i in range(t):
	n = int(input())
	satr = input()
	ruyhat = satr.split(' ')
	for i in range(n):
		ruyhat[i] = int(ruyhat[i])
	Min = min(ruyhat)
	Max = max(ruyhat)
	for i in range(n):
		if ruyhat[i] == Min:
			ind_min = i
		if ruyhat[i] == Max:
			ind_max = i
	if ind_min < ind_max:
		k1 = ind_max + 1
		k2 = n - ind_min
		k3 = ind_min + 1 + (n - ind_max)
		natija.append(min(k1, k2, k3))
	else:
		k1 = ind_min + 1
		k2 = n - ind_max
		k3 = ind_max + 1 + (n - ind_min)
		natija.append(min(k1, k2, k3))
for i in range(t):
	print(natija[i])


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	(maks, mins) = (0, 0)
	for i in range(n):
		if lst[i] == 1:
			mins = i + 1
		if lst[i] == n:
			maks = i + 1
	(kanan, tengah, kiri) = (0, 0, 0)
	kanan = n - max(maks, mins)
	tengah = max(maks, mins) - min(maks, mins) - 1
	kiri = min(maks, mins) - 1
	print(min(kanan + tengah, kanan + kiri, kiri + tengah) + 2)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mx = max(l)
	mn = min(l)
	mxi = l.index(mx)
	mni = l.index(mn)
	if mni < mxi:
		print(min(n - mni, n - mxi + mni + 1, mxi + 1))
	else:
		print(min(mni + 1, n - mxi, n - mni + (mxi + 1)))


import math
for _ in range(int(input())):
	n = int(input())
	array = input().split(' ')
	maxN = -1
	minN = math.inf
	maxP = -1
	minP = -1
	for i in range(n):
		if int(array[i]) > maxN:
			maxN = int(array[i])
			maxP = i
		if int(array[i]) < minN:
			minN = int(array[i])
			minP = i
	left = min(maxP, minP) + 1
	right = n - max(maxP, minP)
	if left + right <= int(n / 2):
		print(left + right)
	else:
		print(min(max(maxP, minP) + 1, left + right, n - min(maxP, minP)))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	x = min(arr)
	y = max(arr)
	cnt = 0
	l_ans = 0
	for i in range(0, n):
		if arr[i] == x or arr[i] == y:
			cnt += 1
			if cnt == 2:
				l_ans = i + 1
				break
	r_ans = 0
	for i in range(n - 1, -1, -1):
		if arr[i] == x or arr[i] == y:
			cnt += 1
			if cnt == 4:
				r_ans = n - i
				break
	m_ans = 0
	for i in range(0, n):
		if arr[i] == x or arr[i] == y:
			m_ans = i + 1
			break
	for i in range(n - 1, -1, -1):
		if arr[i] == x or arr[i] == y:
			m_ans += n - i
			break
	print(min(l_ans, r_ans, m_ans))


a = int(input())
for i in range(a):
	b = int(input())
	c = input().split()
	c = list(map(int, c))
	L = [0] * (b + 1)
	if len(L) % 2 == 0:
		CC = len(L) // 2
	else:
		CC = (len(L) - 1) // 2
	cmin = min(c)
	cmax = max(c)
	for i in range(len(c)):
		if c[i] == cmin:
			cminc = i + 1
			L[i + 1] = cmin
		elif c[i] == cmax:
			cmaxc = i + 1
			L[i + 1] = cmax
	if CC <= cminc and CC <= cmaxc:
		print(len(L) - min(cminc, cmaxc))
	elif CC >= cminc and CC >= cmaxc:
		print(max(cminc, cmaxc))
	else:
		A1 = len(L) - min(cminc, cmaxc)
		A2 = max(cminc, cmaxc)
		A3 = len(L) - max(cminc, cmaxc)
		A4 = min(cminc, cmaxc)
		A5 = A3 + A4
		print(min(A1, A2, A5))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxi = a.index(max(a))
	mini = a.index(min(a))
	minii = min(max(maxi, mini) + 1, n - 1 - min(maxi, mini) + 1, n - 1 - maxi + mini + 2, n - 1 - mini + maxi + 2)
	print(minii)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	(y, x) = (li.index(max(li)) + 1, li.index(min(li)) + 1)
	a = max(y, x)
	b = max(n - y, n - x) + 1
	c = min(y, x) - max(y, x) + 1 + n
	d = min(b, c)
	print(min(a, d))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(mx, mn) = (l.index(max(l)), l.index(min(l)))
	(mx_r, mn_r) = (l[::-1].index(max(l)), l[::-1].index(min(l)))
	if max(mx, mn) < max(mx_r, mn_r):
		st = max(mx, mn)
	else:
		st = max(mx_r, mn_r)
	n_st = 0
	if mx < mx_r:
		n_st += mx + 1
	else:
		n_st += mx_r + 1
	if mn < mn_r:
		n_st += mn + 1
	else:
		n_st += mn_r + 1
	print(min(st + 1, n_st))


import os
import sys

def main():
	fastReadInt = sys.stdin.buffer.readline

	def fastReadStr():
		return sys.stdin.buffer.readline().decode('utf-8').strip()

	def fastWrite(ans):
		return sys.stdout.buffer.write(str(str(ans) + '\n').encode('utf-8'))

	def fastWriteList(ans):
		return sys.stdout.buffer.write(str(' '.join(map(str, ans)) + '\n').encode('utf-8'))
	DEBUG = 'DEBUG' in os.environ

	def debug(*args):
		if DEBUG:
			print('>', *args)

	def solution(value):
		debug('')
		debug(value)
		maxx = -1000000
		minn = 1000000
		maxId = 0
		minId = 0
		for j in range(0, len(value)):
			i = value[j]
			if i > maxx:
				maxx = i
				maxId = j
			if i < minn:
				minn = i
				minId = j
		lowerBound = min(maxId, minId)
		upperBound = max(maxId, minId)
		upperRemain = len(value) - upperBound
		lowerRemain = len(value) - lowerBound - 1
		debug([lowerBound, upperBound])
		debug('lowerBound + upperRemain:', lowerBound + upperRemain)
		debug('upperBound', upperBound)
		debug('lowerRemain', lowerRemain)
		res = min(lowerBound + upperRemain + 1, upperBound + 1, lowerRemain + 1)
		fastWrite(res)
	for t in range(int(fastReadInt())):
		length = fastReadInt()
		solution(list(map(int, fastReadInt().split())))
main()


def solve(t_id):
	n = int(input())
	a = list(map(int, input().split()))
	ind1 = 0
	ind2 = 0
	for i in range(n):
		if a[i] == 1:
			ind1 = i
		elif a[i] == n:
			ind2 = i
	if ind1 > ind2:
		(ind1, ind2) = (ind2, ind1)
	ans = ind1 + 1 + n - ind2
	ans = min(ans, ind2 + 1)
	ans = min(ans, n - ind1)
	print(ans)
t = 1
t = int(input())
for i in range(t):
	solve(i + 1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mni = arr.index(min(arr))
	mx = max(arr)
	mxi = arr.index(mx)
	cnt = 0
	c1 = max(mni, mxi)
	c2 = n - 1 - min(mni, mxi)
	c3 = min(mni, mxi) + n - max(mni, mxi)
	print(min([c1, c2, c3]) + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = min(l)
	b = max(l)
	ai = l.index(a)
	bi = l.index(b)
	x = max(ai, bi) + 1
	y = n - min(ai, bi)
	z = min(ai, bi) + n - max(ai, bi) + 1
	print(min(x, y, z))


l = []
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(x, y) = (a.index(max(a)), a.index(min(a)))
	if x > y:
		(x, y) = (y, x)
	l.append(min((y + 1, n - x, x + n - y + 1)))
print(*l, sep='\n')


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	mi = 0
	ma = 0
	x = y = a[0]
	for i in range(1, len(a)):
		if a[i] > y:
			y = a[i]
			ma = i
		if a[i] < x:
			x = a[i]
			mi = i
	k = min(max(mi + 1, ma + 1), max(n - mi, n - ma), mi + n - ma + 1, ma + n - mi + 1)
	print(k)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(mi, ma) = (0, 0)
	(mini, maxi) = (l[0], l[0])
	for i in range(1, n):
		if mini > l[i]:
			mini = l[i]
			mi = i
		elif maxi < l[i]:
			maxi = l[i]
			ma = i
	if mi < ma:
		(mi, ma) = (ma, mi)
	print(min(min((max(mi, ma) + 1, n - min(mi, ma))), ma + 1 + n - mi))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mini = a.index(min(a))
	maxi = a.index(max(a))
	mi = min(mini, maxi)
	ma = max(mini, maxi)
	if mi + 1 <= n - ma:
		res = mi + 1
		res += min(ma - mi, n - ma)
	else:
		res = n - ma
		res += min(mi + 1, ma - mi)
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(left, right) = sorted((a.index(min(a)), a.index(max(a))))
	print(min([right + 1, n - left, left + 1 + n - right]))


a = int(input())
for x in range(a):
	b = int(input())
	ls = [int(item) for item in input().split(' ')]
	c = min(ls)
	d = max(ls)
	lss = ls[::-1]
	while c in ls and d in ls:
		e = min(ls.index(c), ls.index(d))
		f = min(lss.index(c), lss.index(d))
		if e < f:
			ls = ls[e + 1:]
		else:
			lss = lss[f + 1:]
			ls = lss[::-1]
	if c in ls:
		e = min(ls.index(c), lss.index(c))
		print(b - len(ls) + (e + 1))
	if d in ls:
		e = min(ls.index(d), lss.index(d))
		print(b - len(ls) + (e + 1))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	a = arr.index(max(arr))
	b = arr.index(min(arr))
	p1 = abs(a - b) + min(a + 1, b + 1, len(arr) - a, len(arr) - b)
	p2 = min(a + 1, len(arr) - a) + min(b + 1, len(arr) - b)
	print(min(p1, p2))


def steps(n, idx):
	return min(idx + 1, n - idx)

def solve(n, a):
	(x, y) = sorted([a.index(min(a)), a.index(max(a))])
	(b, c) = (steps(n, x), steps(n, y))
	if b < c:
		result = b + steps(n - b, y - b)
	else:
		result = c + steps(n - c, x)
	return result
T = int(input())
for _ in range(T):
	n = int(input())
	a = [int(x) for x in input().split()]
	result = solve(n, a)
	print(result)


Q = int(input())
for q in range(0, Q):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	m = min(arr)
	M = max(arr)
	p1 = arr.index(m)
	p2 = arr.index(M)
	c1 = min(p1, n - p1 - 1)
	c2 = min(p2, n - p2 - 1)
	cnt = 0
	l1 = 0
	l2 = n - 1
	if c1 == p1 and c1 <= c2:
		cnt = p1 - l1 + 1
		l1 = p1 + 1
		if p2 - l1 <= l2 - p2:
			cnt = cnt + (p2 - l1 + 1)
		else:
			cnt = cnt + (l2 - p2 + 1)
	elif c2 == p2 and c2 < c1:
		cnt = p2 - l1 + 1
		l1 = p2 + 1
		if p1 - l1 <= l2 - p1:
			cnt = cnt + (p1 - l1 + 1)
		else:
			cnt = cnt + (l2 - p1 + 1)
	elif c1 == n - p1 - 1 and c1 <= c2:
		cnt = l2 - p1 + 1
		l2 = p1 - 1
		if p2 - l1 <= l2 - p2:
			cnt = cnt + (p2 - l1 + 1)
		else:
			cnt = cnt + (l2 - p2 + 1)
	elif c2 == n - p2 - 1 and c2 < c1:
		cnt = l2 - p2 + 1
		l2 = p2 - 1
		if p1 - l1 <= l2 - p1:
			cnt = cnt + (p1 - l1 + 1)
		else:
			cnt = cnt + (l2 - p1 + 1)
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(mn, mx) = (a.index(min(a)), a.index(max(a)))
	print(min(max(mn, mx) + 1, n - min(mn, mx), n - mn + mx + 1, n - mx + mn + 1))


def minim(arr, n):
	r = 101
	ind = 0
	for i in range(0, n):
		if r > arr[i]:
			r = min(r, arr[i])
			ind = i
	return ind

def maxim(arr, n):
	r = -1
	ind = -1
	for i in range(0, n):
		if arr[i] > r:
			r = max(r, arr[i])
			ind = i
	return ind
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mini = minim(arr, n)
	maxi = maxim(arr, n)
	if mini < maxi:
		r = mini + 1 + (n - maxi)
	else:
		r = maxi + 1 + (n - mini)
	if mini < maxi:
		r = min(r, n - mini)
	else:
		r = min(r, n - maxi)
	if mini > maxi:
		r = min(r, mini + 1)
	else:
		r = min(r, maxi + 1)
	print(r)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		max_idx = 0
		min_idx = 0
		max_power = a[0]
		min_power = a[0]
		for j in range(n):
			if a[j] > max_power:
				max_power = a[j]
				max_idx = j
			if a[j] < min_power:
				min_power = a[j]
				min_idx = j
		l = min(max_idx, min_idx)
		r = max(max_idx, min_idx)
		choice = r + 1
		if choice > n - l:
			choice = n - l
		if choice > n - r + l + 1:
			choice = n - r + l + 1
		print(choice)
while True:
	try:
		main()
	except:
		break


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	maxi = max(l)
	max_i = l.index(maxi)
	mini = min(l)
	min_i = l.index(mini)
	for i in range(1):
		continue
	for i in range(1):
		continue
	for i in range(1):
		continue
	for i in range(1):
		continue
	for i in range(1):
		continue
	if max_i > min_i:
		maxx = min(max_i, min(n - min_i - 1, min_i + n - max_i))
		print(maxx + 1)
	else:
		maxx = min(min_i, min(n - max_i - 1, max_i + n - min_i))
		print(maxx + 1)


n = int(input())
for i in range(n):
	m = int(input())
	out = 0
	num = list(map(int, input().split()))
	mn = num.index(min(num)) + 1
	mx = num.index(max(num)) + 1
	if mn < mx:
		print(min(mx, len(num) - mn + 1, mn + len(num) - mx + 1))
	else:
		print(min(mn, len(num) - mx + 1, mx + len(num) - mn + 1))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mn1 = a.index(min(a)) + 1
	mn2 = a[::-1].index(min(a)) + 1
	mx1 = a.index(max(a)) + 1
	mx2 = a[::-1].index(max(a)) + 1
	print(min(mx2 + mn1, mn2 + mx1, max(mn1, mx1), max(mx2, mn2)))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = l.index(max(l))
	y = l.index(min(l))
	x += 1
	y += 1
	a = max(x, y)
	b = max(n - x, n - y) + 1
	c = min(x, y) + (n - max(x, y) + 1)
	print(min(a, b, c))


t = int(input(''))
while t:
	n = int(input(''))
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	while 1:
		idx1 = idx2 = -1
		if 1 in a:
			idx1 = a.index(1)
		if n in a:
			idx2 = a.index(n)
		if idx1 == idx2 == -1:
			break
		elif idx1 != -1 and idx2 != -1:
			diff11 = idx1 + 1
			diff1n = len(a) - idx1
			diff1 = min(diff11, diff1n)
			diffn1 = idx2 + 1
			diffnn = len(a) - idx2
			diffn = min(diffn1, diffnn)
			if diff1 > diffn:
				ans += diffn
				if diffn1 > diffnn:
					del a[idx2:len(a)]
				else:
					del a[0:idx2 + 1]
			else:
				ans += diff1
				if diff11 > diff1n:
					del a[idx1:len(a)]
				else:
					del a[0:idx1 + 1]
		elif idx1 == -1:
			diffn1 = idx2 + 1
			diffnn = len(a) - idx2
			if diffn1 > diffnn:
				ans += diffnn
				del a[idx2:len(a)]
			else:
				ans += diffn1
				del a[0:idx2 + 1]
		else:
			diff11 = idx1 + 1
			diff1n = len(a) - idx1
			if diff11 > diff1n:
				ans += diff1n
				del a[idx1:len(a)]
			else:
				ans += diff11
				del a[0:idx1 + 1]
	print(ans)
	t = t - 1


for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	l_pos = ar.index(1) + 1
	r_pos = ar.index(n) + 1
	if l_pos > r_pos:
		temp = l_pos
		l_pos = r_pos
		r_pos = temp
	ans1 = l_pos - 1 + n - r_pos + 2
	ans2 = n - l_pos + 1
	ans3 = r_pos - 1 + 1
	print(min(ans1, ans2, ans3))


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	li = list(map(int, input().split()))
	s = -1
	l = -1
	for i in range(n):
		if li[i] == 1:
			s = i + 1
		if li[i] == n:
			l = i + 1
		if s != -1 and l != -1:
			break
	v1 = s + (n - l + 1)
	v2 = max(s, l)
	v3 = max(n - s + 1, n - l + 1)
	v4 = n - s + 1 + l
	print(min(v1, min(v2, min(v4, v3))))


def solve(arr, n, ans):
	(i1, i2) = (arr.index(min(arr)), arr.index(max(arr)))
	(i1, i2) = (min(i1, i2), max(i1, i2))
	min_val = min(i1 + 1 + n - i2, i2 + 1, n - i1)
	ans.append(str(min_val))

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		solve(arr, n, ans)
	print('\n'.join(ans))
main()


def solve():
	n = int(input())
	l = [int(x) for x in input().split()]
	r = l[::-1]
	mi = min(l)
	ma = max(l)
	mii = l.index(mi)
	mai = l.index(ma)
	mir = r.index(mi)
	mar = r.index(ma)
	left = max(mii + 1, mai + 1)
	right = max(mir + 1, mar + 1)
	a = mii + 1 + mar + 1
	b = mai + 1 + mir + 1
	print(min([a, b, left, right]))
test_cases = int(input())
start = 0
while start < test_cases:
	solve()
	start += 1


def f(l):
	i1 = max(l.index(max(l)), l.index(min(l))) + 1
	i2 = len(l) - min(l.index(max(l)), l.index(min(l)))
	i3 = min(l.index(max(l)), l.index(min(l))) + 1 + len(l) - max(l.index(max(l)), l.index(min(l)))
	return min(i1, i2, i3)
t = int(input())
l = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = f(a)
	l.append(s)
for i in l:
	print(i)


t = int(input())

def f(l, n):
	max_i = 0
	min_i = 0
	max_v = l[0]
	min_v = l[0]
	for i in range(n):
		if l[i] > max_v:
			max_i = i
			max_v = l[i]
		if l[i] < min_v:
			min_i = i
			min_v = l[i]
	ll = max(max_i, min_i) + 1
	lr = max_i + n - min_i + 1
	rl = min_i + n - max_i + 1
	rr = max(n - max_i, n - min_i)
	return min(ll, lr, rl, rr)
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(f(l, n))


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	mi = min(l)
	ma = max(l)
	c = 0
	i1 = l.index(mi)
	i2 = l.index(ma)
	if i2 < i1:
		l = l[::-1]
		i1 = l.index(mi)
		i2 = l.index(ma)
	if i1 + 1 < n - i2:
		c += i1 + 1
		l = l[i1 + 1:]
		i2 = l.index(ma)
		if i2 + 1 < len(l) - i2:
			c += i2 + 1
			l = l[i2 + 1:]
		else:
			c += len(l) - i2
			l = l[:-(len(l) - i2)]
	else:
		c += len(l) - i2
		l = l[:-(len(l) - i2)]
		i1 = l.index(mi)
		if i1 + 1 < len(l) - i1:
			c += i1 + 1
			l = l[i1 + 1:]
		else:
			c += len(l) - i1
			l = l[:-(len(l) - i1)]
	print(c)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a1 = a[::-1]
	mx = max(a)
	mn = min(a)
	l = len(a)
	for i in range(l):
		if a[i] == mn:
			mn_lb = i + 1
		if a[i] == mx:
			mx_lb = i + 1
	for i in range(l):
		if a1[i] == mn:
			mn_rb = i + 1
		if a1[i] == mx:
			mx_rb = i + 1
	for i in range(l):
		if a[i] == mn:
			mn_l1 = i + 1
	for i in range(l):
		if a1[i] == mx:
			mx_l1 = i + 1
	for i in range(l):
		if a1[i] == mn:
			mn_l2 = i + 1
	for i in range(l):
		if a[i] == mx:
			mx_l2 = i + 1
	ans = min(max(mn_lb, mx_lb), max(mn_rb, mx_rb), mn_l1 + mx_l1, mn_l2 + mx_l2)
	print(ans)
	t -= 1


t = int(input())
for tc in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(a, b) = (arr.index(max(arr)), arr.index(min(arr)))
	if b < a:
		(a, b) = (b, a)
	print(n - max(b - a - 1, a, n - 1 - b))


from sys import stdin, stdout

def istr():
	return input()

def inum():
	return int(stdin.readline())

def imul():
	return map(int, stdin.readline().split())

def ilst():
	return list(map(int, stdin.readline().split()))

def splt():
	return list(stdin.readline().strip())

def emat(row):
	return [ilst() for i in range(row)]
max_itr = 100
from itertools import combinations
import math
for i in range(inum()):
	n = inum()
	a = ilst()
	maxx = max(a)
	minn = min(a)
	x = a.index(maxx)
	y = a.index(minn)
	l = max(x + 1, y + 1)
	fir = min(x + n - y + 1, y + n - x + 1)
	sec = max(n - x, n - y)
	m = min(fir, sec)
	ans = min(l, m)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = a.index(min(a)) + 1
	f = a.index(max(a)) + 1
	(k, f) = (min(k, f), max(k, f))
	print(min(k + n - f + 1, n - k + 1, f))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min_idx = a.index(min(a))
	max_idx = a.index(max(a))
	print(min(max(min_idx, max_idx) + 1, max(n - min_idx, n - max_idx), min_idx + 1 + n - max_idx, max_idx + 1 + n - min_idx))


for _ in [*open(0)][2::2]:
	arr = list(map(int, _.split()))
	l = len(arr)
	mini = min(arr)
	maxi = max(arr)
	for i in range(l):
		if arr[i] == mini:
			x = i + 1
		if arr[i] == maxi:
			y = i + 1
	print(min(max(x, y), l - min(x, y) + 1, l - x + y + 1, l + x - y + 1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(d) for d in input().split()]
	max_ = a.index(max(a))
	min_ = a.index(min(a))
	print(min(max(max_ + 1, min_ + 1), max_ + 1 + n - min_, min_ + 1 + n - max_, max(n - max_, n - min_)))


t = int(input())

def cal(nums):
	(mi, ma) = (0, 0)
	(iv, av) = (float('inf'), -float('inf'))
	for (ind, i) in enumerate(nums):
		if i < iv:
			mi = ind
			iv = i
		if i > av:
			av = i
			ma = ind
	if ma < mi:
		(ma, mi) = (mi, ma)
	n = len(nums)
	ans = float('inf')
	if ma < mi:
		(mi, ma) = (ma, mi)
	ans = min(ans, n - ma + mi + 1)
	ans = min(ans, n - mi)
	ans = min(ans, ma + 1)
	ans = min(ans, mi + 1 + n - ma)
	print(ans)
for _ in range(t):
	n = input()
	nums = list(map(int, input().split()))
	cal(nums)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split(' ')))
	mx = max(lst)
	mn = min(lst)
	mni = 0
	mnx = 0
	for i in range(n):
		if lst[i] == mn:
			mni = i
		if lst[i] == mx:
			mnx = i
	if mni < mnx:
		print(min(mnx + 1, n - mni, mni + 1 + n - mnx))
	else:
		print(min(mni + 1, n - mnx, mnx + 1 + n - mni))


t = int(input())
for t in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	a = lst.index(min(lst))
	b = lst.index(max(lst))
	if a > b:
		(a, b) = (b, a)
	j = max(a, b - a - 1, len(lst) - b - 1)
	print(len(lst) - j)


import os
import sys
from io import BytesIO, IOBase
_print = print
BUFSIZE = 8192

def dbg(*args, **kwargs):
	_print('\x1b[95m', end='')
	_print(*args, **kwargs)
	_print('\x1b[0m', end='')

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, inp().split(' '))

def itg():
	return int(inp())

def solve():
	n = itg()
	arr = tuple(mpint())
	(i, j) = (arr.index(min(arr)), arr.index(max(arr)))
	if i > j:
		(i, j) = (j, i)
	i += 1
	j += 1
	n += 1
	return min(i + n - j, j, n - i)

def main():
	for _ in range(itg()):
		print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1538/problem/A'
if DEBUG == 1:
	import requests
	from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
	runner = TestCaseRunner(main, URL)
	inp = runner.input_stream
	print = runner.output_stream
	runner.checking()
else:
	if DEBUG != 2:
		dbg = lambda *args, **kwargs: ...
		(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	if DEBUG == 3:

		def print(*args, **kwargs):
			_print(*args, **kwargs)
			sys.stdout.flush()
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input().split(' ')
	a_int = []
	for i in a:
		a_int.append(int(i))
	a = a_int
	dots = [a.index(max(a)), a.index(min(a))]
	dots.sort()
	res = 0
	res += min(dots[0] + 1, n - dots[1])
	res += min(dots[1] - dots[0], max(dots[0] + 1, n - dots[1]))
	print(res)


num = int(input())
for i in range(num):
	lenz = int(input())
	arr = input()
	arr = arr.split(' ')
	arr = [int(x) for x in arr]
	minn = min(arr)
	maxx = max(arr)
	indexMin = arr.index(minn)
	indexMax = arr.index(maxx)
	if indexMax > indexMin:
		print(min(indexMax + 1, lenz - indexMin, indexMin + 1 + lenz - indexMax))
	else:
		print(min(indexMin + 1, lenz - indexMax, indexMax + 1 + lenz - indexMin))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(mx, mn) = (0, 0)
	for i in range(1, n):
		if l[mx] < l[i]:
			mx = i
		elif l[mn] > l[i]:
			mn = i
	if mx < mn:
		(mx, mn) = (mn, mx)
	print(n - max(mn, mx - mn - 1, n - mx - 1))


for _ in range(int(input())):
	n = int(input())
	spic = list(map(int, input().split()))
	mins = spic.index(min(spic))
	maxs = spic.index(max(spic))
	print(min(max(mins, maxs) + 1, n - min(mins, maxs), n - maxs + mins + 1, n - mins + maxs + 1))


for _ in range(int(input())):
	n = int(input())
	temp = list(map(int, input().split()))
	k1 = temp.index(max(temp))
	k2 = temp.index(min(temp))
	temp1 = [max(k1, k2) + 1, n - min(k1, k2)]
	if k1 < k2:
		temp1.append(len(temp[:k1 + 1]) + len(temp[k2:]))
	else:
		temp1.append(len(temp[:k2 + 1]) + len(temp[k1:]))
	print(min(temp1))


def fun(n, arr):
	i = arr.index(min(arr))
	j = arr.index(max(arr))
	ans = min(max(j + 1, i + 1), max(n - j, n - i), min(i + 1, n - i) + min(j + 1, n - j))
	return ans
for i in range(0, int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = fun(n, arr)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	imin = 0
	imax = 0
	for i in range(n):
		if l[i] == 1:
			imin = i
		if l[i] == n:
			imax = i
	i + 1
	n - i
	sminl = 0
	sminr = 0
	smaxl = 0
	smaxr = 0
	ans = 0
	sminl = imin + 1
	sminr = n - imin
	smaxl = imax + 1
	smaxr = n - imax
	smin = min(sminl, sminr)
	smax = min(smaxl, smaxr)
	if smin <= smax:
		ans += smin
		if smin == sminl:
			ans += min(smaxr, smaxl - smin)
		else:
			ans += min(smaxl, smaxr - smin)
	elif smin > smax:
		ans += smax
		if smax == smaxl:
			ans += min(sminr, sminl - smax)
		else:
			ans += min(sminl, sminr - smax)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split(' ')
	s = [int(x) for x in s]
	contador = 0
	for j in range(len(s)):
		contador += 1
		if s[j] == 1 or s[j] == n:
			flag = 1
			break
		if s[-(j + 1)] == 1 or s[-(j + 1)] == n:
			flag = 0
			break
	while True:
		if flag == 1:
			x = s.pop(0)
		else:
			x = s.pop(-1)
		if x == n or x == 1:
			break
	for k in range(len(s)):
		contador += 1
		if s[k] == 1 or s[k] == n:
			break
		if s[-(k + 1)] == 1 or s[-(k + 1)] == n:
			break
	print(contador)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = a.index(min(a))
	l = a.index(max(a))
	print(min(max(k, l) + 1, n - min(k, l), min(k, l) + 1 + n - max(k, l)))


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	mx = a.index(max(a))
	mn = a.index(min(a))
	l = min(a.index(max(a)), a.index(min(a)))
	r = max(a.index(max(a)), a.index(min(a)))
	ans1 = r + 1
	ans2 = n - l
	ans3 = l + 1 + n - r
	print(min((ans1, ans2, ans3)))


import math
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	s = min(arr)
	si = arr.index(s)
	l = max(arr)
	li = arr.index(l)
	arr2 = [si + 1, li + 1, n - si, n - li]
	k = min(arr2)
	key = arr2.index(k)
	ans = 0
	ans += k
	if key == 0:
		ans += min(n - li, li - si)
	elif key == 1:
		ans += min(n - si, si - li)
	elif key == 2:
		ans += min(si - li, li + 1)
	else:
		ans += min(li - si, si + 1)
	print(ans)
	t = t - 1


t = int(input())

def min(x, y):
	if x <= y:
		return x
	else:
		return y

def q(l, n):
	h = max(l)
	i1 = l.index(h)
	l1 = l[:]
	l1.sort()
	i2 = l.index(l1[0])
	if i1 < i2:
		return [i1 + 1, i2 - i1, n - i2]
	else:
		return [i2 + 1, i1 - i2, n - i1]

def h(l):
	return min(min(l[0] + l[2], l[0] + l[1]), l[1] + l[2])
for i in range(t):
	n = int(input())
	j = input().split()
	l = list(map(int, j))
	print(h(q(l, n)))


t = int(input())
while t:
	n = int(input())
	s = list(map(int, input().split()))
	A = min(s)
	B = max(s)
	a = s.index(A)
	b = s.index(B)
	if a < b:
		if b + 1 > a + 1 + n - b and n - a > a + 1 + n - b:
			print(a + 1 + n - b)
		elif n - a <= a + 1 + n - b and n - a <= b + 1:
			print(n - a)
		else:
			print(b + 1)
	elif a + 1 > b + 1 + n - a and n - b > b + 1 + n - a:
		print(b + 1 + n - a)
	elif n - b <= b + 1 + n - a and n - b <= a + 1:
		print(n - b)
	else:
		print(a + 1)
	t = t - 1


import bisect
from collections import Counter

def main(a):
	min_a = float('inf')
	max_a = float('-inf')
	min_idx = -1
	max_idx = -1
	for (i, x) in enumerate(a):
		if x < min_a:
			min_a = x
			min_idx = i
		if x > max_a:
			max_a = x
			max_idx = i
	p = min(min_idx, max_idx)
	q = max(min_idx, max_idx)
	n = len(a)
	return min(q + 1, n - p, n - q + p + 1)
t = int(input())
outputs = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	output = main(arr)
	outputs.append(output)
for o in outputs:
	print(o)


for i in range(int(input())):
	a = int(input())
	b = [int(X) for X in input().split()]
	c = b[:]
	c.sort()
	mi = b.index(c[0])
	ma = b.index(c[-1])
	l = max(mi, ma)
	r = min(mi, ma)
	p = l + 1
	q = a - r
	k = r + 1 + a - l
	print(min(p, q, k))


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	for i in range(n):
		if A[i] == 1:
			low = i
		if A[i] == n:
			high = i
	gap = abs(high - low)
	if high > low:
		res1 = low + 1 + gap
		res2 = n - high + gap
		res3 = low + 1 + n - high
	if high < low:
		(high, low) = (low, high)
		res1 = low + 1 + gap
		res2 = n - high + gap
		res3 = low + 1 + n - high
	print(min(res1, res2, res3))


t = int(input())
for i in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	c = b.index(min(b))
	d = b.index(max(b))
	y = min(min(c + 1, d + 1, a - c, a - d) + abs(c - d), a + min(c, d) - max(c, d) + 1)
	print(y)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	mn = l.index(min(l))
	mx = l.index(max(l))
	if mn < mx:
		if mn + 1 < n - mx:
			c += mn + 1
			l = l[mn + 1:]
			mx = l.index(max(l))
			c += min(mx + 1, len(l) - mx)
		else:
			c += n - mx
			l = l[:mx]
			mn = l.index(min(l))
			c += min(mn + 1, len(l) - mn)
	elif mx + 1 < n - mn:
		c += mx + 1
		l = l[mx + 1:]
		mn = l.index(min(l))
		c += min(mn + 1, len(l) - mn)
	else:
		c += n - mn
		l = l[:mn]
		mx = l.index(max(l))
		c += min(mx + 1, len(l) - mx)
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxind = a.index(max(a))
	minind = a.index(min(a))
	key = 0
	l = min(minind, maxind, n - 1 - minind, n - 1 - maxind)
	if l == minind or l == maxind:
		del a[0:l + 1]
		if l == minind:
			maxind1 = a.index(max(a))
			print(l + 1 + min(maxind1, len(a) - 1 - maxind1) + 1)
		if l == maxind:
			minind1 = a.index(min(a))
			print(l + 1 + min(minind1, len(a) - 1 - minind1) + 1)
	else:
		del a[n - 1 - l:n]
		if l == n - 1 - minind:
			maxind1 = a.index(max(a))
			print(l + 1 + min(maxind1, len(a) - 1 - maxind1) + 1)
		if l == n - 1 - maxind:
			minind1 = a.index(min(a))
			print(l + 1 + min(minind1, len(a) - 1 - minind1) + 1)


t = int(input())
for T in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxA = max(a)
	maxIndex = a.index(maxA)
	minA = min(a)
	minIndex = a.index(minA)
	answer = n
	if maxIndex < minIndex:
		answer = min(minIndex + 1, maxIndex + 1 + n - minIndex)
		answer = min(answer, n - maxIndex)
	else:
		answer = min(maxIndex + 1, minIndex + 1 + n - maxIndex)
		answer = min(answer, n - minIndex)
	print(answer)


import sys, os, io
from sys import stdin
from types import GeneratorType
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	c = dict(Counter(l))
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieveForSmallestPrimeFactor():
	MAXN = 100001
	spf = [0 for i in range(MAXN)]
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, math.ceil(math.sqrt(MAXN))):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
	return spf

def getPrimeFactorizationLOGN(x):
	spf = sieveForSmallestPrimeFactor()
	ret = list()
	while x != 1:
		ret.append(spf[x])
		x = x // spf[x]
	return ret

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def si():
	return input()

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = ii()
	l = li()
	mxind = l.index(max(l))
	minind = l.index(min(l))
	ans = float('inf')
	ans = min(ans, max(1 + mxind, minind + 1), max(n - mxind, n - minind), mxind + 1 + n - minind, minind + 1 + n - mxind)
	print(ans)
t = 1
t = ii()
for _ in range(t):
	solve()


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mini = l.index(min(l))
	maxi = l.index(max(l))
	if mini > maxi:
		(mini, maxi) = (maxi, mini)
	print(min(mini + 1 + n - maxi, maxi + 1, n - mini))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	max_ = max(a)
	min_ = min(a)
	ans = float('inf')
	ans = min(ans, a.index(max_) + 1 + n - a.index(min_))
	ans = min(ans, a.index(min_) + 1 + n - a.index(max_))
	ans = min(ans, max(a.index(max_) + 1, a.index(min_) + 1))
	ans = min(ans, max(n - a.index(max_), n - a.index(min_)))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	min1 = a.index(min(a))
	max1 = a.index(max(a))
	b = a[::-1]
	min2 = b.index(min(b))
	max2 = b.index(max(b))
	x = max(min1, max1) + 1
	y = max(min2, max2) + 1
	if min1 < max1:
		z = min1 + 1
		z += n - max1
	else:
		z = max1 + 1
		z += n - min1
	print(min(x, y, z))


import math
t = int(input())
for _ in range(t):
	n = input()
	arr = list(map(int, input().split()))
	(STONE_MIN, STONE_MAX) = (min(arr), max(arr))
	(MI, MA) = (arr.index(STONE_MIN), arr.index(STONE_MAX))
	LEFT = max(MI, MA) + 1
	RIGHT = len(arr) - min(MI, MA)
	EDGES = len(arr) - max(MI, MA) + min(MI, MA) + 1
	print(min(LEFT, RIGHT, EDGES))


import sys, os, io
from sys import stdin, stdout
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import bisect
import math
input = stdin.readline
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mi():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')

def power_set(L):
	cardinality = len(L)
	n = 2 ** cardinality
	powerset = []
	for i in range(n):
		a = bin(i)[2:]
		subset = []
		for j in range(len(a)):
			if a[-j - 1] == '1':
				subset.append(L[j])
		powerset.append(subset)
	powerset_orderred = []
	for k in range(cardinality + 1):
		for w in powerset:
			if len(w) == k:
				powerset_orderred.append(w)
	return powerset_orderred

def fastPlrintNextLines(a):
	print('\n'.join(map(str, a)))

def sortByFirstAndSecond(A):
	A = sorted(A, key=lambda x: x[0])
	A = sorted(A, key=lambda x: x[1])
	return list(A)

def dfs(node, vis, adj, lis):
	lis.append(node)
	vis[node] = 1
	for i in adj[node]:
		if vis[i] == 0:
			dfs(i, vis, adj, lis)

def dfsOfGraph(n, adj):
	lis = []
	vis = [0] * (n + 1)
	for i in range(1, n + 1):
		if vis[i] == 0:
			dfs(i, vis, adj, lis)
	return lis

def bfsOfGraph(n, adj):
	vis = [0] * (n + 1)
	lis = []
	for i in range(1, n + 1):
		if vis[i] == 0:
			queue = [i]
			vis[i] = 1
			while len(queue) > 0:
				node = queue.pop(0)
				lis.append(node)
				for j in adj[node]:
					if vis[j] == 0:
						queue.append(j)
						vis[j] = 1
	return lis
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = ii()
	l = li()
	mn = l.index(min(l))
	mx = l.index(max(l))
	a = min(mn, mx)
	b = max(mn, mx)
	ans = min([a + 1 + n - b, b + 1, n - a])
	return ans
t = ii()
for _ in range(t):
	res = solve()
	print(res)


for w in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ma = max(a)
	mi = min(a)
	i1 = a.index(ma)
	i2 = a.index(mi)
	ans = min(max(i1, i2) + 1, n - min(i1, i2), i2 + 1 + n - i1, i1 + 1 + n - i2)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = min(arr)
	mx = max(arr)
	mni = arr.index(mn)
	mxi = arr.index(mx)
	if mxi > mni:
		print(min(mxi + 1, n - mni, mni + 1 + n - mxi))
	else:
		print(min(mni + 1, n - mxi, mxi + 1 + n - mni))


from operator import index
nbTest = int(input())
for _ in range(nbTest):
	n = int(input())
	a = [int(i) for i in input().split()]
	indexMin = a.index(1)
	indexMax = a.index(n)
	distMin = min(indexMin + 1, n - indexMin)
	distMax = min(indexMax + 1, n - indexMax)
	miniDist = min(distMin, distMax)
	distBetween = abs(indexMin - indexMax)
	if miniDist == distMin:
		print(min(miniDist + distBetween, miniDist + distMax))
	else:
		print(min(miniDist + distBetween, miniDist + distMin))


def destory(l):
	(mi, ma) = (l.index(min(l)), l.index(max(l)))
	(mim, mam) = (min(ma, mi), max(ma, mi))
	le = len(l)
	first = mam + 1
	second = le - mim
	third = le - mim + mam + 1
	fourth = le - mam + mim + 1
	return min(first, second, third, fourth)
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(destory(l))


t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	minNum = -1
	maxNum = -1
	minIndex = -1
	maxIndex = 1
	for i in range(n):
		if arr[i] > maxNum:
			maxNum = arr[i]
			maxIndex = i
		if arr[i] < minNum or minNum == -1:
			minNum = arr[i]
			minIndex = i
	minIndex2 = n - minIndex - 1
	maxIndex2 = n - maxIndex - 1
	list2 = []
	if maxIndex > minIndex:
		list2.append(maxIndex + 1)
		list2.append(minIndex + maxIndex2 + 2)
		list2.append(minIndex2 + 1)
	if minIndex > maxIndex:
		list2.append(minIndex + 1)
		list2.append(maxIndex + minIndex2 + 2)
		list2.append(maxIndex2 + 1)
	print(min(list2))


n = int(input())
for i in range(n):
	c = int(input())
	a = list(map(int, input().split()))
	b = max(a)
	m = min(a)
	x = a.index(b)
	y = a.index(m)
	x1 = min(c - x - 1, x)
	y1 = min(c - y - 1, y)
	print(min(x1 + abs(x - y) + 1, y1 + abs(x - y) + 1, x1 + y1 + 2))


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	mi = min(arr)
	mx = max(arr)
	ind1 = arr.index(mi)
	ind2 = arr.index(mx)
	one = max(ind1, ind2) + 1
	two = n - min(ind1, ind2)
	three = ind1 + 1 + n - ind2
	four = ind2 + 1 + n - ind1
	ans = min(one, two, three, four)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	mi = min(ar)
	ma = max(ar)
	imi = ar.index(mi)
	ima = ar.index(ma)
	zmi = min(imi + 1, n - imi)
	zma = min(ima + 1, n - ima)
	if zmi <= zma:
		k = zmi
		g = zma
	else:
		k = zma
		g = zmi
	ans = k
	if abs(imi - ima) > g:
		ans += g
	else:
		ans += abs(imi - ima)
	print(ans)


import sys
inputlines = sys.stdin.readlines()
number_of_testcase = int(inputlines[0])

def find_index_of_min_and_max_element(input_array, length):
	min_index = 0
	max_index = 0
	max = input_array[0]
	min = input_array[0]
	for i in range(1, length):
		if input_array[i] > max:
			max_index = i
			max = input_array[max_index]
		if input_array[i] < min:
			min_index = i
			min = input_array[min_index]
	return (max_index, min_index)
for i in range(number_of_testcase):
	number_of_elements = int(inputlines[2 * i + 1])
	elements = list(map(int, inputlines[2 * i + 2].split(' ')))
	(max_index, min_index) = find_index_of_min_and_max_element(elements, number_of_elements)
	positons_list = []
	positons_list.extend([max_index + 1, number_of_elements - max_index, min_index + 1, number_of_elements - min_index])
	(position_maxindex, position_minindex) = find_index_of_min_and_max_element(positons_list, 4)
	similar_index_to_positon_minindex = (position_minindex + 2) % 4
	positons_list[similar_index_to_positon_minindex] -= positons_list[position_minindex]
	first_deletion = positons_list.pop(position_minindex)
	second_deletion = min(positons_list)
	print(first_deletion + second_deletion)


tests = int(input())
for test in range(tests):
	total = int(input())
	stones = [int(i) for i in input().split()]
	counter = 0
	while 1 in stones and total in stones:
		small = stones.index(1)
		big = stones.index(total)
		right_small = len(stones) - 1 - small
		right_big = len(stones) - 1 - big
		answer = min(small, big, right_small, right_big)
		if answer == right_small or answer == right_big:
			stones = stones[:-1]
		elif answer == small or answer == big:
			stones = stones[1:]
		counter += 1
	if 1 not in stones:
		big = stones.index(total)
		right_big = len(stones) - 1 - big
		counter += min(big, right_big) + 1
	else:
		small = stones.index(1)
		right_small = len(stones) - 1 - small
		counter += min(small, right_small) + 1
	print(counter)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	(maxi, mini) = (0, 0)
	for i in range(n):
		if arr[i] > arr[maxi]:
			maxi = i
		if arr[i] < arr[mini]:
			mini = i
	max1 = max(maxi, mini)
	min1 = min(maxi, mini)
	result = min(max1 + 1, min1 + (n - max1) + 1, n - min1)
	print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	(m, M) = (min(a), max(a))
	(mi, MI) = (a.index(m), a.index(M))
	(i, I) = (min(mi, MI), max(mi, MI))
	ans = min(I + 1, n - i, i + 1 + n - I)
	print(ans)


for b in [*open(0)][2::2]:
	a = list(map(int, b.split()))
	(x, y) = sorted([a.index(min(a)), a.index(max(a))])
	print(len(a) - max(x, y - x - 1, len(a) - y - 1))


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	left_max = arr.index(max(arr)) + 1
	left_min = arr.index(min(arr)) + 1
	right_max = n - left_max + 1
	right_min = n - left_min + 1
	ans1 = max(left_max, left_min)
	ans2 = max(right_max, right_min)
	ans3 = left_max + right_min
	ans4 = left_min + right_max
	ans = min(ans1, ans2, ans3, ans4)
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = max(l)
	b = min(l)
	c = l.index(a) + 1
	d = l.index(b) + 1
	e = n - c + 1
	f = n - d + 1
	l = min(c + f, d + e, max(c, d), max(e, f))
	print(l)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(mi, mx) = (min(l), max(l))
	l2 = sorted(l)
	(p1, p2) = (l.index(mi) + 1, l.index(mx) + 1)
	(p3, p4) = (n - p1 + 1, n - p2 + 1)
	ll = max(p1, p2)
	lr = max(p3, p4)
	lm = min(p1, p3) + min(p2, p4)
	print(min(lm, ll, lr))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p1 = a.index(1)
	p2 = a.index(n)
	(px, py) = (min(p1, p2), max(p1, p2))
	ans = min(py + 1, n - px, n - py + px + 1)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	maxim = max(arr[:])
	minim = min(arr[:])
	ind1 = arr.index(maxim)
	ind2 = arr.index(minim)
	print(min(max(ind1, ind2) + 1, ind1 + 1 + (n - ind2), ind2 + 1 + n - ind1, max(n - ind1, n - ind2)))


for t in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	m = min(x)
	M = max(x)
	i = x.index(m) + 1
	j = x.index(M) + 1
	print(min(max(i, j), max(len(x) + 1 - j, len(x) + 1 - i), i + len(x) + 1 - j, j + len(x) + 1 - i))


import sys
import collections
import math
inf = sys.maxsize

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = get_array()
	mn = min(arr)
	mx = max(arr)
	idx1 = idx2 = -1
	for i in range(n):
		if arr[i] == mn:
			idx1 = i
		if arr[i] == mx:
			idx2 = i
	print(min(max(idx1 + 1, idx2 + 1), min(max(n - idx1, n - idx2), min(idx1 + n - idx2 + 1, idx2 + n - idx1 + 1))))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mx = max(arr)
	mn = min(arr)
	ind1 = arr.index(mx)
	ind2 = arr.index(mn)
	res = float('inf')
	res = min(res, 1 + max(ind1, ind2))
	res = min(res, max(n - ind1, n - ind2))
	res = min(res, min(ind1 + 1 + n - ind2, ind2 + 1 + n - ind1))
	print(res)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	minn = l.index(1) + 1
	maxx = l.index(n) + 1
	print(min(max(minn, maxx), min(minn, maxx) + (n - max(minn, maxx) + 1), n - min(minn, maxx) + 1))


t = int(input(''))
for i in range(t):
	n = int(input(''))
	a = list(map(int, input('').split()))
	pos1 = a.index(max(a))
	pos2 = a.index(min(a))
	print(min(max(pos2, pos1) + 1, n - min(pos1, pos2), min(pos1, pos2) + 1 + (n - max(pos1, pos2))))


for _ in range(int(input())):
	n = int(input())
	ans = 0
	array = list(map(int, input().split()))
	minIdx = array.index(min(array))
	maxIdx = array.index(max(array))
	idxArray = [minIdx + 1, maxIdx + 1, n - minIdx, n - maxIdx]
	curMin = min(idxArray)
	curIdx = idxArray.index(curMin)
	ans += curMin
	if curIdx == 0:
		array = array[minIdx + 1:]
		maxIdx = array.index(max(array))
		ans += min([maxIdx + 1, len(array) - maxIdx])
	elif curIdx == 1:
		array = array[maxIdx + 1:]
		minIdx = array.index(min(array))
		ans += min([minIdx + 1, len(array) - minIdx])
	elif curIdx == 2:
		array = array[:minIdx]
		maxIdx = array.index(max(array))
		ans += min([maxIdx + 1, len(array) - maxIdx])
	else:
		array = array[:maxIdx]
		minIdx = array.index(min(array))
		ans += min([minIdx + 1, len(array) - minIdx])
	print(ans)