#from itertools import product
#from itertools import combinations
from collections import Counter
#from collections import defaultdict
#from collections import deque # deque([iterable[, maxlen]]) #appendleft popleft rotate
#from heapq import heapify, heappop, heappush # func(heapifiedlist, item)
# sadness lies below
#from bisect import bisect_left, bisect_right, insort # func(sortedlist, item)
#from sys import setrecursionlimit

from sys import stdin, stderr
input = stdin.readline

def dbp(*args, **kwargs): # calling with dbp(locals()) is perfectly cromulent
	print(*args, file=stderr, **kwargs)

def get_int_list():
	return [int(x) for x in input().strip().split()]

def slow_match(n, extras):
	count = 0
	for i in range(n):
		for j in range(i+1, n):
			if i < extras and j >= extras:
				count += 1
	return count


def do_thing():
	n = int(input())
	alist = get_int_list()

	total = sum(alist)
	flravg, extras = divmod(sum(alist), len(alist))
	#sr = slow_match(n, extras)
	r = (n-extras) * extras
	#dbp(sr, r)
	#assert(sr==r)
	return r

def multicase():
	maxcc = int(input().strip())
	for cc in range(maxcc):
		print(do_thing())

if __name__ == "__main__":
	multicase()
	#print(do_thing())