number_of_testcases = int(input())
for _ in range(number_of_testcases):
    n = int(input())
    arr = list(map(int,input().split()))
    sum1 = 0
    for i in range(n):
        sum1 = sum1 + arr[i]
    
    total_num = sum1%n 
    average = sum1//n 
    if sum1 != average*n:
        print(total_num*(n - total_num))
    
    else:
       print(0)
    
    