import math
 
m = input()
 
for i in range(int(m)):
    track = int(input())
    cars = list(map(int, input().split()))
    total_cars = sum(cars)
 
    temp_track = track
    max_total = 1
    max_elm = 1
    for j in range(len(cars)):
        avg = total_cars/temp_track
        p = math.ceil(avg)
        cars[j] = p
 
        total_cars -= p
        temp_track -= 1
 
        if(j == 0):
            max_elm = p
            max_total = 1
        else:
            if(p == max_elm):
                max_total += 1
            else:
                min_elm = p
 
    if  max_total == track:
        min_elm = max_elm
        min_total = 0
    else:
        min_total = track - max_total
        
    print(abs(max_elm - min_elm)*max_total*min_total)