t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    suma = 0
    for x in a:
        suma += x
    diff = suma // n
    larger = suma - diff * n
    less = n - larger
    print(less * larger)