You are given an array $a$ that contains $n$ integers. You can choose any proper subsegment $a_l, a_{l + 1}, \ldots, a_r$ of this array, meaning you can choose any two integers $1 \le l \le r \le n$, where $r - l + 1 < n$. We define the beauty of a given subsegment as the value of the following expression:

$$\max(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) - \min(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) + \max(a_{l}, \ldots, a_{r}) - \min(a_{l}, \ldots, a_{r}).$$

Please find the maximum beauty among all proper subsegments.


-----Input-----

The first line contains one integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Then follow the descriptions of each test case.

The first line of each test case contains a single integer $n$ $(4 \leq n \leq 10^5)$ — the length of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_{i} \leq 10^9$) — the elements of the given array.

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


-----Output-----

For each testcase print a single integer — the maximum beauty of a proper subsegment.


-----Examples-----

Input
4
8
1 2 2 3 1 5 6 1
5
1 2 3 100 200
4
3 3 3 3
6
7 8 3 1 1 8
Output
9
297
0
14


-----Note-----

In the first test case, the optimal segment is $l = 7$, $r = 8$. The beauty of this segment equals to $(6 - 1) + (5 - 1) = 9$.

In the second test case, the optimal segment is $l = 2$, $r = 4$. The beauty of this segment equals $(100 - 2) + (200 - 1) = 297$.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Greedy algorithms

Complete search

Mathematics

Data structures


Solutions:
import math
t = int(input())
for t1 in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m1 = max(l)
	l.pop(l.index(m1))
	m2 = max(l)
	mi1 = min(l)
	l.pop(l.index(mi1))
	mi2 = min(l)
	print(m1 + m2 - mi1 - mi2)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = list(set(l))
	if len(a) == 1:
		print(0)
	elif len(a) == 2:
		print(max(a) - min(a))
	else:
		min1 = min(l)
		l.remove(min1)
		min2 = min(l)
		l.remove(min2)
		max1 = max(l)
		l.remove(max1)
		max2 = max(l)
		l.remove(max2)
		print(max1 + max2 - min1 - min2)


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	print(arr[-1] + arr[-2] - arr[0] - arr[1])
	t -= 1


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


def sums(lis):
	min1 = min(lis)
	max1 = max(lis)
	lis.remove(min1)
	lis.remove(max1)
	min2 = min(lis)
	max2 = max(lis)
	print(max1 + max2 - min1 - min2)
tc = int(input())
for _ in range(tc):
	t = int(input())
	templist = list(map(int, input().split()[:t]))
	sums(templist)


t = int(input())
while t:
	n = int(input())
	li = list(map(int, input().split()))
	li.sort()
	ans = li[-1] - li[0] + (li[-2] - li[1])
	print(ans)
	t -= 1


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]
	max1 = arr.pop(arr.index(max(arr)))
	min1 = arr.pop(arr.index(min(arr)))
	max2 = arr.pop(arr.index(max(arr)))
	min2 = arr.pop(arr.index(min(arr)))
	print(max1 + max2 - (min1 + min2))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ma = max(l)
	mi = min(l)
	l.remove(max(l))
	l.remove(min(l))
	mat = max(l)
	mit = min(l)
	print(ma + mat - mi - mit)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = max(a)
	a2 = min(a)
	a.remove(a1)
	a.remove(a2)
	a3 = max(a)
	a4 = min(a)
	print(a1 - a2 + (a3 - a4))


t = int(input())
for k in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	a = max(lst)
	lst.remove(a)
	b = max(lst)
	c = min(lst)
	lst.remove(c)
	d = min(lst)
	print(a + b - c - d)


t = int(input())
for ti in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	M = max(a)
	m = min(a)
	a.remove(M)
	a.remove(m)
	MM = max(a)
	mm = min(a)
	print(M + MM - m - mm)


t = int(input())
for j in range(t):
	n = int(input())
	m = list(map(int, input().split()))
	a = max(m)
	b = min(m)
	m.remove(a)
	m.remove(b)
	c = max(m)
	d = min(m)
	print(a + c - b - d)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	MAX = max(a)
	MIN = min(a)
	aN = list(a)
	aN.remove(MAX)
	aN.remove(MIN)
	print(MAX - MIN + max(aN) - min(aN))


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


def ans(a):
	l = a
	g = max(a)
	h = min(a)
	l.remove(g)
	l.remove(h)
	enter = g - h + max(l) - min(l)
	return enter
t = int(input(''))
lst = []
for i in range(t):
	le = int(input())
	l = [int(x) for x in input().split()]
	lst.append(ans(l))
for i in lst:
	print(i)


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a.count(a[0]) == n:
		print(0)
	else:
		b = list()
		diff = max(a) - min(a)
		a.remove(max(a))
		a.remove(min(a))
		diff1 = max(a) - min(a)
		print(diff + diff1)


for _ in range(int(input())):
	input()
	li = [int(e) for e in input().split()]
	(mx, mn) = (max(li), min(li))
	(li.remove(mx), li.remove(mn))
	(ndMx, ndMn) = (max(li), min(li))
	print(mx + ndMx - mn - ndMn)


t = int(input())
for _ in range(t):
	n = int(input())
	lis = list(map(int, input().rstrip(' ').split(' ')))
	maximum = max(lis)
	minimum = min(lis)
	lis.remove(maximum)
	lis.remove(minimum)
	secmax = max(lis)
	secmin = min(lis)
	print(maximum + secmax - secmin - minimum)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	a = arr[-1]
	b = arr[-2]
	c = arr[0]
	d = arr[1]
	ans = a - c + (b - d)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = max(l) - min(l)
	l.remove(max(l))
	l.remove(min(l))
	ans += max(l) - min(l)
	print(ans)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = max(l) - min(l)
	l.remove(max(l))
	l.remove(min(l))
	ans += max(l) - min(l)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(Mx, mx, Mn, mn) = (0, 0, 1000000000.0 + 10, 1000000000.0 + 10)
	for el in arr:
		if el > mx:
			if el > Mx:
				(Mx, mx) = (el, Mx)
			else:
				mx = el
		if el < mn:
			if el < Mn:
				(Mn, mn) = (el, Mn)
			else:
				mn = el
	print(Mx - Mn + mx - mn)


from math import *
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = l[n - 1] + l[n - 2] - l[0] - l[1]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	a = max(l)
	l.remove(a)
	b = max(l)
	l.remove(b)
	c = min(l)
	l.remove(c)
	d = min(l)
	r = a + b - c - d
	print(r)


for _ in range(int(input())):
	n = int(input())
	v = list(map(int, input().split()))[:n]
	(m1, n1) = (max(v), min(v))
	for i in [m1, n1]:
		v.remove(i)
	(m2, n2) = (max(v), min(v))
	print(m1 + m2 - n1 - n2)


t = int(input())
for i in range(t):
	p = int(input())
	l = list(map(int, input().split()))
	g = len(l)
	l.sort()
	print(l[g - 1] + l[g - 2] - l[0] - l[1])


for _ in range(int(input())):
	len_arr = int(input())
	list1 = list(map(int, input().split()))
	max_1 = max(list1)
	list1.remove(max(list1))
	max_2 = max(list1)
	mini_1 = min(list1)
	list1.remove(min(list1))
	mini_2 = min(list1)
	print(max_1 - mini_1 + max_2 - mini_2)


def InterestingSum(l):
	(min1, max1) = (min(l), max(l))
	l.pop(l.index(max1))
	l.pop(l.index(min1))
	(min2, max2) = (min(l), max(l))
	print(max1 - min1 + (max2 - min2))
t = int(input())
l = []
for i in range(t):
	n = int(input())
	m = list(map(int, input().split()))
	l.append(m)
for i in l:
	InterestingSum(i)


class Solution:

	def maxBeauty(self, arr):
		arrMin = min(arr)
		minIdx = arr.index(arrMin)
		del arr[minIdx]
		nMin = min(arr)
		arrMax = max(arr)
		maxIdx = arr.index(arrMax)
		del arr[maxIdx]
		nMax = max(arr)
		return arrMax + nMax - arrMin - nMin
ob = Solution()
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().strip().split()))
	res = ob.maxBeauty(arr)
	print(res)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	print(sum(arr[-2:]) - sum(arr[:2]))


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


t = int(input())
a = 1
while a <= t:
	n = int(input())
	arr = input().split()
	b = map(int, arr)
	l = list(b)
	m1 = max(l)
	n1 = min(l)
	l.remove(m1)
	l.remove(n1)
	m2 = max(l)
	n2 = min(l)
	c = m1 - n1
	d = m2 - n2
	print(c + d)
	a = a + 1


y = int(input())
x = [0] * y
f = 0
while y > 0:
	n = int(input())
	t = list(map(int, input().split()))
	sum = max(t) - min(t)
	t.remove(max(t))
	t.remove(min(t))
	x[f] = sum + max(t) - min(t)
	y -= 1
	f += 1
for i in x:
	print(i)


for s in [*open(0)][2::2]:
	(a, b, *_, c, d) = sorted(map(int, s.split()))
	print(c + d - a - b)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lis.sort()
	print(lis[-1] + lis[-2] - lis[1] - lis[0])


n = int(input())
p = []
for i in range(n):
	input()
	ns = [int(j) for j in input().split()]
	n1 = max(ns) - min(ns)
	ns.remove(max(ns))
	ns.remove(min(ns))
	n2 = max(ns) - min(ns)
	p.append(str(n1 + n2))
print('\n'.join(p))


def get_Highest_Sum(list):
	sum = max(list) - min(list)
	list.remove(max(list))
	list.remove(min(list))
	sum += max(list) - min(list)
	return sum
t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	print(get_Highest_Sum(nums))


t = int(input())
for j in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l
	minn1 = min(l)
	maxx1 = max(l)
	del l[l.index(minn1)]
	del l[l.index(maxx1)]
	minn2 = min(l)
	maxx2 = max(l)
	if l1.index(minn2) != 0 and l1.index(maxx2) != 0 or l1.count(minn2) > 1 or l1.count(maxx2) > 0:
		print(maxx1 - minn1 + (maxx2 - minn2))
	else:
		while l1.index(minn2) == 0 and l1.index(maxx2) == 0:
			del l[l.index(minn2)]
			del l[l.index(maxx2)]
			minn2 = min(l)
			maxx2 = max(l)
		print(maxx1 - minn1 + (maxx2 - minn1))


q = int(input())
for i in range(q):
	n = int(input())
	r1 = list(map(int, input().split()))
	r1.sort()
	print(r1[-1] + r1[-2] - r1[0] - r1[1])


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


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	beauty = 0
	for i in range(2):
		beauty += max(a) - min(a)
		a.remove(max(a))
		a.remove(min(a))
	print(beauty)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	c.sort()
	print(c[-1] + c[-2] - (c[0] + c[1]))


for _ in range(int(input())):
	input()
	li = sorted(map(int, input().split()))
	li = list(li)
	print(li[-1] + li[-2] - li[0] - li[1])


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


from math import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(a[-1] + a[-2] - a[0] - a[1])


for pratyush in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(a[n - 1] - a[0] - a[1] + a[n - 2])


t = int(input())
for _ in range(t):
	n = int(input())
	a = sorted(map(int, input().split()))
	print(sum(a[-2:]) - sum(a[:2]))


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())

def solve():
	n = inp()
	nums = inlt()
	nums.sort()
	ans = nums[-1] + nums[-2] - nums[0] - nums[1]
	return ans
for _ in range(inp()):
	print(solve())


a = int(input())
for y in range(a):
	a = int(input())
	b = list(map(int, input().split()))
	b.sort()
	j = b[-1] + b[-2] - (b[0] + b[1])
	print(j)


for _ in range(int(input())):
	result = 0
	_ = int(input())
	tmp = list(input().split())
	a = [int(_) for _ in tmp]
	t = max(a)
	result += t
	a.remove(t)
	t = max(a)
	result += t
	a.remove(t)
	t = min(a)
	result -= t
	a.remove(t)
	t = min(a)
	result -= t
	a.remove(t)
	print(result)


length = int(input())
for _ in range(length):
	n = int(input())
	lst = list(map(int, input().split(' ')))
	lst.sort()
	print(lst[-1] + lst[-2] - lst[0] - lst[1])


for test in range(int(input())):
	n = int(input())
	lt = list(map(int, input().split()))
	lt.sort()
	print(lt[-1] + lt[-2] - lt[0] - lt[1])


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


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	print(lst[n - 1] + lst[n - 2] - lst[0] - lst[1])


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	max1 = max(arr)
	min1 = min(arr)
	arr.remove(max1)
	arr.remove(min1)
	max2 = max(arr)
	min2 = min(arr)
	print(max1 - min1 + (max2 - min2))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	s = 0
	m = max(l)
	m1 = min(l)
	s += m - m1
	l.remove(m)
	l.remove(m1)
	m = max(l)
	m1 = min(l)
	s += m - m1
	print(s)


import sys
input = lambda : sys.stdin.readline().rstrip()
g = lambda : [*map(int, input().split())]
for _ in range(int(input())):
	N = int(input())
	nums = g()
	nums.sort()
	ans = nums[-1] + nums[-2] - nums[0] - nums[1]
	print(ans)


t = int(input())
for i in range(1, t + 1):
	n = int(input())
	a = [int(j) for j in input().split()]
	a.sort()
	print(a[n - 1] + a[n - 2] - a[0] - a[1])


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
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()))
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

def solve(n, a):
	a.sort()
	print(a[n - 1] + a[n - 2] - a[0] - a[1])
for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	solve(n, a)


R = lambda : [*map(int, input().split())]
for _ in range(int(input())):
	n = int(input())
	a = R()
	a.sort()
	print(a[-1] + a[-2] - a[1] - a[0])


def help():
	n = int(input())
	a = [int(y) for y in input().split()]
	x = max(a)
	a.remove(x)
	y = max(a)
	a.remove(y)
	z = min(a)
	a.remove(z)
	w = min(a)
	a.remove(w)
	print(x + y - z - w)
t = int(input())
for i in range(0, t):
	help()


import math

def solve():
	n = int(input())
	lst = list(map(int, input().split(' ')))
	lst.sort()
	print(lst[-1] + lst[-2] - lst[0] - lst[1])
t = int(input())
while t:
	t -= 1
	solve()


import sys
from os import path
import os
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import math

def beauty(a, l, r):
	t1 = a[:l] + a[r + 1:]
	t2 = a[l:r + 1]
	return max(t1) - min(t1) + max(t2) - min(t1)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(a[-1] + a[-2] - a[0] - a[1])


import math
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	print(A[-1] + A[-2] - A[0] - A[1])


T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(a[n - 1] - a[0] + (a[n - 2] - a[1]))


t = int(input())
res = []
for _ in range(t):
	n = int(input())
	nums = [int(x) for x in input().split()]
	nums.sort(reverse=True)
	res.append(nums[0] + nums[1] - nums[-1] - nums[-2])
for val in res:
	print(val)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	max1 = max(l)
	l.remove(max1)
	max2 = max(l)
	l.remove(max2)
	min1 = min(l)
	l.remove(min1)
	min2 = min(l)
	l.remove(min2)
	print(max1 + max2 - min1 - min2)


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()))
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 = 10 ** 9 + 7
import os
import sys
from io import BytesIO, IOBase

def solve():
	length = inp()
	arr = seq()
	arr.sort()
	return arr[-1] - arr[0] + arr[-2] - arr[1]

def main():
	testcases = inp()
	for test in range(testcases):
		print(solve())
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	ma = max(l)
	place = l.index(ma)
	a = l
	l[place] = 0
	mo = max(l)
	l[place] = ma
	mia = min(a)
	place = a.index(mia)
	a[place] = max(a)
	mio = min(a)
	print(ma + mo - mia - mio)


for _ in range(int(input())):
	n = int(input())
	mass = list(map(int, input().split()))
	mass.sort()
	(a, b) = (max(mass), min(mass))
	imax = mass.index(a)
	imin = mass.index(b)
	del mass[imax]
	del mass[imin]
	(c, d) = (max(mass), min(mass))
	print(a - b + (c - d))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	max1 = max(a)
	max1_p = a.index(max1)
	min1 = min(a)
	min1_p = a.index(min1)
	max2 = 0
	min2 = float('inf')
	for i in range(n):
		if a[i] < min2 and i != min1_p:
			min2 = a[i]
		if a[i] > max2 and i != max1_p:
			max2 = a[i]
	print(max1 + max2 - min1 - min2)


t = int(input())
while t:
	n = int(input())
	arr = [int(i) for i in input().split()]
	arr.sort()
	res = arr[n - 1] + arr[n - 2] - arr[0] - arr[1]
	print(res)
	t -= 1


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split(' ')))
	a = arr.pop(arr.index(max(arr)))
	b = arr.pop(arr.index(max(arr)))
	c = arr.pop(arr.index(min(arr)))
	d = arr.pop(arr.index(min(arr)))
	print(a + b - c - d)
	t -= 1


import math
import os
import random
import re
import sys
from math import gcd
from math import sqrt
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mx = max(arr)
	mn = min(arr)
	arr.remove(max(arr))
	arr.remove(min(arr))
	se_mx = max(arr)
	se_mn = min(arr)
	first = mx - mn
	second = se_mx - se_mn
	print(first + second)


t = int(input())
for o in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = max(a) - min(a)
	a.pop(a.index(max(a)))
	a.pop(a.index(min(a)))
	print(s + max(a) - min(a))


t = int(input())
for i in range(t):
	l = int(input())
	li = list(map(int, input().split()))
	sum = max(li)
	li.remove(max(li))
	sum += max(li) - min(li)
	li.remove(min(li))
	sum -= min(li)
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	l = input().split()
	ln = []
	for j in l:
		ln.append(int(j))
	ln.sort()
	print(ln[n - 1] + ln[n - 2] - ln[0] - ln[1])


T = int(input())
for i in range(T):
	n = int(input())
	s1 = input()
	arr = [int(x) for x in s1.split()]
	beauty = 0
	temp = max(arr)
	beauty += temp
	arr.remove(temp)
	temp = max(arr)
	beauty += temp
	arr.remove(temp)
	temp = min(arr)
	beauty -= temp
	arr.remove(temp)
	temp = min(arr)
	beauty -= temp
	arr.remove(temp)
	print(beauty)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(k) for k in input().split()]
	arr.sort()
	a = arr[-1]
	b = arr[0]
	c = a - b
	d = arr[-2]
	e = arr[1]
	f = d - e
	print(c + f)


import bisect
from collections import deque, defaultdict
T = int(input())
q = []
for i in range(T):
	n = int(input())
	data = [int(i) for i in input().split()]
	q.append((n, data))

def solve(n, data):
	maxx1 = max(data)
	minn1 = min(data)
	data.remove(maxx1)
	data.remove(minn1)
	maxx2 = max(data)
	minn2 = min(data)
	print(maxx1 - minn1 + maxx2 - minn2)
for (n, data) in q:
	solve(n, data)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split(None, n)[:n]))
	a.sort()
	print(a[n - 1] + a[n - 2] - a[0] - a[1])


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


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


p = lambda a: print(a)
iip = lambda : int(input())
sip = lambda : input()
mip = lambda : map(int, input().strip().split())
lip = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
flr = lambda x: int(x)
tstcs = lambda : range(int(input()))
for t in tstcs():
	n = iip()
	l = lip()
	l.sort()
	a = l[0]
	b = l[1]
	c = l[n - 1]
	d = l[n - 2]
	print(c + d - a - b)


t = int(input())
for i in range(t):
	n = int(input())
	xs = input()
	xs = [int(x) for x in xs.split()]
	max1 = max(xs)
	new_list = xs
	new_list.remove(max(new_list))
	max2 = max(new_list)
	min1 = min(xs)
	new_list = xs
	new_list.remove(min(new_list))
	min2 = min(new_list)
	print(max1 - min1 + max2 - min2)