import java.io.*;
import java.util.*;
public class problemB {

	public static void main(String[] args)throws IOException {
		// TODO Auto-generated method stub
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    int t = Integer.parseInt(br.readLine());
	    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	    for(int i=0; i<t; i++) {
	    	int n = Integer.parseInt(br.readLine());
	    	StringTokenizer st = new StringTokenizer(br.readLine());
	    	int[] arr = new int[n];
	    	long sum = 0;
	    	for(int j=0; j<n; j++) {
	    		arr[j] = Integer.parseInt(st.nextToken());
	    		sum += arr[j];
	    	}
	    	long extra = sum - (sum/(long)n)*((long)n);
	    	out.write((extra*((long)n-extra)) + "\n");
	    }
	    out.flush();
	    out.close();
	}
}
