import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class CustomizingTheTrack {

	public static void main(String[] args) throws IOException {
		BufferedReader read = new BufferedReader(new InputStreamReader(System.in));//new FileReader("cowdance.in")
		 PrintWriter out = new PrintWriter(System.out);//new FileWriter("cowdance.out")
		 StringTokenizer st = new StringTokenizer(read.readLine());
		 int t = Integer.parseInt(st.nextToken());
		 
		 for (int ie = 0; ie < t; ie++) {
		 st = new StringTokenizer(read.readLine());
		 int n = Integer.parseInt(st.nextToken());
		 st = new StringTokenizer(read.readLine());
		 long sum = 0;
		 for (int i = 0; i < n; i++) {
			sum += Long.parseLong(st.nextToken()); 
		 }
		 long m = sum % n;
		 out.println(m * (n-m));
		 
		 
		
		
		 }
		 
		 
		 out.close();
	}

}
