import re
inputs = []
while True:
    try:
        line = input()
    except EOFError:
        break
    inputs.append(line)

n = inputs[0]
cn = 0
x = 0
a = 0
for i in range(1, int(n) + 1):
    cn = int(inputs[2 * i -1])
    x = sum(map(int, re.findall('\d+', inputs[2 * i])))
    a = x%cn
    print(a*(cn-a))



