import java.util.*;

public class Main
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int t = sc.nextInt();
	    while(t-->0){
	        int a = sc.nextInt();
	        long[] arr = new long[a];
	        long sum=0;
	        for(int i=0;i<a;i++){
	            arr[i] = sc.nextLong();
	            sum+=arr[i];
	        }
	        long temp = sum%a;
	        long ans = temp*(a-temp);
		    System.out.println(ans);
	    }
	}
}
