You are given $n$ sticks with positive integral length $a_1, a_2, \ldots, a_n$.

You can perform the following operation any number of times (possibly zero):

choose one stick, then either increase or decrease its length by $1$. After each operation, all sticks should have positive lengths.

What is the minimum number of operations that you have to perform such that it is possible to select three of the $n$ sticks and use them without breaking to form an equilateral triangle?

An equilateral triangle is a triangle where all of its three sides have the same length.


-----Input-----

The first line of the input contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($3 \le n \le 300$) — the number of sticks.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the lengths of the sticks.

It is guaranteed that the sum of $n$ over all test cases does not exceed $300$.


-----Output-----

For each test case, print one integer on a single line — the minimum number of operations to be made.


-----Examples-----

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


-----Note-----

In the first test case, you can increase the length of the first stick by $1$, then decrease the length of the third stick by $1$. In total, you perform $2$ operations, such that the three sticks form an equilateral triangle of side length $2$.

In the fourth test case, you can decrease the length of the seventh stick by $1$. An equilateral triangle of side length $1$ can be selected and formed by the second, fourth, and seventh sticks.

Source: codeforces | Difficulty: EASY
Tags: Complete search

Sorting

Greedy algorithms


Solutions:
import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	arr = inlt()
	arr.sort()
	min_dif = 1000000009
	for i in range(1, n - 1):
		min_dif = min(min_dif, arr[i + 1] - arr[i - 1])
	print(min_dif)


try:
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split(' ')))
		l.sort()
		for i in range(1, len(l) - 1):
			z = l[i] - l[i - 1] + (l[i + 1] - l[i])
			if i == 1:
				mn = z
			else:
				mn = min(mn, z)
		print(mn)
except:
	pass


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
i = inp()
ans = []
for _ in range(i):
	x = inp()
	l = inlt()
	l = sorted(l)
	res = float('inf')
	for idx in range(2, x):
		res = min(res, l[idx - 1] - l[idx - 2] + l[idx] - l[idx - 1])
	ans.append(res)
for row in ans:
	print(row)


t = int(input())
for _ in range(t):
	n = int(input())
	args = input().split()
	args = [int(e) for e in args]
	args.sort()
	mn = 10000000000.0
	for i in range(1, len(args) - 1):
		dif = args[i + 1] - args[i - 1]
		mn = dif if dif < mn else mn
	print(mn)


test_case = int(input())
for t in range(test_case):
	n = int(input())
	l = list(map(int, input().strip().split()))
	l.sort()
	l_ = []
	for i in range(1, n - 1):
		l_.append(l[i + 1] - l[i - 1])
	print(min(l_))


t = int(input())
for testcase in range(t):
	n = int(input())
	sticks = [int(x) for x in input().split()]
	sticks.sort()
	best = sticks[2] - sticks[0]
	for i in range(0, n - 2):
		diff = sticks[i + 2] - sticks[i]
		if diff < best:
			best = diff
	print(best)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	k = 10 ** 20
	arr.sort()
	for i in range(n - 2):
		x = abs(arr[i] - arr[i + 1])
		x += abs(arr[i + 1] - arr[i + 2])
		k = min(k, x)
	print(k)


t = int(input())
for _ in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	fre = dict()
	for val in arr:
		fre[val] = fre.get(val, 0) + 1
	arr.sort()
	mnOps = 10000000000.0
	for i in range(0, len(arr)):
		if i + 3 <= len(arr):
			f = arr[i]
			s = arr[i + 1]
			t = arr[i + 2]
			op = s - f + t - s
			mnOps = min(op, mnOps)
	print(mnOps)


n = int(input())
for i in range(n):
	num = int(input())
	string = str(input())
	arr = list(string.split(' '))
	arr = [int(x) for x in arr]
	arr.sort()
	moves = 10 ** 9
	j = 0
	while j + 2 < num:
		s = arr[j + 2] - arr[j]
		if s < moves:
			moves = s
		j += 1
	print(moves)


for jj in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	arr.sort()
	ans = 10 ** 9
	for i in range(1, n - 1):
		ans = min(ans, arr[i] - arr[i - 1] + arr[i + 1] - arr[i])
	print(ans)


t = int(input())
while t != 0:
	n = int(input())
	A = [int(a) for a in input().split()]
	A.sort()
	m = A[2] - A[0]
	for i in range(n - 2):
		if A[i + 2] - A[i] < m:
			m = A[i + 2] - A[i]
	print(m)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(sep=' ')))
	arr.sort()
	mini = arr[2] - arr[0]
	for i in range(1, n - 2):
		if arr[i + 2] - arr[i] < mini:
			mini = arr[i + 2] - arr[i]
	print(mini)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = 10 ** 20
	for i in range(1, n - 1):
		ans = min(ans, l[i + 1] - l[i - 1])
	print(ans)


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	arr = []
	cost = -1
	for idx in range(n - 2):
		c = abs(a[idx + 2] - a[idx + 1]) + abs(a[idx] - a[idx + 1])
		if cost == -1:
			cost = c
			continue
		if cost > c:
			cost = c
	print(cost)
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	x = int(input())
	y = list(map(int, input().split()))
	z = list(sorted(y))
	(l, m, r) = (0, 1, 2)
	count = z[-1]
	while r <= x - 1:
		total = 0
		total += z[m] - z[l]
		total += z[r] - z[m]
		count = min(count, total)
		l += 1
		m += 1
		r += 1
	print(count)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = math.inf
	for i in range(1, n - 1):
		s = min(s, abs(a[i + 1] - a[i]) + abs(a[i] - a[i - 1]))
	print(s)


def cal(a=[]):
	ans = a[2] - a[0]
	return ans
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = cal(a[0:3])
	for i in range(n - 2):
		ans = min(ans, cal(a[i:i + 3]))
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	k = [int(i) for i in input().split(' ')]
	k.sort()
	i = 0
	mop = 1000000000
	for j in range(n - 2):
		op1 = k[j + 1] - k[j]
		op2 = k[j + 2] - k[j + 1]
		mop = min(mop, op1 + op2)
	print(mop)


n = int(input())
for i in range(n):
	l = int(input())
	A = list(map(int, input().split()))
	A.sort()
	ans_lis = []
	for j in range(len(A)):
		x = A[j:j + 3]
		if len(x) == 3:
			summ = x[2] - x[1] + (x[1] - x[0])
			ans_lis.append(summ)
	print(min(ans_lis))


for _ in range(int(input())):
	n = int(input())
	x = sorted(list(map(int, input().split())))
	q = -1
	if n >= 4:
		for (i, e) in enumerate(x[:-2]):
			if i <= n - 2:
				tmp = x[i:i + 3]
			else:
				tmp = x[i - 1:]
				break
			dif = sum([tmp[1] - tmp[0], tmp[2] - tmp[1]])
			if q < 0:
				q = dif
			elif dif <= q:
				q = dif
			else:
				pass
	else:
		q = sum([x[1] - x[0], x[2] - x[1]])
	print(q)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = 0
	an = []
	for i in range(0, n - 2):
		ans = l[i + 2] - l[i + 1] + (l[i + 1] - l[i])
		an.append(ans)
	print(min(an))
	t = t - 1


for x in range(int(input())):
	n = int(input())
	res = 1000000000.0
	arr = list(map(int, input().split()))
	arr.sort()
	for x in range(n - 2):
		res = min(res, arr[x + 2] - arr[x + 1] + arr[x + 1] - arr[x])
	print(res)


num_tests = int(input())
for i in range(num_tests):
	num_sticks = int(input())
	sticks = sorted(list((int(val) for val in input().split(' '))))
	min_diff = sticks[2] - sticks[0]
	for j in range(2, num_sticks):
		min_diff = min(min_diff, sticks[j] - sticks[j - 2])
	print(min_diff)


for testcase in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	res = 10 ** 9 + 7
	for i in range(2, n):
		res = min(res, abs(a[i - 1] - a[i - 2]) + abs(a[i - 1] - a[i]))
	print(res)


n = int(input())
a = []
for i in range(n):
	x = int(input())
	s = str(input()).split()
	s = map(int, s)
	k = 10 ** 9
	s = sorted(s)
	for i in range(0, len(s) - 2):
		k = min(k, s[i + 2] - s[i])
	a.append(k)
for j in range(len(a)):
	print(a[j])


for _ in range(int(input())):
	num = int(input())
	ks = list(map(int, input().split()))
	ks = sorted(ks)
	res = 10 ** 9
	for i in range(1, num - 1):
		res = min(res, ks[i + 1] - ks[i - 1])
	print(res)


n = int(input())
for j in range(n):
	s = int(input())
	a = list(map(int, input().split()))
	a.sort()
	(l, r) = (0, 2)
	res = a[-1] - a[0]
	while r < s:
		res = min(a[r] - a[l], res)
		l += 1
		r += 1
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	l1.sort()
	d = 12435465768
	for i in range(len(l1) - 2):
		d = min(d, l1[i + 2] - l1[i])
	print(d)


n = int(input())
for i in range(n):
	l = int(input())
	a = input()
	g = a.split()
	for i in range(len(g)):
		g[i] = int(g[i])
	g.sort()
	ans = 9999999999999999999999999999999999999999999999999
	for i in range(1, len(g) - 1):
		if g[i - 1] == g[i] == g[i + 1]:
			ans = min(ans, 0)
		elif g[i - 1] == g[i]:
			ans = min(ans, g[i + 1] - g[i])
		elif g[i] == g[i + 1]:
			ans = min(ans, g[i] - g[i - 1])
		else:
			ans = min(ans, g[i + 1] - g[i] + g[i] - g[i - 1])
	print(ans)


t = int(input())
for ii in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = 999999999999
	for i in range(n - 2):
		ans = min(ans, abs(a[i] - a[i + 1]) + abs(a[i + 2] - a[i + 1]))
	print(ans)


for s in [*open(0)][2::2]:
	a = sorted(map(int, s.split()))
	print(min((y - x for (x, y) in zip(a, a[2:]))))


t = int(input(''))
for z in range(t):
	n = int(input(''))
	a = list(map(int, input('').split()))[:n]
	a = sorted(a)
	c = 10000000000
	k = 0
	for i in range(n):
		if i == 0:
			if c > abs(a[1] - a[0]) + abs(a[2] - a[0]):
				c = abs(a[1] - a[0]) + abs(a[2] - a[0])
		elif i == n - 1:
			if c > abs(a[n - 1] - a[n - 2]) + abs(a[n - 3] - a[n - 1]):
				c = abs(a[n - 1] - a[n - 2]) + abs(a[n - 3] - a[n - 1])
		elif c > abs(a[i - 1] - a[i]) + abs(a[i + 1] - a[i]):
			c = abs(a[i - 1] - a[i]) + abs(a[i + 1] - a[i])
	print(c)


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	ans = 2000000000.0
	for i in range(2, n):
		ans = min(ans, a[i] - a[i - 2])
	print(ans)
t = int(input())
for i in range(t):
	solve()


for str in [*open(0)][2::2]:
	p = sorted(map(int, str.split()))
	print(min((b - a for (a, b) in zip(p, p[2:]))))


def solve():
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	min_ans = 1000000000.0
	for i in range(1, n - 1):
		sum = arr[i] + arr[i - 1] + arr[i + 1]
		ans = arr[i + 1] - arr[i - 1]
		min_ans = min(min_ans, ans)
	return min_ans
t = int(input())
for i in range(t):
	print(solve())


for _ in range(int(input())):
	g = eval(input())
	p = sorted(map(int, input().split()))
	print(min([p[i + 1] - p[i - 1] for i in range(1, g - 1)]))


for _ in range(int(input())):
	nap = int(input())
	app = sorted(map(int, input().split()))
	rat = 10 ** 9
	for i in range(nap - 2):
		rat = min(rat, app[i + 2] - app[i])
	print(rat)


for t in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	print(min([a[i + 1] - a[i - 1] for i in range(1, n - 1)]))


for i in range(int(input())):
	n = int(input())
	s = sorted(list(map(int, input().split())))
	ans = 10000000000
	for i in range(2, n):
		ans = min(ans, s[i] - s[i - 1] + (s[i - 1] - s[i - 2]))
	print(ans)


def solve():
	for i in range(int(input())):
		n = int(input())
		a = [int(x) for x in input().split()]
		a.sort()
		l = []
		for i in range(1, n - 1):
			l.append(a[i] - a[i - 1] + (a[i + 1] - a[i]))
		print(min(l))
solve()


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = max(a)
	for i in range(2, n):
		ans = min(ans, a[i] - a[i - 2])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 10 ** 128
	arr.sort()
	for i in range(1, n - 1):
		ans = min(ans, arr[i] - arr[i - 1] + arr[i + 1] - arr[i])
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = input().split()
	arr = [int(i) for i in arr]
	arr.sort()
	minv = 1 << 34
	for i in range(n - 2):
		minv = min(minv, arr[i + 2] - arr[i])
	print(minv)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	arr.sort()
	ans = 10 ** 10
	for i in range(2, n):
		ans = min(ans, arr[i] - arr[i - 1] + arr[i] - arr[i - 2])
	for i in range(1, n - 1):
		ans = min(ans, arr[i] - arr[i - 1] + arr[i + 1] - arr[i])
	for i in range(0, n - 2):
		ans = min(ans, arr[i + 2] - arr[i + 1] + arr[i + 1] - arr[i])
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	arr.sort()
	ans = 1000000000000000
	for i in range(1, n - 1):
		ans = min(ans, abs(arr[i] - arr[i - 1]) + abs(arr[i] - arr[i + 1]))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	m = float('inf')
	for i in range(n - 2):
		if A[i + 2] - A[i] < m:
			m = A[i + 2] - A[i]
	print(m)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 3 * 10 ** 9
	for i in range(1, n - 1):
		ans = min(ans, -arr[i - 1] + arr[i + 1])
	return ans
for _ in range(int(input())):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 10 ** 9
	l.sort()
	for x in range(n - 2):
		sm = 0
		sm += l[x + 1] - l[x]
		sm += l[x + 2] - l[x + 1]
		ans = min(ans, sm)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	L.sort()
	ans = abs(L[0] - L[1]) + abs(L[1] - L[2])
	for i in range(1, n - 1):
		ans = min(ans, abs(L[i] - L[i - 1]) + abs(L[i] - L[i + 1]))
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	d = a[0] - a[1] + (a[1] - a[2])
	for i in range(3, n):
		d = min(a[i - 2] - a[i - 1] + (a[i - 1] - a[i]), d)
	print(d)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 1000000000
	for i in range(2, n):
		ans = min(ans, arr[i] - arr[i - 1] + arr[i - 1] - arr[i - 2])
	print(ans)


t = int(input())
for q in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 1000000000
	a.sort()
	for i in range(n - 2):
		ans = min(ans, abs(a[i + 1] - a[i]) + abs(a[i + 1] - a[i + 2]))
	print(ans)


import sys
import math

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

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

def test_ok(ok):
	print('YES' if ok else 'NO')

def main():
	for _ in range(int(input())):
		n = int(input())
		ls = get_ls()
		ls.sort()
		mn = 1000000000
		for i in range(1, n - 1):
			diff = ls[i + 1] - ls[i - 1]
			mn = min(mn, diff)
		print(mn)
main()


for _ in range(int(input())):
	n = int(input())
	arr = input().split(' ')
	arr = [int(i) for i in arr]
	arr.sort()
	ans = 2 ** 31
	for i in range(n - 2):
		ans = min(ans, arr[i + 2] - arr[i])
	print(ans)


n = int(input())
for i in range(n):
	l = int(input())
	m = list(map(int, input().split()))
	p1 = 0
	p2 = 2
	m.sort()
	minn = float('inf')
	while p2 < len(m):
		s = m[p1 + 1] - m[p1] + (m[p2] - m[p1 + 1])
		minn = min(s, minn)
		p1 += 1
		p2 += 1
	print(minn)


import sys
t = int(sys.stdin.readline())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	ans = 1000000000000000.0
	for j in range(n):
		ele = lst[j]
		c = 0
		if j - 2 >= 0:
			c += ele - lst[j - 2]
			c += ele - lst[j - 1]
			ans = min(ans, c)
		if j - 1 >= 0 and j + 1 < n:
			c = -lst[j - 1] + lst[j + 1]
			ans = min(ans, c)
		if j + 2 < n:
			c = lst[j + 2] - ele + lst[j + 1] - ele
			ans = min(ans, c)
	print(ans)


for i in range(int(input())):
	n = int(input())
	values = list(map(int, input().split()))
	values.sort()
	j = 1
	out = 2 * 1000000000
	while j <= n - 2:
		out = min(out, values[j + 1] - values[j - 1])
		j += 1
	print(out)


for _ in range(int(input())):
	n = int(input())
	ls = [int(i) for i in input().split(' ')]
	ls.sort()
	mn = ls[2] - ls[0]
	for i in range(2, n - 1):
		mn = min(mn, ls[i + 1] - ls[i - 1])
	print(mn)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	x = a[2] - a[0]
	for i in range(3, n):
		y = a[i] - a[i - 2]
		if y < x:
			x = y
	print(x)


import math

def gcd(a, b):
	while a % b != 0:
		aux = b
		b = a % b
		a = aux
	return b

def lcm(a, b):
	return a / gcd(a, b) * b

def solve(a, b):
	dif = math.inf
	for i in range(2, a):
		dif = min(b[i] - b[i - 2], dif)
	return dif
res = []
for _ in range(int(input())):
	a = int(input())
	b = sorted(list(map(int, input().split())))
	res.append(solve(a, b))
print(*res, sep='\n')


def f():
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = 999999999
	for i in range(len(l) - 2):
		x = l[i]
		y = l[i + 1]
		z = l[i + 2]
		res = y - x + (z - y)
		ans = min(res, ans)
	print(ans)
t = int(input())
while t:
	f()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	k = a[-1]
	for i in range(1, n - 1):
		if a[i + 1] - a[i - 1] < k:
			k = a[i + 1] - a[i - 1]
	print(k)


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
test = inp()
for k in range(test):
	n = inp()
	arr = inlt()
	arr.sort()
	minturn = math.inf
	for i in range(1, n - 1):
		minturn = min(minturn, arr[i] - arr[i - 1] + (arr[i + 1] - arr[i]))
	print(minturn)


for i in range(int(input())):
	leng = int(input())
	sticks = [int(x) for x in input().split()]
	sticks = sorted(sticks)
	mi = float('inf')
	for i in range(1, len(sticks) - 1):
		bam = abs(sticks[i] - sticks[i - 1]) + abs(sticks[i] - sticks[i + 1])
		if bam < mi:
			mi = bam
	print(mi)


y = int(input())
for z in range(y):
	trash = int(input())
	item = list(map(int, input().split()))
	if len(item) <= 2:
		print('0')
	item.sort()
	counter = abs(item[1] - item[0]) + abs(item[2] - item[1])
	for x in range(len(item) - 2):
		counter = max(min(abs(item[x + 1] - item[x]) + abs(item[x + 2] - item[x + 1]), counter), 0)
	print(counter)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	mn = None
	for i in range(1, len(arr) - 1):
		l = arr[i - 1]
		r = arr[i + 1]
		curr = r - l
		if mn is None:
			mn = curr
		else:
			mn = min(mn, curr)
	print(mn)


import sys
readline = sys.stdin.readline
INF = 1 << 60
t = int(readline())
for _ in range(t):
	ans = INF
	n = int(readline())
	A = sorted(map(int, readline().split()))
	for i in range(n - 2):
		AA = A[i:i + 3] * 2
		for j in range(3):
			(a, b, c) = AA[j:j + 3]
			tmp = abs(a - b) + abs(a - c)
			ans = min(ans, tmp)
	print(ans)


n = int(input())

def func(arr, n):
	arr.sort()
	res = 2 ** 32 + 1
	for i in range(1, n - 1, 1):
		res = min(res, arr[i] - arr[i - 1] + arr[i + 1] - arr[i])
	print(res)
for i in range(n):
	l = int(input())
	A = [int(i) for i in input().split()]
	func(A, l)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = input().split()
	for i in range(n):
		arr[i] = int(arr[i])
	arr.sort()
	ans = 1000000000
	for i in range(0, n - 2):
		if arr[i] == arr[i + 1] == arr[i + 2]:
			ans = min(0, ans)
		elif arr[i] == arr[i + 1]:
			ans = min(ans, arr[i + 2] - arr[i + 1])
		else:
			ans = min(ans, arr[i + 1] - arr[i] + arr[i + 2] - arr[i], arr[i + 1] - arr[i] + arr[i + 2] - arr[i + 1])
	print(ans)


N = int(input())
for _ in range(N):
	n = int(input())
	A = [int(x) for x in input().split(' ')]
	A = sorted(A)
	ans = max(A) * 3
	for i in range(0, n - 2):
		a1 = A[i + 1] - A[i]
		a2 = A[i + 2] - A[i + 1]
		ans = min(ans, a1 + a2)
	print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
arr = lambda : list(map(int, input().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 1000000007
for _ in range(inp()):
	n = inp()
	a = arr()
	a.sort()
	ans = 9999999999
	for i in range(1, n - 1):
		ans = min(ans, abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]))
	print(ans)


t = int(input())
while t != 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	minm = 10000000000.0
	for i in range(n):
		if i == 0:
			total = a[i + 1] - a[i] + (a[i + 2] - a[i])
		elif i == n - 1:
			total = a[i] - a[i - 1] + (a[i] - a[i - 2])
		else:
			total = a[i] - a[i - 1] + (a[i + 1] - a[i])
		minm = min(minm, total)
	print(minm)
	t -= 1


t = int(input())

def main():
	for q in range(t):
		M = 100000000000000
		NUM = int(input())
		ARRAY = list(map(int, input().split()))
		ARRAY = sorted(ARRAY)
		for i in range(NUM - 2):
			RESULT = abs(ARRAY[i + 2] - ARRAY[i + 1]) + abs(ARRAY[i + 1] - ARRAY[i])
			if RESULT < M:
				M = RESULT
		print(M)
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	minn = 10 ** 20
	a = 0
	b = 0
	c = 0
	for i in range(n - 2):
		a = arr[i]
		b = arr[i + 1]
		c = arr[i + 2]
		minn = min(minn, abs(a - b) + abs(a - c), abs(a - b) + abs(b - c), abs(a - c) + abs(b - c))
	print(minn)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = 10000000000
	for i in range(n - 2):
		a = l[i]
		b = l[i + 1]
		c = l[i + 2]
		x = b - a + c - b
		m = min(x, m)
	print(m)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	a = []
	for i in range(1, n):
		a.append(l[i] - l[i - 1])
	b = []
	for i in range(1, n - 1):
		b.append(a[i] + a[i - 1])
	print(min(b))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = []
	for i in range(len(a) - 2):
		b.append(a[i + 2] - a[i])
	ans = 0
	ans = min(b)
	print(ans)


def pd(x, y, z):
	a = y - x
	b = z - y
	return b + a
n = int(input())
for _ in range(n):
	a = int(input())
	b = list(map(int, input().split()))
	b.sort()
	zx = 10000000000
	for i in range(a - 2):
		if zx > pd(b[i], b[i + 1], b[i + 2]):
			zx = pd(b[i], b[i + 1], b[i + 2])
	print(zx)


for j in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = sorted(l)
	diff = 9223372036854775807
	for i in range(1, n - 1):
		dif2 = abs(l[i - 1] - l[i]) + abs(l[i] - l[i + 1])
		if dif2 < diff:
			diff = dif2
	print(diff)


import sys
sys.setrecursionlimit(200000)
import math
from collections import Counter
from collections import defaultdict
from collections import deque
input = sys.stdin.readline
from functools import lru_cache
import heapq

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
testcases = inp()
for _ in range(testcases):
	length = inp()
	arr = inlt()
	arr.sort()
	y = math.inf
	for i in range(len(arr)):
		if i - 1 >= 0 and i + 1 < len(arr):
			y = min(arr[i + 1] - arr[i - 1], y)
		if i - 2 >= 0:
			y = min(2 * arr[i] - arr[i - 1] - arr[i - 2], y)
		if i + 2 < len(arr):
			y = min(arr[i + 2] + arr[i + 1] - 2 * arr[i], y)
	print(y)


t = int(input())

def count_steps(a, b, c):
	return abs(b - a) + abs(b - c)
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split(' ')))
	a.sort()
	min_steps = 10 ** 10
	for i in range(len(a)):
		if i + 3 == len(a) + 1:
			break
		min_steps = min(min_steps, count_steps(a[i], a[i + 1], a[i + 2]))
	print(min_steps)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = 1000000007
	for i in range(n - 2):
		k = abs(a[i] - a[i + 1]) + abs(a[i + 1] - a[i + 2])
		ans = min(k, ans)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	(l, r) = (0, 2)
	ans = float('inf')
	while r < n:
		for m in range(l, r + 1):
			temp = 0
			for k in range(l, r + 1):
				temp += abs(a[k] - a[m])
			ans = min(ans, temp)
		l += 1
		r += 1
	print(ans)


ans = 0
for asdfh in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	valuemin = a[2]
	for x in range(n - 2):
		valuenew = a[x + 2] - a[x]
		if valuenew < valuemin:
			valuemin = valuenew
	print(valuemin)


def solve(array, m):
	array.sort()
	ans = abs(arr[0] - arr[2])
	for i in range(0, m - 2):
		if abs(array[i] - array[i + 2]) < ans:
			ans = abs(array[i] - array[i + 2])
	return ans
n = int(input())
for i in range(n):
	m = int(input())
	arr = [int(x) for x in input().split()]
	print(solve(arr, m))


for _ in range(int(input())):
	__ = input()
	arr = [int(i) for i in input().split(' ')]
	arr.sort()
	res = arr[2] - arr[0]
	for i in range(1, len(arr) - 1):
		res = min(res, arr[i + 1] - arr[i - 1])
	print(res)