import math
import collections
from sys import stdin,stdout,setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2**20)

t = 1
t = int(stdin.readline())

for _ in range(t):
    n = int(stdin.readline())
    #s = stdin.readline().strip('\n')
    a = list(map(int, stdin.readline().rstrip().split()))
    
    su = sum(a)
    #print(su)
    if(su%n==0):
        print(0)
    else:
        tmp = su//n
        r = (su - tmp*n)
        
        print((n-r)*r)
    
    
    
    
    
    
    