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

public class B {
	public static void main(String[] args) {
		// try {
		// 	System.setIn(new FileInputStream("input.txt"));
		// 	System.setOut(new PrintStream(new FileOutputStream("output.txt")));
		// } catch (Exception e) {
		// 	System.err.println("Error" + e);
		// }
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		
		while(t-- > 0) {
			int n = sc.nextInt();
			long sum = 0, x;
			for(int i=0; i<n; i++) {
				x = sc.nextInt();
				sum += x;
			}
			
			long extra = sum%n;
			long one = n - extra;
			long two = extra;
			System.out.println(one*two);
		}
	}
}