for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    sm=sum(a)
    if sm<n:
        print((n-sm)*(sm))
    elif sm%n==0:
        print(0)
    else:
        t=sm%n
        print((t)*(n-t))