import math
t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    if (sum(arr)%n==0 and sum(arr)>=n):
        print(0)
    else:
        s = sum(arr)
        rem = s%n
        res = (n-rem) * rem
        print(res)