import math
n=int(input())
while(n!=0):
    n-=1
    t=int(input())
    a=list(map(int,input().split()))
    new=0
    for i in range(len(a)):
        new+=a[i]
    k=new%t
    ans=k*(t-k)
    print(ans)