tests = int(input())

for _ in range(tests):
	n = int(input())
	t = list(map(int,input().split()))
	total = sum(t)
	if total < n :
		print((n - total)*total)
	else:
		res = total%n
		print((n - res)*res)