import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CustomizingTheTrack {
	public static void main (String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int t = Integer.parseInt(st.nextToken());
		
		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(br.readLine());
			int n = Integer.parseInt(st.nextToken());
			
			st = new StringTokenizer(br.readLine());
			long[] arr = new long[n];
			long sum = 0;
			
			for (int j = 0; j < n; j++) {
				sum += Long.parseLong(st.nextToken());
			}
			
			long v1 = sum/n;
			long r = sum - n*v1;
			
			
			for (int j = 0; j < n; j++) {
				arr[j] = v1;
			}
			
			for (int j = 0; j < r; j++) {
				arr[j]++;
			}
			
			// System.out.println(Arrays.toString(arr));
			
			sum = 0;
			
			for (int j = 1; j < n; j++) {
				sum += arr[0] - arr[j];
			}
			
			long inSum = sum;
			
			System.out.println(inSum*r);
			
			// System.out.println(sum);
		}
		
		br.close();
	}
}
