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:
        m=sm//(n-1)
        t=sm%(n-1)
        
        print((m-t)*(n-(m-t)))
    
            