import math, sys
from collections import defaultdict, Counter, deque 
from heapq import heapify, heappush, heappop

MOD = int(1e9) + 7
INF = float('inf')


def solve():
	n = int(input())
	arr = list(map(int, input().split()))

	total = sum(arr)

	c1 = total // n
	have = total - c1 * n

	c2 = have - c1
	# print(c1, c2, have)
	print(abs(c2 - c1) * have)



def main():
	ts = 1
	ts = int(input())
	for t in range(1, ts + 1):
		solve()






def input():
	return sys.stdin.readline().rstrip('\n').strip()

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(sep)
		sys.stdout.write(str(arg))
		first = False 

	sys.stdout.write(end)

def flush():
	sys.stdout.flush()


main()


