def main():
	for _ in range(int(input())):
		n=int(input())
		a=list(map(int,input().split()))
		if sum(a)%n==0:
			print(0)
			continue
		else:
			k=sum(a)
			s=0
			b=[k//n+1 for i in range(k%n)]
			b+=[k//n for i in range(n-k%n)]
			# print(b)
			b.sort()
			print((n-k%n)*(k%n))
if __name__=='__main__':
	main()