from math import ceil

t = int(input())
for _ in range(t):
    n = int(input())
    given_nums = map(int, input().split())
    num_sum = sum(given_nums)
    remainder = num_sum % n
    avg = ceil(num_sum / n)
    decreased_count = avg * n - num_sum
    res = decreased_count * (n - decreased_count)
    print(res)