Read problem statements in [Mandarin], [Bengali], and [Russian] as well.

You are handed a binary string S, but not in any ordinary form. Instead of being directly given the value of S, you are given an array C of size N representing the *01-compression representation* of S. This means S first contains C_{1} \texttt{0} characters, then C_{2} \texttt{1} characters, then C_{3} \texttt{0} characters, and so on. For example, the array C = [2, 3, 4, 3] corresponds with the binary string \texttt{001110000111}.

You are allowed to modify S by swapping elements of C. More specifically, you are allowed to swap C_{i} and C_{j} *as long as* C_{i} and C_{j} both encodes blocks of the same character. For example, from C = [2, 3, 4, 3], you can swap C_{1} and C_{3} because both of them encode blocks of \texttt{0}'s, turning C to [4, 3, 2, 3] and S to \texttt{000011100111}. However, you cannot swap C_{1} and C_{2} because C_{1} encodes a block of \texttt{0}'s, while C_{2} encodes a block of \texttt{1}s.

Perform the swapping operation in any way as many times as you want (including zero times) so that the final string S has as many \texttt{01} subsequences as possible. As a reminder, a subsequence of a string is a sequence that can be derived by deleting zero or more characters without changing the order of the remaining characters.

You need to find any optimal final array C, for which the number of \texttt{01} subsequence will be the largest possible.

------ Input Format ------ 

- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains a single integer N - the size of the array C.
- The second line of each test case contains N integers C_{1}, C_{2}, \dots, C_{N} - the \texttt{01}-compression representation of S.

------ Output Format ------ 

For each test case, output two lines: the first line contains N integers representing the optimal array C, and the second line contains the maximum number of \texttt{01} subsequences.

------ Constraints ------ 

$ 1 ≤ T ≤ 1000 $
$ 1 ≤ N ≤ 100 $
$ 1 ≤  C_{i} ≤ 1000 $

----- Sample Input 1 ------ 
1
4
2 3 4 3
----- Sample Output 1 ------ 
4 3 2 3
30
----- explanation 1 ------ 
- Test case $1$: $C = [2, 3, 4, 3]$, which means $S$ corresponds to $\texttt{001110000111}$. The optimal list of operations is to only swap $C_{1}$ and $C_{3}$, resulting in $C = [4, 3, 2, 3]$ and $S = $ $\texttt{000011100111}$, which has $30$ subsequences of $\texttt{01}$.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Ad-hoc


Solutions:
def ans(arr, n):
	ones = []
	zeros = []
	i = 0
	while i < n:
		if i % 2 == 0:
			zeros.append(arr[i])
		else:
			ones.append(arr[i])
		i += 1
	ones.sort()
	zeros.sort()
	zeros.reverse()
	final = []
	i = 0
	j = 0
	k = 0
	while len(final) != n:
		if k % 2 == 0:
			final.append(zeros[i])
			i += 1
		else:
			final.append(ones[j])
			j += 1
		k += 1
	ones_sum = 0
	i = 0
	while i < n:
		if i % 2 == 1:
			ones_sum += final[i]
		i += 1
	count = 0
	i = 0
	while i < n:
		if i % 2 == 0:
			count += final[i] * ones_sum
		else:
			ones_sum = max(0, ones_sum - final[i])
		i += 1
	return (final, count)
test_cases = int(input())
while test_cases != 0:
	d = int(input())
	d2 = list(map(int, input().split()))
	(x, y) = ans(d2, d)
	print(*x)
	print(y)
	test_cases -= 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	c = list(map(int, input().split()))
	a1 = []
	a2 = []
	for i in range(n):
		if i % 2 == 0:
			a1.append(c[i])
		else:
			a2.append(c[i])
	a1.sort(reverse=True)
	a2.sort()
	x = 0
	x1 = []
	i = 0
	j = 0
	l1 = len(a1)
	l2 = len(a2)
	while len(x1) != n:
		if i < l1:
			x1.append(a1[i])
			i += 1
		if j < l2:
			x1.append(a2[j])
			j += 1
	for i in range(len(a2) - 1, -1, -1):
		a2[i] += x
		x = a2[i]
	ans = 0
	for i in range(len(a2)):
		ans += a1[i] * a2[i]
	print(*x1)
	print(ans)


def ans(arr):
	i = 0
	ones = []
	zeros = []
	while i != len(arr):
		if i % 2 == 0:
			zeros.append(arr[i])
		else:
			ones.append(arr[i])
		i += 1
	new_array = []
	ones.sort()
	zeros.sort()
	zeros.reverse()
	i = 0
	j = 0
	k = 0
	while k < len(arr):
		if k % 2 == 0:
			if i != len(zeros):
				new_array.append(zeros[i])
				i += 1
		elif j != len(ones):
			new_array.append(ones[j])
			j += 1
		k += 1
	i = 0
	a = 0
	zeros = []
	ones = []
	while i != len(new_array):
		if i % 2 == 0:
			zeros.append(new_array[i])
		else:
			ones.append(new_array[i])
		i += 1
	i = 0
	while i != len(zeros):
		if i < len(ones):
			j = i
			k = sum(ones[j:len(ones)])
			a += zeros[i] * k
		i += 1
	return (new_array, a)
test_cases = int(input())
while test_cases != 0:
	d = input()
	d2 = list(map(int, input().split()))
	(x, y) = ans(d2)
	print(*x)
	print(y)
	test_cases -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(l0, l1) = ([], [])
	for i in range(n):
		if i % 2 == 0:
			l0.append(l[i])
		else:
			l1.append(l[i])
	l0.sort(reverse=True)
	l1.sort()
	sum1 = 0
	sum2 = sum(l1)
	for i in range(n // 2):
		print(l0[i], l1[i], end=' ')
		sum1 += l0[i] * sum2
		sum2 -= l1[i]
	if n % 2 == 1:
		print(l0[-1])
	else:
		print()
	print(sum1)


t = int(input())
for i in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	m = []
	n = []
	for j in range(a):
		if j % 2 == 0:
			m.append(b[j])
		else:
			n.append(b[j])
	m.sort(reverse=True)
	n.sort()
	e = 0
	o = 0
	for k in range(a):
		if k % 2 == 0:
			b[k] = m[e]
			e += 1
		else:
			b[k] = n[o]
			o += 1
	print(*b)
	ans = 0
	for z in range(len(m)):
		for x in n[z:]:
			ans += m[z] * x
	print(ans)


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	m = []
	n = []
	for i in range(a):
		if i % 2 == 0:
			m.append(b[i])
		else:
			n.append(b[i])
	m.sort(reverse=True)
	n.sort()
	e = o = 0
	for i in range(a):
		if i % 2 == 0:
			b[i] = m[e]
			e += 1
		else:
			b[i] = n[o]
			o += 1
	print(*b)
	solun = 0
	for i in range(len(m)):
		for j in n[i:]:
			solun += m[i] * j
	print(solun)


t = int(input())
while t:
	n = int(input())
	c = list(map(int, input().split()))
	zero = []
	one = []
	for i in range(n):
		if i % 2 == 0:
			zero.append(c[i])
		else:
			one.append(c[i])
	zero.sort(reverse=True)
	one.sort()
	sm = sum(one)
	k = 0
	ans = 0
	for i in zero:
		ans += i * sm
		if len(one) != 0 and k != len(one) - 1:
			sm = sm - one[k]
			k += 1
		else:
			break
	updated = []
	x = 0
	y = 0
	for i in range(n):
		if i % 2 == 0:
			updated.append(zero[x])
			x += 1
		else:
			updated.append(one[y])
			y += 1
	print(*updated)
	print(ans)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	_arr = [int(x) for x in input().split()]
	arr_0s = []
	arr_1s = []
	for i in range(n):
		if i % 2 == 0:
			arr_0s.append(_arr[i])
		else:
			arr_1s.append(_arr[i])
	arr_0s = sorted(arr_0s, reverse=True)
	arr_1s = sorted(arr_1s)
	arr_op = []
	i = 0
	total = 0
	for i in range(n // 2):
		arr_op.append(str(arr_0s[i]))
		arr_op.append(str(arr_1s[i]))
		total += arr_0s[i] * sum(arr_1s[i:])
	if n % 2 != 0 and n > 1:
		arr_op.append(str(arr_0s[i + 1]))
	elif n % 2 != 0:
		arr_op.append(str(arr_0s[i]))
	print(' '.join(arr_op))
	print(total)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = []
	s = []
	new = []
	ans = 0
	for i in range(len(arr)):
		if i % 2 == 0:
			m.append(arr[i])
		else:
			s.append(arr[i])
	m.sort(reverse=True)
	s.sort()
	if n % 2 == 0:
		for k in range(n // 2):
			new.append(m[k])
			new.append(s[k])
	else:
		for k in range(n // 2 + 1):
			new.append(m[k])
			if k < n // 2:
				new.append(s[k])
	for ele in new:
		print(ele, end=' ')
	print()
	while len(s) > 0 and len(m) > 0:
		ans += m[0] * sum(s)
		m.remove(m[0])
		s.remove(s[0])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l2 = []
	cou1 = 0
	for i in range(len(l)):
		if i % 2 == 0:
			l1.append(l[i])
		else:
			l2.append(l[i])
			cou1 = cou1 + l[i]
	l1.sort(reverse=True)
	l2.sort()
	cou = 0
	for i in range(len(l2)):
		cou = cou + l1[i] * cou1
		cou1 = cou1 - l2[i]
		print(l1[i], l2[i], end=' ')
	if len(l1) != len(l2):
		print(l1[-1], end=' ')
	print()
	print(cou)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	zero = [a[x] for x in range(n) if not x & 1]
	one = [a[x] for x in range(n) if x & 1]
	zero = sorted(zero, reverse=True)
	one = sorted(one)
	sumones = sum(one)
	total = 0
	arr = []
	for x in range(len(one)):
		arr.append(zero[x])
		arr.append(one[x])
		total += zero[x] * sumones
		sumones -= one[x]
	if n & 1:
		arr.append(zero[len(one)])
		print(*arr)
	else:
		print(*arr)
	print(total)


def sol(arr):
	zero = []
	one = []
	total_one = 0
	for i in range(len(arr)):
		if i & 1 == 0:
			zero.append(arr[i])
		else:
			one.append(arr[i])
			total_one += arr[i]
	zero.sort(reverse=True)
	one.sort()
	j = 0
	k = 0
	for i in range(len(arr)):
		if i & 1 == 0:
			arr[i] = zero[j]
			j = j + 1
		else:
			arr[i] = one[k]
			k = k + 1
	ans = 0
	j = 0
	prev = 0
	for i in zero:
		if len(one) != 0:
			ans += i * (total_one - prev)
			prev += one.pop(0)
	return ans
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	ans = sol(arr)
	for i in arr:
		print(i, end=' ')
	print()
	print(ans)
	t = t - 1


t = int(input())
while t:
	n = int(input())
	c = list(map(int, input().split()))
	zero = []
	one = []
	for i in range(n):
		if i % 2 == 0:
			zero.append(c[i])
		else:
			one.append(c[i])
	zero.sort(reverse=True)
	one.sort()
	d = []
	j = 0
	k = 0
	for i in range(n):
		if i % 2 == 0:
			d.append(zero[j])
			j += 1
		else:
			d.append(one[k])
			k += 1
	total = 0
	for i in range(n):
		sm = 0
		if i % 2 == 0:
			for j in range(i + 1, n, 2):
				sm += d[j]
			total += d[i] * sm
	print(*d)
	print(total)
	t -= 1


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().strip().split()))
	arr[::2] = sorted(arr[::2], reverse=True)
	arr[1::2] = sorted(arr[1::2])
	print(*arr)
	(ans, prev_0) = (0, 0)
	for i in range(1, n, 2):
		prev_0 += arr[i - 1]
		ans += prev_0 * arr[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	a1 = [arr[i] for i in range(n) if i % 2 == 0]
	a2 = [arr[i] for i in range(n) if i % 2 == 1]
	a1.sort(reverse=True)
	a2.sort()
	if len(a2) == 0:
		print(*arr)
		print(0)
		continue
	ans = []
	for i in range(len(a2)):
		ans.append(a1[i])
		ans.append(a2[i])
	if len(a2) != len(a1):
		ans.append(a1[-1])
	print(*ans)
	acc = [0 for i in range(len(a2))]
	acc[-1] = a2[-1]
	for i in range(len(a2) - 2, -1, -1):
		acc[i] = acc[i + 1] + a2[i]
	count = 0
	for i in range(len(acc)):
		count += a1[i] * acc[i]
	print(count)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = [int(i) for i in stdin.readline().split()]
	zeros = []
	ones = []
	for i in range(n):
		if i % 2 == 0:
			zeros.append(arr[i])
		else:
			ones.append(arr[i])
	zeros.sort(reverse=True)
	ones.sort()
	agg = []
	for ele in ones[::-1]:
		if len(agg) == 0:
			agg.append(ele)
		else:
			agg.append(ele + agg[-1])
	agg = agg[::-1]
	ans = []
	for i in range(n):
		if i % 2 == 0:
			ans.append(zeros[i // 2])
		else:
			ans.append(ones[(i - 1) // 2])
	print(*ans)
	ans2 = 0
	for (i, ele) in enumerate(zeros):
		if i < len(agg):
			ans2 += ele * agg[i]
	print(ans2)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(t1, t2) = ([], [])
	for i in range(n):
		if i % 2 == 0:
			t1.append(l[i])
		else:
			t2.append(l[i])
	t1 = sorted(t1, reverse=True)
	t2 = sorted(t2)
	oup = []
	for i in range(n // 2):
		oup.append(t1[i])
		oup.append(t2[i])
	if len(oup) == n - 1:
		oup.append(t1[-1])
	print(*oup)
	ans = 0
	for i in range(1, n, 2):
		temp = 0
		for j in range(0, i, 2):
			temp += oup[j]
		ans += temp * oup[i]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	l[::2] = sorted(l[0::2], reverse=True)
	l[1::2] = sorted(l[1::2])
	print(*l)
	summ = 0
	for i in range(1, len(l), 2):
		summ += l[i]
	ans = 0
	for i in range(len(l)):
		if i % 2 == 0:
			ans += l[i] * summ
		else:
			summ -= l[i]
	print(ans)


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a1 = [a[i] for i in range(n) if i % 2 == 0]
	a2 = [a[i] for i in range(n) if i % 2 == 1]
	a1.sort(reverse=True)
	a2.sort()
	s = sum(a2)
	t = 0
	for i in range(len(a2)):
		print(a1[i], a2[i], end=' ')
		t += a1[i] * s
		s -= a2[i]
	if n % 2 == 1:
		print(a1[-1], end=' ')
	print()
	print(t)


t = int(input())
for cur in range(t):
	n = int(input())
	zeros = []
	ones = []
	inp = input().split()
	for i in range(n):
		if i % 2 == 0:
			zeros.append(int(inp[i]))
		else:
			ones.append(int(inp[i]))
	zeros_s = sorted(zeros)[::-1]
	ones_s = sorted(ones)
	out = []
	Z_c = 0
	O_c = 0
	out_C = 0
	res = 0
	for i in range(n):
		if i % 2 == 0:
			out.append(zeros_s[Z_c])
			out_C += zeros_s[Z_c]
			Z_c += 1
		else:
			out.append(ones_s[O_c])
			res += out_C * ones_s[O_c]
			O_c += 1
	for i in out[:-1]:
		print(i, end=' ')
	print(out[-1])
	print(res)


import sys

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

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

def get_list_string():
	return list(map(str, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

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

def get_print(x):
	sys.stdout.write(x + '\n')

def get_print_int_same(x):
	sys.stdout.write(str(x) + ' ')

def get_print_same(x):
	sys.stdout.write(x + ' ')
from sys import maxsize

def solve():
	for _ in range(get_int()):
		n = get_int()
		arr = get_list()
		(t1, t2) = ([], [])
		ones = 0
		for i in range(n):
			if i % 2 == 0:
				t1.append(arr[i])
			else:
				ones += arr[i]
				t2.append(arr[i])
		t1.sort(reverse=True)
		t2.sort()
		(i, j) = (0, 0)
		x = 0
		while x < n:
			arr[x] = t1[i]
			if x + 1 < n:
				arr[x + 1] = t2[j]
			else:
				break
			i += 1
			j += 1
			x += 2
		ans = 0
		for i in range(0, n, 2):
			ans += arr[i] * ones
			if i + 1 < n:
				ones -= arr[i + 1]
			else:
				ones = 0
		get_print(' '.join(map(str, arr)))
		get_print_int(ans)
solve()


import heapq
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	oddFlag = False
	if n % 2 == 1:
		oddFlag = True
		n -= 1
	ev = []
	heapq.heapify(ev)
	od = []
	heapq.heapify(od)
	for i in range(0, n, 2):
		heapq.heappush(ev, -1 * a[i])
		heapq.heappush(od, a[i + 1])
	if oddFlag:
		heapq.heappush(ev, -1 * a[-1])
	temp = od.copy()
	temp.sort()
	for i in range(n // 2 - 2, -1, -1):
		temp[i] += temp[i + 1]
	ans = 0
	while od:
		x = -1 * heapq.heappop(ev)
		y = heapq.heappop(od)
		z = temp.pop(0)
		ans += x * z
		print(x, y, end=' ')
	if oddFlag:
		print(-1 * heapq.heappop(ev))
	else:
		print()
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr1 = sorted([arr[i] for i in range(0, n, 2)], reverse=True)
	arr2 = sorted([arr[i] for i in range(1, n, 2)])
	for i in range(0, n, 2):
		arr[i] = arr1[i // 2]
	for i in range(1, n, 2):
		arr[i] = arr2[i // 2]
	ev = sum(arr1)
	od = sum(arr2)
	res = 0
	for i in range(0, n, 2):
		res += arr[i] * od
		if i + 1 < n:
			ev -= arr[i]
			od -= arr[i + 1]
	print(*arr)
	print(res)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	ei = []
	oi = []
	for i in range(n):
		if i % 2 == 0:
			ei.append(li[i])
		else:
			oi.append(li[i])
	ei.sort(reverse=True)
	oi.sort()
	i = 0
	j = 0
	ki = []
	s = 0
	while i < len(ei) or j < len(oi):
		if i < len(ei):
			ki.append(ei[i])
			i = i + 1
		if j < len(oi):
			ki.append(oi[j])
			s = s + oi[j]
			j = j + 1
	c = 0
	for i in range(len(ei)):
		c = c + s * ei[i]
		if i < len(oi):
			s = s - oi[i]
	print(*ki)
	print(c)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	if n == 1:
		print(*a)
		print(0)
		continue
	(zero, one) = ([], [])
	for j in range(n):
		if j & 1:
			one.append(a[j])
		else:
			zero.append(a[j])
	zero.sort(reverse=True)
	one.sort()
	to = 0
	for j in range(len(one)):
		to += zero[j]
		ans += to * one[j]
	(x, y) = (0, 0)
	for j in range(n):
		if j % 2 == 0:
			print(zero[x], end=' ')
			x += 1
		else:
			print(one[y], end=' ')
			y += 1
	print()
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = arr[::2]
	b = arr[1::2]
	a.sort(reverse=True)
	b.sort()
	arr[::2] = a
	arr[1::2] = b
	c = 0
	for i in range(0, n, 2):
		c += arr[i] * sum(arr[i + 1::2])
	print(*arr)
	print(c)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	even = a[0::2]
	even.sort(reverse=True)
	a[0::2] = even
	odd = a[1::2]
	odd.sort()
	a[1::2] = odd
	for i in range(0, len(even)):
		a[int(2 * i)] = even[i]
	for j in range(0, len(odd)):
		a[int(2 * j + 1)] = odd[j]
	pos = 0
	for k in range(0, n, 2):
		pos = pos + a[k] * sum(a[k + 1::2])
	print(*a)
	print(pos)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = sorted(l[0::2], reverse=True)
	y = sorted(l[1::2])
	z = sum(y)
	total = 0
	f = []
	for i in range(len(y)):
		total += x[i] * z
		z -= y[i]
		f.append(x[i])
		f.append(y[i])
	if n % 2 != 0:
		f.append(x[len(x) - 1])
	print(*f)
	print(total)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr[0::2] = sorted(arr[0::2], reverse=True)
	arr[1::2] = sorted(arr[1::2])
	arr_right_sum = sum(arr[1::2])
	print(*arr)
	ans = 0
	for j in range(len(arr[1::2])):
		ans += arr[0::2][j] * arr_right_sum
		arr_right_sum -= arr[1::2][j]
	print(ans)


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


t = int(input())
while t != 0:
	n = int(input())
	arr = [int(x) for x in input().split()]
	z = []
	o = []
	l = []
	total = 0
	for i in range(len(arr)):
		if i % 2 == 0:
			z.append(arr[i])
		else:
			o.append(arr[i])
	o.sort()
	z.sort(reverse=True)
	for i in range(int(n / 2)):
		l.append(z[i])
		l.append(o[i])
	if n % 2 != 0:
		l.append(z[-1])
	for j in range(len(l)):
		s = 0
		if j % 2 == 0:
			for i in range(j + 1, len(l), 2):
				s += l[i]
			total += l[j] * s
	print(*l)
	print(total)
	t -= 1


t = int(input())
while t != 0:
	n = int(input())
	c = list(map(int, input().split()))[:n]
	zero = [c[i] for i in range(n) if i % 2 == 0]
	one = [c[i] for i in range(n) if i % 2 != 0]
	zero.sort(reverse=True)
	one.sort()
	(j, k) = (0, 0)
	zeros = 0
	net = 0
	ans = []
	for i in range(n):
		if i % 2 == 0:
			ans.append(zero[j])
			zeros += zero[j]
			j += 1
		else:
			ans.append(one[k])
			net += zeros * one[k]
			k += 1
	print(*ans)
	print(net)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = sorted(arr[::2], reverse=True)
	b = sorted(arr[1::2])
	(ans, flag, i, j) = (0, 0, 0, 0)
	for _ in range(n):
		if flag:
			print(b[j], end=' ')
			j += 1
			flag = 0
		else:
			print(a[i], end=' ')
			i += 1
			flag = 1
	for i in range(len(a)):
		ans += sum(b[i:]) * a[i]
	print()
	print(ans)


def binary(n, arr):
	zeros = []
	ones = []
	ans = []
	res = 0
	for i in range(0, n, 2):
		zeros.append(arr[i])
	for i in range(1, n, 2):
		ones.append(arr[i])
	zeros.sort(reverse=True)
	ones.sort()
	(i, j) = (0, 0)
	while i < len(zeros) or j < len(ones):
		if i < len(zeros):
			ans.append(zeros[i])
		if j < len(ones):
			ans.append(ones[j])
		i += 1
		j += 1
	for i in ans:
		print(i, end=' ')
	print('\r')
	for i in range(len(zeros)):
		if i < len(ones):
			res += zeros[i] * sum(ones[i:])
	print(res)
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	binary(n, arr)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	ol = []
	el = []
	while i < len(l):
		if i % 2 == 0:
			ol.append(l[i])
			i += 1
		else:
			el.append(l[i])
			i += 1
	ol.sort(reverse=True)
	el.sort()
	ln = []
	for j in range(len(l)):
		if j % 2 == 0:
			ln.append(ol[j // 2])
		else:
			ln.append(el[j // 2])
	zc = 0
	sub = 0
	for k in range(len(ln)):
		if k % 2 == 0:
			zc += ln[k]
		else:
			sub = sub + zc * ln[k]
		print(ln[k], end=' ')
	print()
	print(sub)


from sys import stdin

def main(T):
	while T > 0:
		N = int(stdin.readline())
		C = list(map(int, stdin.readline().split()))
		C1 = [C[i] for i in range(0, N, 2)]
		C1.sort(reverse=True)
		C2 = [C[i] for i in range(1, N, 2)]
		C2.sort()
		temp = 0
		if N % 2 != 0:
			temp = C1.pop()
		total = 0
		j = 0
		k = 0
		for i in C1:
			for j in range(k, N // 2):
				total += i * C2[j]
			k += 1
		for i in range(0, N // 2):
			print(C1[i], C2[i], end=' ')
		if N % 2 != 0:
			print(temp)
		else:
			print()
		print(total)
		T -= 1
main(int(stdin.readline()))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	b = []
	c = []
	d = []
	for i in range(n):
		if i % 2 == 0:
			b.append(a[i])
		if i % 2 == 1:
			c.append(a[i])
	b.sort()
	c.sort()
	b = b[::-1]
	x = min(len(b), len(c))
	y = max(len(b), len(c))
	for i in range(x):
		d.append(b[i])
		d.append(c[i])
	if len(b) != len(c):
		if len(b) > len(c):
			for j in range(x, y):
				d.append(b[j])
		else:
			for j in range(x, y):
				d.append(b[j])
	print(*d)
	z = len(d)
	sum1 = 0
	for i in range(0, z, 2):
		temp = 0
		for j in range(i + 1, z, 2):
			temp += d[j]
		temp = temp * d[i]
		sum1 += temp
	print(sum1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	e = []
	o = []
	for i in range(n):
		if i % 2 == 0:
			e.append(l[i])
		else:
			o.append(l[i])
	e.sort()
	e = e[::-1]
	o.sort()
	o = o[::-1]
	p = []
	s = 0
	for i in range(len(o)):
		p.append(o[i] + s)
		s = p[i]
	p = p[::-1]
	ans = 0
	for i in range(len(o)):
		ans += e[i] * p[i]
	o = o[::-1]
	for i in range(len(o)):
		print(e[i], end=' ')
		print(o[i], end=' ')
	if n % 2 != 0:
		print(e[-1])
	else:
		print()
	print(ans)


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


for i in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	i = 0
	li1 = []
	li2 = []
	li3 = []
	cnt0 = 0
	ans = 0
	for j in li:
		if i % 2 == 0:
			li1.append(j)
		else:
			li2.append(j)
		i += 1
	li1.sort(reverse=True)
	li2.sort()
	for _ in range(n):
		if _ % 2 == 0:
			li3.append(li1[0])
			li1.pop(0)
		else:
			li3.append(li2[0])
			li2.pop(0)
	for i in li3:
		print(i, end=' ')
	print()
	if n % 2 != 0:
		n -= 1
	even = 2
	for k in range(n, 0, -1):
		if even % 2 == 0:
			cnt0 += li3[k - 1]
		else:
			ans += cnt0 * li3[k - 1]
		even += 1
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	s = []
	s1 = []
	for i in range(0, N, 2):
		s.append(A[i])
	for i in range(1, N, 2):
		s1.append(A[i])
	s1.sort()
	summ = sum(s)
	s.sort(reverse=True)
	tsum = sum(s1)
	ans = 0
	for i in range(N):
		if i % 2 == 0:
			A[i] = s[i // 2]
		else:
			A[i] = s1[i // 2]
	i = 0
	while i + 1 < N:
		ans += A[i] * tsum
		tsum -= A[i + 1]
		i += 2
	print(*A)
	print(ans)


def solve(s, n):
	s[::2] = sorted(s[::2], reverse=True)
	s[1::2] = sorted(s[1::2])
	print(*s)
	(ans, sm) = (0, 0)
	for i in range(1, n, 2):
		sm += s[i - 1]
		ans += s[i] * sm
	print(ans)
	return
t = int(input())
while t != 0:
	n = int(input())
	s = list(map(int, input().split()))
	solve(s, n)
	t -= 1


import sys

def get_string():
	return sys.stdin.readline().strip()

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

def get_num():
	return int(sys.stdin.readline())
test = get_num()
for tests in range(test):
	n = get_num()
	c = get_array()
	z = []
	o = []
	for i in range(len(c)):
		if i % 2 == 0:
			z.append(c[i])
		else:
			o.append(c[i])
	z.sort(reverse=True)
	o.sort()
	p = 0
	q = 0
	for i in range(len(c)):
		if i % 2 == 0:
			c[i] = z[p]
			p += 1
		else:
			c[i] = o[q]
			q += 1
	pref = []
	summ = 0
	for i in range(len(o) - 1, -1, -1):
		summ += o[i]
		pref.append(summ)
	pref.reverse()
	res = 0
	for i in range(len(o)):
		res += pref[i] * z[i]
	for i in range(len(c)):
		print(c[i], end=' ')
	print()
	print(res)


from itertools import accumulate
for x in range(int(input())):
	(_, l, z, o, sol, ll) = (input(), [int(x) for x in input().split()], [], [], 0, [])
	for (i, x) in enumerate(l):
		o.append(x) if i % 2 else z.append(x)
	(z, o) = (sorted(z, reverse=True), sorted(o))
	p = list(accumulate(o[::-1]))[::-1]
	for (i, x) in enumerate(z[:len(o)]):
		sol += x * p[i]
	for (x, y) in zip(z, o):
		ll.extend([x, y])
	if len(o) != len(z):
		ll.append(z[-1])
	print(*ll)
	print(sol)


t = int(input())
while t > 0:
	n = int(input())
	ar = list(map(int, input().split()))
	even = []
	odd = []
	for i in range(n):
		if i % 2 == 0:
			even.append(ar[i])
		else:
			odd.append(ar[i])
	even.sort(reverse=True)
	odd.sort()
	l = []
	for i in range(len(odd)):
		l.append(even[i])
		l.append(odd[i])
	if len(odd) + 1 == len(even):
		l.append(even[-1])
	for i in range(1, len(even)):
		even[i] = even[i] + even[i - 1]
	s = 0
	for i in range(len(odd)):
		s += odd[i] * even[i]
	for i in l:
		print(i, end=' ')
	print()
	print(s)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(one, zero) = ([], [])
	for i in range(n):
		if i % 2 == 0:
			zero.append(arr[i])
		else:
			one.append(arr[i])
	ans = []
	(a, b) = (0, 0)
	zero.sort(reverse=True)
	one.sort()
	for i in range(n):
		if i % 2 == 0:
			ans.append(zero[a])
			a += 1
		else:
			ans.append(one[b])
			b += 1
	count = 0
	for i in range(n):
		if i % 2 == 0:
			temp = 0
			for j in range(i + 1, n, 2):
				temp += ans[j]
			count += temp * ans[i]
	for i in range(n):
		print(ans[i], end=' ')
	print()
	print(count)


n = int(input().splitlines()[0])
for i in range(n):
	N = int(input().splitlines()[0])
	A = [int(e) for e in input().split(' ')]
	zeros = sorted([A[i] for i in range(0, N, 2)], reverse=True)
	ones = sorted([A[i] for i in range(1, N, 2)])
	count = 0
	a = len(zeros)
	b = len(ones)
	m = min(a, b)
	q = []
	for k in range(m):
		q.append(zeros[k])
		q.append(ones[k])
		count += zeros[k] * sum(ones[k:])
	if a != b:
		q.append(zeros[a - 1])
	print(*q)
	print(count)


import sys
input = sys.stdin.readline
_ = 1
_ = int(input())
while _ > 0:
	_ -= 1
	n = int(input())
	l = [int(x) for x in input().split()]
	(ze, on) = ([], [])
	ans = []
	su = 0
	for x in range(n):
		if x % 2 == 0:
			ze.append(l[x])
		else:
			su += l[x]
			on.append(l[x])
	ze.sort(reverse=True)
	on.sort()
	for x in range(n // 2):
		ans.append(ze[x])
		ans.append(on[x])
	if n & 1:
		ans.append(ze[-1])
	res = ze[0] * su
	for x in range(1, n // 2):
		su -= on[x - 1]
		res += ze[x] * su
	sys.stdout.write(' '.join(map(str, ans)) + '\n')
	sys.stdout.write(str(res) + '\n')


import io, os, sys
from itertools import accumulate

def solve():
	num = int(input())
	lis = list(map(int, input().split()))
	lis[::2] = sorted(lis[::2], reverse=True)
	lis[1::2] = sorted(lis[1::2])
	print(*lis)
	(prefix, total) = (0, 0)
	for index in range(1, num, 2):
		prefix += lis[index - 1]
		total += lis[index] * prefix
	print(total)
T = int(input())
while T:
	solve()
	T -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	zeros = []
	ones = []
	for i in range(n):
		if i % 2 == 0:
			zeros.append(arr[i])
		else:
			ones.append(arr[i])
	zeros = sorted(zeros, reverse=True)
	ones = sorted(ones)
	one = ones.copy()
	for i in range(len(ones) - 2, -1, -1):
		ones[i] = ones[i + 1] + ones[i]
	ans = 0
	if len(zeros) == len(ones):
		for i in range(len(zeros)):
			ans += zeros[i] * ones[i]
			fin = []
		for i in range(n // 2):
			fin.append(zeros[i])
			fin.append(one[i])
	else:
		for i in range(len(zeros) - 1):
			ans += zeros[i] * ones[i]
		fin = []
		for i in range(n // 2):
			fin.append(zeros[i])
			fin.append(one[i])
		fin.append(zeros[-1])
	print(*fin)
	print(ans)


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	zero_array = []
	one_array = []
	for j in range(len(A)):
		if j % 2 == 0:
			zero_array.append(A[j])
		else:
			one_array.append(A[j])
	zero_array.sort()
	zero_array = zero_array[::-1]
	one_array.sort()
	new_A = ['X'] * N
	for j in range(len(zero_array)):
		new_A[2 * j] = zero_array[j]
	for j in range(len(one_array)):
		new_A[2 * j + 1] = one_array[j]
	new_sum = 0
	for j in range(len(zero_array)):
		if j < len(one_array):
			new_sum += zero_array[j] * sum(one_array[j:])
		else:
			pass
	print(*new_A)
	print(new_sum)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	o = []
	e = []
	for i in range(n):
		if i % 2 == 0:
			e.append(l[i])
		else:
			o.append(l[i])
	o1 = o.copy()
	e1 = e.copy()
	o1.reverse()
	e1.reverse()
	o.sort(reverse=True)
	e.sort()
	o1.sort()
	e1.sort(reverse=True)
	ans = []
	while True:
		if len(e) > 0:
			ans.append(e.pop())
		if len(o) > 0:
			ans.append(o.pop())
		if len(e) == 0 and len(o) == 0:
			break
	cnt = 0
	for i in range(len(e1)):
		for j in range(i, len(o1)):
			cnt += e1[i] * o1[j]
	print(*ans)
	print(cnt)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(l1, l2) = ([], [])
	sum = 0
	for i in range(n):
		if i % 2 == 0:
			l1.append(arr[i])
		else:
			l2.append(arr[i])
			sum = sum + arr[i]
	l1.sort(reverse=True)
	l2.sort()
	j = 0
	l = len(l2)
	ans = 0
	for i in l1:
		if j < l:
			ans = ans + i * sum
			sum = sum - l2[j]
		j = j + 1
	j = 0
	k = 0
	for i in range(n):
		if i % 2 == 0:
			print(l1[j], end=' ')
			j = j + 1
		else:
			print(l2[k], end=' ')
			k = k + 1
	print()
	print(ans)


for __ in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	Ones = [L[2 * i + 1] for i in range(N // 2)]
	Zeros = [L[2 * i] for i in range((N + 1) // 2)]
	Ones.sort()
	Zeros.sort(reverse=True)
	C = [0] * N
	for i in range(N):
		C[i] = Zeros[i // 2] if i % 2 == 0 else Ones[(i - 1) // 2]
	subsequences = Z = O = 0
	for i in range(N // 2):
		Z += Zeros[i]
		subsequences += Ones[i] * Z
	print(*C)
	print(subsequences)


for _ in range(int(input())):
	x = int(input())
	arr = list(map(int, input().split()))
	arr_zeros = []
	arr_ones = []
	cnt = 0
	while cnt < x:
		if cnt % 2 == 0:
			arr_zeros.append(arr[cnt])
		else:
			arr_ones.append(arr[cnt])
		cnt += 1
	arr_zeros.sort(reverse=True)
	arr_ones.sort()
	num_seq = 0
	pref_zeros = []
	for i in arr_zeros:
		if pref_zeros == []:
			pref_zeros.append(i)
		else:
			pref_zeros.append(pref_zeros[-1] + i)
	(len1, len2) = (len(arr_zeros), len(arr_ones))
	for i in range(len2):
		num_seq += arr_ones[i] * pref_zeros[i]
	new_arr = []
	new_string = ''
	for (x, y) in zip(arr_zeros, arr_ones):
		new_arr.append(x)
		new_arr.append(y)
	if len1 > len2:
		new_arr.append(arr_zeros[-1])
	elif len1 < len2:
		new_arr.append(arr_ones[-1])
	new_arr = list(map(str, new_arr))
	print(' '.join(new_arr))
	print(num_seq)


a = int(input())
for i in range(a):
	b = int(input())
	li = list(map(int, input().split()))
	c = []
	d = []
	for i in range(b):
		if i % 2 != 0:
			c.append(li[i])
		else:
			d.append(li[i])
	c.sort()
	d.sort(reverse=True)
	sumi = 0
	for i in range(len(c)):
		print(d[i], c[i], end=' ')
	if b % 2 != 0:
		print(d[-1], end=' ')
	print()
	for i in range(len(c)):
		sumi += d[i] * sum(c[i:])
	print(sumi)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	ev = []
	od = []
	for i in range(0, n):
		if i % 2 == 0:
			ev.append(li[i])
		else:
			od.append(li[i])
	ev.sort(reverse=True)
	od.sort()
	e = f = 0
	i = 0
	t = ''
	zer = one = 0
	while i < n:
		if i % 2 == 0:
			print(ev[e], end=' ')
			t += '0' * ev[e]
			zer += ev[e]
			e += 1
		else:
			print(od[f], end=' ')
			t += '1' * od[f]
			one += od[f]
			f += 1
		i += 1
	print()
	ans = 0
	for i in range(len(t)):
		if t[i] == '0':
			ans += one
			zer -= 1
		else:
			one -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	c = list(map(int, input().split()))
	n0 = []
	n1 = []
	for i in range(n):
		if i % 2 == 0:
			n0.append(c[i])
		else:
			n1.append(c[i])
	n0.sort(reverse=True)
	n1.sort()
	new = []
	for i in range(n // 2):
		new.append(n0[i])
		new.append(n1[i])
	if n % 2 != 0:
		new.append(n0[-1])
	print(*new)
	cnt = 0
	cnt1 = 0
	for i in range(len(n1)):
		cnt += n0[i]
		cnt1 += cnt * n1[i]
	print(cnt1)


from sys import stdin, stdout

def sub_seq(arr, so, se):
	ans = 0
	ce = 0
	for i in range(len(arr)):
		if i & 1 == 0:
			ans += (se - ce) * arr[i]
		else:
			ce += arr[i]
	return ans

def mgc(arr, n):
	(zer, one) = ([], [])
	for i in range(n):
		if i & 1 == 0:
			zer.append(arr[i])
		else:
			one.append(arr[i])
	zer.sort(reverse=True)
	one.sort()
	ans_arr = []
	i = 0
	while i < len(zer) and i < len(one):
		ans_arr.append(zer[i])
		ans_arr.append(one[i])
		i += 1
	while i < len(zer):
		ans_arr.append(zer[i])
		i += 1
	while i < len(one):
		ans_arr.append(one[i])
		i += 1
	for i in ans_arr:
		print(i, end=' ')
	print()
	return sub_seq(ans_arr, sum(zer), sum(one))
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	print(mgc(arr, n))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = [l[i] for i in range(0, len(l), 2)]
	b = [l[i] for i in range(1, len(l), 2)]
	r = 0
	a = sorted(a, reverse=True)
	b = sorted(b)
	c = 0
	for i in range(len(a)):
		c = c + sum(b[i:]) * a[i]
	while r < len(a) and r < len(b):
		print(a[r], b[r], end=' ')
		r = r + 1
	if r == len(a) and r != len(b):
		print(b[len(b) - 1])
	elif r == len(b) and r != len(a):
		print(a[len(a) - 1])
	print()
	print(c)


t = int(input())
for k in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l2 = []
	for i in range(n):
		if i % 2 == 0:
			l1.append(l[i])
		else:
			l2.append(l[i])
	l1.sort()
	l2.sort()
	l1.reverse()
	s = 0
	a = []
	b = []
	for i in range(len(l1)):
		a.append(l1[i])
		if i < len(l2):
			a.append(l2[i])
		s = s + l1[i] * sum(l2[i:])
	print(*a)
	print(s)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = []
	e = []
	t = 0
	for i in range(len(c)):
		if i % 2 == 0:
			d.append(c[i])
		else:
			e.append(c[i])
	d.sort(reverse=True)
	e.sort()
	if len(d) == len(e):
		for i in range(len(d)):
			t += d[i] * sum(e[i:])
			print(d[i], e[i], end=' ')
		print()
		print(t)
	elif len(d) > len(e):
		for i in range(len(d) - 1):
			t += d[i] * sum(e[i:])
			print(d[i], e[i], end=' ')
		print(d[len(d) - 1])
		print(t)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	z_arr = []
	o_arr = []
	for i in range(n):
		if i % 2 == 0:
			z_arr.append(arr[i])
		else:
			o_arr.append(arr[i])
	an_arr = []
	z_arr = sorted(z_arr, reverse=True)
	o_arr = sorted(o_arr)
	j = 0
	k = 0
	mid = n // 2
	for i in range(mid):
		an_arr.append(z_arr[i])
		an_arr.append(o_arr[i])
	if n % 2 == 1:
		an_arr.append(z_arr[-1])
	print(*an_arr)
	p = 0
	q = 0
	for l in range(len(z_arr)):
		for m in range(l, len(o_arr)):
			p = p + o_arr[m]
		q = q + z_arr[l] * p
		p = 0
	print(q)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = []
	ans = [0] * len(a)
	for i in range(len(a)):
		if i % 2 == 0:
			b.append(a[i])
		else:
			c.append(a[i])
	ccc = sum(c)
	b.sort(reverse=True)
	c.sort()
	bb = 0
	cc = 0
	for i in range(len(a)):
		if i % 2 == 0:
			ans[i] = b[bb]
			bb += 1
		else:
			ans[i] = c[cc]
			cc += 1
	count = 0
	for i in range(1, len(ans), 2):
		count += ans[i - 1] * ccc
		ccc -= ans[i]
	print(*ans)
	print(count)


for tc in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	q0 = []
	q1 = []
	tot = 0
	for x in range(len(ls)):
		if x % 2 == 0:
			q0.append(ls[x])
		else:
			q1.append(ls[x])
			tot += ls[x]
	q0.sort(reverse=True)
	q1.sort()
	res = []
	cnt = 0
	itr0 = itr1 = 0
	(n, m) = (len(q0), len(q1))
	while itr0 < n or itr1 < m:
		if itr0 < n:
			a = q0[itr0]
			res.append(a)
			cnt += tot * a
			itr0 += 1
		if itr1 < m:
			a = q1[itr1]
			res.append(a)
			tot -= a
			itr1 += 1
	for x in res:
		print(x, end=' ')
	print()
	print(cnt)


from sys import stdin
input = stdin.readline

def answer():
	(even, odd) = ([], [])
	for i in range(n):
		if i & 1 == 0:
			even.append(a[i])
		else:
			odd.append(a[i])
	even.sort()
	odd.sort(reverse=True)
	(ans, value, total) = ([], 0, sum(odd))
	for i in range(n):
		if i & 1 == 0:
			v = even.pop()
			ans.append(v)
			value += v * total
		else:
			v = odd.pop()
			ans.append(v)
			total -= v
	print(*ans)
	print(value)
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	answer()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))[:n]
	even = []
	odd = []
	for i in range(n):
		if i % 2 == 0:
			even.append(l[i])
		else:
			odd.append(l[i])
	even.sort(reverse=True)
	e = len(even)
	odd.sort()
	o = n - e
	c = 0
	s = sum(odd)
	for i in range(o):
		c = c + even[i] * s
		s = s - odd[i]
	i = 0
	j = 0
	k = 0
	while k < n:
		if k % 2 == 0:
			l[k] = even[i]
			i += 1
		else:
			l[k] = odd[j]
			j += 1
		k += 1
	for i in l:
		print(i, end=' ')
	print()
	print(c)


for _ in range(int(input())):
	n = int(input())
	c = list(map(int, input().split()))
	c_e = list(reversed(sorted(c[::2])))
	c_o = sorted(c[1::2])
	new_list = [item for pair in zip(c_e, c_o + [0]) for item in pair]
	ans = 0
	for i in range(len(c_e)):
		l = c_o[i:]
		ans += sum(list((j * c_e[i] for j in l)))
	print(*new_list[:n], sep=' ')
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr1 = []
	arr2 = []
	for i in range(0, n, 2):
		arr1.append(arr[i])
	for i in range(1, n, 2):
		arr2.append(arr[i])
	arr2 = sorted(arr2)
	arr1 = sorted(arr1, reverse=True)
	arr3 = []
	ans = 0
	t = min(len(arr1), len(arr2))
	for i in range(t):
		arr3.append(arr1[i])
		arr3.append(arr2[i])
		ans += arr1[i] * sum(arr2[i:])
	if len(arr1) != len(arr2):
		arr3.append(arr1[len(arr1) - 1])
	print(*arr3)
	print(ans)


def solve(n, lis):
	zero = []
	one = []
	tot = 0
	for i in range(n):
		if i % 2 == 0:
			zero.append(lis[i])
		else:
			one.append(lis[i])
			tot += lis[i]
	zero.sort()
	zero.reverse()
	one.sort()
	count = 0
	j = 0
	for i in range(n // 2):
		count = count + zero[i] * tot
		tot -= one[i]
		lis[j] = zero[i]
		j += 1
		lis[j] = one[i]
		j += 1
	if n % 2 == 1:
		lis[-1] = zero[-1]
	print(*lis)
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	lis = [int(x) for x in input().split()]
	ans = solve(n, lis)
	print(ans)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	one = []
	zero = []
	res = 0
	finList = []
	for i in range(n):
		if i % 2 == 0:
			zero.append(l[i])
		else:
			one.append(l[i])
	sOne = sum(one)
	one.sort()
	zero.sort(reverse=True)
	for j in range(len(zero)):
		res += zero[j] * sOne
		finList.append(zero[j])
		if j < len(one):
			sOne = sOne - one[j]
			finList.append(one[j])
	print(*finList)
	print(res)


for zz in range(int(input())):
	a = int(input())
	l = list(map(int, input().split()))
	x = []
	y = []
	b = c = co = 0
	for i in range(a):
		if co % 2 == 0:
			x.append(l[i])
			b += 1
		else:
			y.append(l[i])
			c += 1
		co += 1
	x.sort()
	x = x[::-1]
	y.sort()
	l1 = []
	count = 0
	for i in range(a):
		if i % 2 == 0:
			aa = x.pop(0)
			l1.append(aa)
			count += aa * sum(y)
		else:
			l1.append(y.pop(0))
	print(*l1)
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(e, o) = ([], [])
	for i in range(n):
		if i % 2 == 0:
			e.append(arr[i])
		else:
			o.append(arr[i])
	e.sort(reverse=True)
	o.sort()
	i = 0
	temp = []
	while e or o:
		if i % 2 == 0:
			temp.append(e.pop(0))
			i += 1
		else:
			temp.append(o.pop(0))
			i += 1
	ans = 0
	for i in range(n):
		if i % 2 == 0:
			for j in range(i + 1, n, 2):
				ans += temp[i] * temp[j]
	print(' '.join(map(str, temp)))
	print(ans)