//1543B
import java.util.*;
public class CustomisingTheTrack {
	public static void main(String[]args) {
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while(t-->0) {
			int n = s.nextInt();
			long sum = 0;
			for(int i = 0; i<n; i++) {
				sum+=s.nextInt();
			}
			//make it as close to average as possible;
			long ex = sum%n;
			System.out.println((long)ex*(n-ex));
		}
		s.close();
	}
}
