You want to build a temple for snakes. The temple will be built on a mountain range, which can be thought of as n blocks, where height of i-th block is given by hi. The temple will be made on a consecutive section of the blocks and its height should start from 1 and increase by exactly 1 each time till some height and then decrease by exactly 1 each time to height 1, 
i.e. a consecutive section of 1, 2, 3, .. x-1, x, x-1, x-2, .., 1 can correspond to a temple. Also, heights of all the blocks other than of the temple should have zero height, so that the temple is visible to people who view it from the left side or right side.
You want to construct a temple. For that, you can reduce the heights of some of the blocks. In a single operation, you can reduce the height of a block by 1 unit. Find out minimum number of operations required to build a temple.

-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains an integer n.
The next line contains n integers, where the i-th integer denotes hi

-----Output-----
For each test case, output a new line with an integer corresponding to the answer of that testcase.

-----Constraints-----
- 1 ≤ T ≤ 10
- 2 ≤ n ≤ 105
- 1 ≤ hi ≤ 109

-----Example-----
Input
3
3
1 2 1
4
1 1 2 1
5
1 2 6 2 1

Output
0
1
3

-----Explanation-----
Example 1. The entire mountain range is already a temple. So, there is no need to make any operation.
Example 2. If you reduce the height of the first block to 0. You get 0 1 2 1. The blocks 1, 2, 1 form a temple. So, the answer is 1.
Example 3. One possible temple can be 1 2 3 2 1. It requires 3 operations to build. This is the minimum amount you have to spend in order to build a temple.

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	sumi = sum(arr)
	prev = 1
	for i in range(n):
		arr[i] = min(arr[i], prev)
		prev = arr[i] + 1
	prev = 1
	for i in range(n - 1, -1, -1):
		arr[i] = min(arr[i], prev)
		prev = arr[i] + 1
	temp = 0
	for i in range(n):
		temp = max(temp, arr[i])
	print(sumi - temp * temp)


for t in range(int(input())):
	n = int(input())
	h = [0] + list(map(int, input().split())) + [0]
	L = [0] * (n + 2)
	for i in range(1, n + 1):
		L[i] = min(L[i - 1] + 1, h[i])
	R = [0] * (n + 2)
	for i in range(n, 0, -1):
		R[i] = min(R[i + 1] + 1, h[i])
	max_height = 0
	for i in range(1, n + 1):
		height = min(L[i], R[i])
		max_height = max(max_height, height)
	ans = sum(h) - max_height ** 2
	print(ans)


t = int(input())
for a in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sumarr = sum(arr)
	prev = 1
	for i in range(n):
		arr[i] = min(arr[i], prev)
		prev = arr[i] + 1
	prev = 1
	for i in range(n - 1, -1, -1):
		arr[i] = min(arr[i], prev)
		prev = arr[i] + 1
	temp = 0
	for i in range(n):
		temp = max(temp, arr[i])
	print(sumarr - temp * temp)


def temple(n, arr):
	s = sum(arr)
	left = [0] * n
	right = [0] * n
	left[0] = 1
	right[-1] = 1
	maxHeight = 0
	for i in range(1, n):
		left[i] = min(left[i - 1] + 1, arr[i])
	for i in range(n - 2, -1, -1):
		right[i] = min(right[i + 1] + 1, arr[i])
	for i in range(n):
		maxHeight = max(maxHeight, min(left[i], right[i]))
	print(s - maxHeight ** 2)
for _ in range(int(input())):
	temple(int(input()), list(map(int, input().split())))


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	h = list(map(int, input().split()))
	s = sum(h)
	h[0] = 1
	for i in range(1, n):
		if h[i] > h[i - 1] + 1:
			h[i] = h[i - 1] + 1
	h[n - 1] = 1
	for i in range(n - 2, -1, -1):
		if h[i] > h[i + 1] + 1:
			h[i] = h[i + 1] + 1
	print(s - max(h) ** 2)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	a = [0] * n
	b = [0] * n
	if arr[0] >= 1:
		a[0] += 1
		b[0] += 1
	if arr[-1] >= 1:
		a[-1] += 1
		b[-1] += 1
	for i in range(1, n):
		if arr[i] >= a[i - 1] + 1:
			a[i] = a[i - 1] + 1
		else:
			a[i] = arr[i]
	for j in range(n - 2, -1, -1):
		if arr[j] >= b[j + 1] + 1:
			b[j] = b[j + 1] + 1
		else:
			b[j] = arr[j]
	max = 0
	for i in range(n):
		tmp = min(a[i], b[i])
		if tmp > max:
			max = tmp
	total = sum(arr)
	s = max + max * (max - 1)
	print(total - s)


def ceil(num, arr, l, r):
	if l > r:
		return r + 1
	if arr[r] < num:
		return r + 1
	while l < r:
		m = (l + r) // 2
		if arr[m] >= num:
			r = m
		else:
			l = m + 1
	return r
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	l = [0 for i in range(n)]
	r = [0 for i in range(n)]
	l[0] = 1
	r[-1] = 1
	for i in range(1, n):
		l[i] = min(l[i - 1] + 1, arr[i])
		r[n - 1 - i] = min(r[n - i] + 1, arr[n - 1 - i])
	counter = -1
	for i in range(n):
		counter = max(counter, min(l[i], r[i]))
	print(sum(arr) - counter ** 2)


for __ in range(int(input())):
	N = int(input())
	arr = [0] + list(map(int, input().split())) + [0]
	L = [0] * (N + 2)
	R = [0] * (N + 2)
	for i in range(1, N + 1):
		L[i] = min(L[i - 1] + 1, arr[i])
	for i in range(N, 0, -1):
		R[i] = min(R[i + 1] + 1, arr[i])
	maxval = 0
	for i in range(1, N + 1):
		maxval = max(maxval, min(L[i], R[i]))
	print(sum(arr) - maxval ** 2)


t = int(input())
for _ in range(t):
	n = int(input())
	h = list(map(int, input().split()))
	(L, R) = ([0] * (n + 2), [0] * (n + 2))
	for i in range(1, n + 1):
		L[i] = min(L[i - 1] + 1, h[i - 1])
	for i in range(n, 0, -1):
		R[i] = min(R[i + 1] + 1, h[i - 1])
	mx = 0
	for i in range(1, n + 1):
		mx = max(mx, min(L[i], R[i]))
	ans = sum(h) - mx ** 2
	print(ans)


for _ in range(int(input().strip())):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	sum_arr = sum(arr)
	l = [0 for i in range(n)]
	count = 0
	for idx in range(n):
		if arr[idx] >= count + 1:
			count += 1
		else:
			count = arr[idx]
		l[idx] = count
	r = [0 for i in range(n)]
	count = 0
	for idx in range(n - 1, -1, -1):
		if arr[idx] >= count + 1:
			count += 1
		else:
			count = arr[idx]
		r[idx] = count
	optimum = [min(l[i], r[i]) for i in range(n)]
	max_m = max(optimum)
	print(sum_arr - max_m ** 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [0] + [int(i) for i in input().split()] + [0]
	l = [0] * (n + 2)
	r = [0] * (n + 2)
	for i in range(1, n + 1):
		l[i] = min(l[i - 1] + 1, a[i])
	for i in range(n, 0, -1):
		r[i] = min(r[i + 1] + 1, a[i])
	max_h = 0
	for i in range(1, n + 1):
		height = min(l[i], r[i])
		max_h = max(max_h, height)
	ans = sum(a) - max_h ** 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	a[0] = 1
	for i in range(1, len(a)):
		if a[i] > a[i - 1] + 1:
			a[i] = a[i - 1] + 1
	a[n - 1] = 1
	for i in range(len(a) - 2, -1, -1):
		if a[i] > a[i + 1] + 1:
			a[i] = a[i + 1] + 1
	print(s - max(a) ** 2)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [0] + arr + [0]
	L = [0] * (n + 2)
	for i in range(1, n + 1):
		L[i] = min(L[i - 1] + 1, arr[i])
	R = [0] * (n + 2)
	for i in range(n, 0, -1):
		R[i] = min(R[i + 1] + 1, arr[i])
	ma = 0
	for i in range(1, n + 1):
		l = min(L[i], R[i])
		ma = max(ma, l)
	ans = sum(arr) - ma ** 2
	print(ans)


t = int(input())
for I in range(t):
	n = int(input())
	a = [0] + [int(i) for i in input().split()] + [0]
	L = [0] * (n + 2)
	R = [0] * (n + 2)
	for i in range(1, n + 1):
		L[i] = min(1 + L[i - 1], a[i])
	for i in range(n, 0, -1):
		R[i] = min(R[i + 1] + 1, a[i])
	max_height = 0
	for i in range(1, n + 1):
		ht = min(L[i], R[i])
		max_height = max(max_height, ht)
	ans = sum(a) - max_height ** 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ls = [0] + list(map(int, input().split())) + [0]
	r = [0] * (n + 2)
	l = [0] * (n + 2)
	for i in range(1, n + 1):
		l[i] = min(l[i - 1] + 1, ls[i])
	for i in range(n, 0, -1):
		r[i] = min(r[i + 1] + 1, ls[i])
	Max = 0
	for i in range(1, n + 1):
		Max = max(Max, min(l[i], r[i]))
	print(sum(ls) - Max ** 2)


for T in range(int(input())):
	n = int(input())
	arr = [0] + [int(x) for x in input().split()] + [0]
	L = [0] * (n + 2)
	R = [0] * (n + 2)
	k = 0
	for i in range(1, n + 1):
		L[i] = min(L[i - 1] + 1, arr[i])
	for i in range(n, 0, -1):
		R[i] = min(R[i + 1] + 1, arr[i])
	maxHeight = 0
	for i in range(1, n + 1):
		height = min(L[i], R[i])
		maxHeight = max(maxHeight, height)
	print(sum(arr) - maxHeight ** 2)


for _ in range(int(input())):
	n = int(input())
	h = [0] + [int(i) for i in input().split()] + [0]
	l = [0] * (n + 2)
	r = [0] * (n + 2)
	for i in range(1, n + 1):
		l[i] = min(l[i - 1] + 1, h[i])
	for i in range(n, 0, -1):
		r[i] = min(r[i + 1] + 1, h[i])
	maxi = 0
	for i in range(n):
		curr = min(l[i], r[i])
		maxi = max(maxi, curr)
	print(sum(h) - maxi * maxi)


for tests in range(int(input())):
	n = int(input())
	blockHeights = [int(blockHeight) for blockHeight in input().split()]
	sumOfBlockHeightsBefore = sum(blockHeights)
	blockHeights[0] = blockHeights[n - 1] = 1
	for i in range(1, n):
		if blockHeights[i] > blockHeights[i - 1] + 1:
			blockHeights[i] = blockHeights[i - 1] + 1
	for i in range(n - 2, -1, -1):
		if blockHeights[i] > blockHeights[i + 1] + 1:
			blockHeights[i] = blockHeights[i + 1] + 1
	heightOfTallestTemple = max(blockHeights)
	s = heightOfTallestTemple ** 2
	noOfOperations = sumOfBlockHeightsBefore - s
	print(noOfOperations)


for t in range(int(input())):
	n = int(input())
	ht_array = [int(e) for e in input().split()]
	sum_of_array = sum(ht_array)
	ht_array[0] = 1
	for i in range(1, n):
		if ht_array[i] > ht_array[i - 1] + 1:
			ht_array[i] = ht_array[i - 1] + 1
	ht_array[n - 1] = 1
	for i in range(n - 2, -1, -1):
		if ht_array[i] > ht_array[i + 1] + 1:
			ht_array[i] = ht_array[i + 1] + 1
	max_height = max(ht_array)
	moves = sum_of_array - max_height * max_height
	print(moves)


def left(h, m, n):
	k = 0
	L = [False] * n
	for i in range(n):
		if h[i] >= k + 1:
			k += 1
			if k == m:
				L[i] = True
				k -= 1
		else:
			k = h[i]
	return L

def right(h, m, n):
	k = 0
	R = [False] * n
	for i in range(n - 1, -1, -1):
		if h[i] >= k + 1:
			k += 1
			if k == m:
				R[i] = True
				k -= 1
		else:
			k = h[i]
	return R

def comp(h, m, n):
	l = left(h, m, n)
	r = right(h, m, n)
	for i in range(n):
		if l[i] == True and r[i] == True:
			return True
	return False
for _ in range(int(input().strip())):
	blocks = int(input().strip())
	blocks_height = list(map(int, input().strip().split()))
	su = sum(blocks_height)
	start = 1
	end = blocks + 1
	while start < end - 1:
		mid = (start + end) // 2
		if comp(blocks_height, mid, blocks):
			start = mid
		else:
			end = mid
	print(su - start * start)