import sys
import os
from io import BytesIO

sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))

f = sys.stdin

line = lambda: f.readline().strip('\r\n').split()

def solve():
    s = sum(A)
    r = s%N
    
    res = r * (N - r)
    
    return str(res)

T = int(line()[0])
for test in range(1,T+1):
    N = int(line()[0])
    A = list(map(int,line()))
    sys.stdout.write(solve())
    sys.stdout.write("\n")
    
f.close()