
from collections import Counter,defaultdict,deque
#from heapq import *
#from itertools import *
#from operator import itemgetter
#from itertools import count, islice
#from functools import reduce
#alph = 'abcdefghijklmnopqrstuvwxyz'
#n,x = [int(_) for _ in input().split()]
#s = input().strip()
#sarr = [x for x in input().strip().split()]
import math
#from math import *
#from heapq import *
import sys
input=sys.stdin.readline
#sys.setrecursionlimit(2**30)
mod = 10**9+7
#ons = ['no','yes']


def solve():
    n = int(input())
    arr = [int(_) for _ in input().split()]
    s = sum(arr)
    r = s%n
    print(r*(n-r))

tt = int(input())
for test in range(tt):
    solve()
















#
