



def solve():
    n=int(input())
    l=list(map(int,input().split()))
    if(sum(l)%n==0):
        print(0)
    elif(sum(l)<n):
        print(n)
    else:
        y=sum(l)//n
        ans=(y*n)
        diff=sum(l)-ans
        original=(n-diff)
        ans=(original)*(diff)
        print(ans)
        return












t=int(input())
while(t!=0):
    solve()
    t-=1
