/*==========================================================================
*   	AUTHOR:    RonWonWon
*   	CREATED:   09.07.2021 22:53:26        
/*==========================================================================*/
import java.io.*;
import java.util.*;

public class B {
	
	public static void main(String[] args) throws IOException {
		/*
    in.ini();
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
    */
    long startTime = System.nanoTime();
    int t = in.nextInt(), T = 1;  
    while(t-->0) { 
      int n = in.nextInt();
      int a[] = in.readArray(n);
      long sum = 0;      
      for(int i : a) sum += i;
      long rem = sum%n;
      long ans = rem*(n-rem);
      out.println(ans);
   		//out.println("Case #"+T+": "+ans); T++;
    }
    /*
    startTime = System.nanoTime() - startTime;
    System.err.println("Time: "+(startTime/1000000));
    */
    out.flush();
	}
	
	static FastScanner in = new FastScanner();
  static PrintWriter out = new PrintWriter(System.out);
	static int oo = Integer.MAX_VALUE;
	static long ooo = Long.MAX_VALUE;
	
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		
		void ini() throws FileNotFoundException {
			br = new BufferedReader(new FileReader("input.txt"));
		}
		
		String next() {
			while(!st.hasMoreTokens())
				try { st = new StringTokenizer(br.readLine()); }
				catch(IOException e) {}
			return st.nextToken();
		}
		
		String nextLine(){
			try{ return br.readLine(); } 
			catch(IOException e) { } return "";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		long[] readArrayL(int n) {
			long a[] = new long[n];
			for(int i=0;i<n;i++) a[i] = nextLong();
			return a;
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
		double[] readArrayD(int n) {
			double a[] = new double[n];
			for(int i=0;i<n;i++) a[i] = nextDouble();
			return a;
		}
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n), temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a); 	
	}
	
	static void ruffleSortL(long[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n);
			long temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a); 	
	}
}
