t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))

	x = sum(arr)
	y = (n - x)

	if (x % n == 0):
		cnt = 0
	elif (x < n):
		cnt = (x * y)
	else:
		a = (x // n) 
		b = (x - a * n)

		cnt = ((n - b) * b)

	print(cnt)

