def main():
    t = int(input()) # Número de casos
    
    for _ in range(t):
        n = int(input()) # Número de sub carreteras
        a = list(map(int, input().split()))
        
        tot = sum(a)
        rest = tot - (tot//n * n)
        
        if tot % n == 0: print(0)
                                
        else: print(rest*(n-rest))
                    

if __name__ == "__main__":
    main()