# import math
for _ in range(int(input())):
    n = int(input())
    a, res = [int(i) for i in input().split()], 0
    total = sum(a)
    if total%n==0:print(res)
    elif total<n: print(total*(n-total))
    else:
        v1 = total//n; v2 = total - v1*n
        print((n-v2)*v2)
    