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

public class B {
	static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(System.out);
	public static void main(String[] args) throws IOException {
		int t = Integer.parseInt(reader.readLine());
		for (int ii=0; ii<t; ii++) {
			int n = Integer.parseInt(reader.readLine());
			StringTokenizer s = new StringTokenizer(reader.readLine());
			long[] arr = new long[n];
			long sum = 0;

			for (int i=0; i<n; i++) {
				long x = Long.parseLong(s.nextToken());
				arr[i] = x;
				sum += x;
			}


			long goal = (long) sum/n; 
			long leftover = sum%n; 

			//7 of (goal+1), 3 of (goal)

			long goal2 = leftover;
			long goal1 = n - leftover;

			long ans = goal1*goal2;

			pw.println(ans);

		}
		pw.close();
	}
}