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

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