 import java.util.Arrays;
import java.math.*;
import java.lang.Long;
import java.util.Scanner;
public class ForTesting {

	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		
		int test=sc.nextInt();
	
		while(test>0)
		{
			int n=sc.nextInt();
			 int arr[]=new int[n];
			long sum=0; 
	          for(int i=0; i<n; i++)
	          { arr[i]=sc.nextInt(); sum+=arr[i]; }
	          if(sum==n) System.out.println(0);
	          else if(sum<n) System.out.println((sum%n)*((int)(Math.ceil((double)n/(double)sum))));
	          else if(sum>n && sum%n==0) {
	        	  
	        	  System.out.println(0);}
	          else 
	          {
	        	 long re=sum/n+1;
	        	  long res=sum%n;
	        	  long res1=sum-(re*res);
	        	long d=res1/(re-1);
	        	 long resi=d*res;
	        	  System.out.println(resi);
	          }
	          test--;
		}
		
  }
}