n=int(input())
for i in range(n):
    l=int(input())
    arr=[int(i) for i in input().split()]
    total=sum(arr)
    n=len(arr)
    net=total%n
    if net==0:
        print(0)
    else:
        print(net*(n-net))
        