import java.util.*;

public class Track {
    public static void main (String[] args) 
    {
        Scanner scan=new Scanner(System.in);
        int t=scan.nextInt();
        int j=0;
        int i=0;
        int z=0;
        while(t!=0)
        {   int avg=0;
            int sum=0;
            int rem=0;
            int x=0;
            int n=scan.nextInt();
            int a[]=new int[n];
            for( z=0;z<n;z++)
             {
               a[z]=scan.nextInt();
               sum=sum+a[z];
               
             }
             avg=sum/n;
             rem=sum%n;
             for(i=0;i<n;i++)
             {  
               a[i]=avg;
             }
             for(i=0;i<rem;i++)
             {   
               
                 a[i]=a[i]+1;
                 
             
             }
             for(i=0;i<n;i++)
             {
                 for(j=i+1;j<n;j++)
                 { 
                     x=x+Math.abs((a[i]-a[j]));
                 }
             }
             System.out.println(x);
             t--;
        }
    }
}