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

public class B {
	public static void main(String[] args) {
		FastReader in = new FastReader();
		
		int T = in.nextInt();
		while(T-- > 0) {
			int n = in.nextInt();
			long[] a = in.readLong(n);

			long sum = 0;
			for(long i : a) sum += i;

			long x = sum % n;
			long total = Long.parseLong(String.valueOf(n));
			
			System.out.println(x*(total - x));
		}
	}

	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()); }
		String nextLine() {
			String str = "";
			try { str = br.readLine(); }
			catch(IOException e) { e.printStackTrace(); }
			return str;
		}
		int[] readInt(int size) {
			int[] arr = new int[size];
			for(int i = 0; i < size; i++)
				arr[i] = Integer.parseInt(next());
			return arr;
		}
		long[] readLong(int size) {
			long[] arr = new long[size];
			for(int i = 0; i < size; i++)
				arr[i] = Long.parseLong(next());
			return arr;
		}
		int[][] read2dArray(int rows, int cols) {
			int[][] arr = new int[rows][cols];
			for(int i = 0; i < rows; i++) {
				for(int j = 0; j < cols; j++)
					arr[i][j] = Integer.parseInt(next());
			}
			return arr;
		}
	}
}