/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0)
		{
			long n=sc.nextInt();
		//	long ar[]=new long[n];
			long sum=0;
			for(int i=0;i<n;i++)
			{sum=sum+sc.nextLong();
				
			}
			double p=sum*1.0/n*1.0;
			if(Math.floor(p)==p)
			{
				System.out.println(0);
				continue;
			}
			long a=(long)Math.floor(p);
			long b=(long)Math.ceil(p);
		long x=sum-a*n;
		long f=(n-x)*x;
		System.out.println(f);
			
			
		}
		
	}
}