import math

m = input()
for _ in range(int(m)):
    k1 = int(input())
    k2 = list(map(int, input().split()))
    cars = sum(k2)

    avg = cars/k1
    if(avg > 1):
        for i in range(len(k2)):
            avg = cars/k1
            p = math.ceil(avg)
            k2[i] = p

            cars -= p
            k1 -= 1
        
        
    sums = 0
    sums = 0
    for i in range(1, len(k2) - 1):
        sums += (abs(k2[0] - k2[i]))*i
    print(sums)
