import math

# Helper functions at top
def readint():
    return int(input())
def intline():
    return [int(e) for e in input().split()]

def mod_add(a, b):
    return (a + b) % MOD
def mod_mul(a, b):
    return a * b % MOD
def mod_pow(a, b):
    if b == 0:
        return 1
    rt = mod_pow(a, b // 2)
    res = mod_mul(rt, rt)
    if b % 2:
        res = mod_mul(res, a)
    return res
def inverse(x):
    return mod_pow(x, MOD - 2)
def mod_div(a, b):
    return mod_mul(a, inverse(b))

# To read input from file
# import sys
# sys.stdin = open("input.txt", "r")

_cases = True
t = 1
if _cases:
    t = int(input())

class Solver:

    def solve(tc):
        n = readint()
        a = intline()
        s = sum(a)
        avg = s // n
        extra = s - avg * n
        ans = extra * (n - extra)
        print(ans)

for tc in range(1, t + 1):
    Solver.solve(tc)
