t=int(input())
m=[]
for i in range(t):
    s=0
    n=int(input())
    a=list(map(int,input().split()))
    b,av=sum(a)%n,sum(a)/n
    a[:b],a[b:]=[int(av)]*(b),[int(av)+1]*(n-b)
    for i in range(len(a)):
        """for j in range(i+1,len(a)):
            s+=abs(a[i]-a[j])"""
        if i<b:
            s+=(n-b)
    m+=[s]
for i in m:
    print(int(i))
