def main():
    t = int(input())
    for i in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        
        total = sum(a)
        r = total % n
        k = n-r
        res = n*(n-1)//2
        if r > 1:
            res -= r*(r-1)//2
        if k > 1:
            res -= k*(k-1)//2
        print(res)

    
#main()

while True:
    try: main()
    except: break