
import java.util.*;
import java.io.*;

public class P07 {
	static class FastReader{
		BufferedReader br ;
		StringTokenizer st;
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
			
		}
		String next() {
			while(st==null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				}catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return  Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
		float nextFloat() {
			return Float.parseFloat(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			}catch(IOException e) {
				e.printStackTrace();
			}
			return str ;
		}
	}
	
 	public static  void main(String[] args)throws IOException {
		FastReader fs = new FastReader();
		int t = fs.nextInt();
		while(t-->0) {
			int n = fs.nextInt();
			int a []= new int [n];
			int s =0;
			for(int i =0;i<n;i++) {
				a[i]= fs.nextInt();
				s+=a[i];
			}
			int b []= new int[n];
			for(int i =0;i<n;i++) {
				b[i]=(s/(n-i));
				s-=b[i];
			}
//			for(int i =0;i<n;i++) {
//				System.out.print(b[i]+" ");
//			}
//			System.out.println();
		//	System.out.println(s+" "+(double)s/(double)n);
			int inc=0;
			for(int i =0;i<1;i++) {
				int sum = 0;
				for(int j=0;j<n-1;j++) {
					for(int k =j+1;k<n;k++) {
						sum+=Math.abs(b[j]-b[k]);
					}
				}
				inc=sum;
			}
			System.out.println(inc);
		}
		
    }
	private static int gcd(int i, int j) {
		if(j==0) {
			return i;
		}
		return gcd(j,i%j);
	}
	
}
