import math
t = int(input())
for i in range(t):
    n = int(input())
    ar = list(map(int,input().strip().split()))[:n]
    total = sum(ar)
    a = total // n
    b = total % n
    a = n - b
    ans = a * b
    print(ans)