import sys
import bisect
#import math
#import itertools

def get_line(): return list(map(int, sys.stdin.readline().strip().split()))
def in1(): return int(input())
def decimalToBinary(n): return bin(n).replace("0b", "")

for _ in range(in1()):
    n=in1()
    a=get_line()
    t1=sum(a)
    if t1%n==0:
        print(0)
    else:
        t=t1//n
        c=0
        t2=t1%n
        print((n-t2)*t2)






