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)*2)
        continue
    l,h = floor(avg), ceil(avg)
    #lx + hy = s
    i = 0
    while s>=l*i:
        inter = s-l*i
        if inter%h==0:
            break
        i+=1
    x  = i
    y = n-i
    print(x*y)
