from math import floor,ceil
for _ in range(int(input())):
    n = int(input())
    lanes = list(map(int,input().split()))
    s = sum(lanes)
    avg = s/n
    if avg<=1:
        print((n-s)*s)
        continue
    l,h = floor(avg), ceil(avg)
    #lx + hy = s
    #x+y = n
    if h!=l:
        y = (s-n*l)//(h-l)
    else:
        y = 0
    print((n-y)*y)
