import java.util.Arrays;
import java.util.Scanner;

public class traffic {
    public static void main(String [] args){
        Scanner console = new Scanner(System.in);
        long numCases = console.nextInt();
        while (numCases-->0){
            int length = console.nextInt();
            long [] cars = new long[length];
            long sum = 0;
            for(int i=0;i<length;i++){
                long temp = console.nextInt();
                cars[i] = temp;
                sum+=temp;
            }
            long add = sum/length;
            long extra = sum- add*length;
            long count = 0;
            for(int i=0;i<length;i++){
                cars[i] = add;
                if(extra>0){
                    cars[i]+=1;
                    extra--;
                }else{
                    count++;
                }
            }
            System.out.println(count*(length-count));
        }
    }
}
