#import math
#import defaultdict
#import itertools
for _ in range(int(input())):
    n = int(input())
    arr = [int(i) for i in input().split()]
    T = sum(arr)
    if T<n: print(T*(n-T))
    #print(T*n)
    elif T%n==0: print(0)
    else: quot = T//n; diff = T-quot*n; print((n-diff)*diff)