t = int(input())
for T in range(t):
    n = int(input())
    A = [int(i) for i in input().split()]
    s = sum(A)
    mean = s//n
    rem = s%n
    for i in range(n):
        if i<rem:
            A[i] = mean+1
        else:
            A[i] = mean
    A.sort()
    cursum = 0
    curnums = 0
    ans = 0
    for i in range(n):
        cursum += A[i]
        curnums += 1
        ans += curnums*A[i] - cursum
    print(ans)
        
