from sys import *
from math import *
#from collections import Counter
input = lambda:stdin.readline()
 
int_arr = lambda : list(map(int,stdin.readline().strip().split()))
str_arr = lambda :list(map(str,stdin.readline().split()))
get_str = lambda : map(str,stdin.readline().strip().split())
get_int = lambda: map(int,stdin.readline().strip().split())
get_float = lambda : map(float,stdin.readline().strip().split())
 
 
setrecursionlimit(1000)

for _ in range(int(input())):
    n = int(input())
    arr = int_arr()

    tot = 0
    val = sum(arr) / n
    if int(val + 0.5) == ceil(val):
        val = ceil(val)
    else:
        val = int(val)
    for i in range(n):
        tot += arr[i] - val
    tot = abs(tot)
    print((n - tot) * tot)
