import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;

public class Oliver_And_The_Game {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		// int t = 1;
		while (t-- > 0) {
			int n = Integer.parseInt(br.readLine());
			String[] parts = br.readLine().split(" ");
			long[] arr = new long[n];
			long sum = 0;
			for(int i = 0; i < n; i++) {
				arr[i] = Long.parseLong(parts[i]);
				sum += arr[i];
			}
			
			long val = sum % n;
			System.out.println(val * (n - val));
		}

	}

}
