for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    al = sum(a)//n
    rem = sum(a)%n
    res = 0
    res = rem*(n-rem)
    # for i in a:
    #     res+=max(0,al-i)
    print(res)
