import java.util.*;
public class count_set_bits 
   
    {
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        while(t-->0)
        {
        int n=in.nextInt();
        long sum=0;
        for (int i=0;i<n ;i++ ) {
            sum+= in.nextLong();
        }

        long x  = sum %n;

        System.out.println(x*(n-x));
        }
        in.close();
    }
    
}
