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

 
public class Practice{
	
	
	 static class FastReader {
	        BufferedReader br;
	        StringTokenizer st;
	 
	        public FastReader()
	        {
	            br = new BufferedReader(
	                new InputStreamReader(System.in));
	        }
	 
	        String next()
	        {
	            while (st == null || !st.hasMoreElements()) {
	                try {
	                    st = new StringTokenizer(br.readLine());
	                }
	                catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            return st.nextToken();
	        }
	 
	        int nextInt() { return Integer.parseInt(next()); }
	 
	        long nextLong() { return Long.parseLong(next()); }
	 
	        double nextDouble()
	        {
	            return Double.parseDouble(next());
	        }
	 
	        String nextLine()
	        {
	            String str = "";
	            try {
	                str = br.readLine();
	            }
	            catch (IOException e) {
	                e.printStackTrace();
	            }
	            return str;
	        }
	    }
	 
	 
	 static long  gcd(long a, long b) {


	    if (b == 0) {
	        return a;}
	    else {
	        return gcd(b, a % b);}}
	 
	 //for prime no
	  static boolean isPrime(int n) {
	        return BigInteger.valueOf(n).isProbablePrime(1);
	    }
	 
	    static boolean isPrime(long n) {
	        return BigInteger.valueOf(n).isProbablePrime(1);
	    }

	 static boolean[] seiveOfEratoSthenes(int n) {
		 boolean isPrime[]=new boolean[n+1];
		 Arrays.fill(isPrime, true);
		 isPrime[0]=false;
		 isPrime[1]=false;
		 for(int i=2;i<=Math.sqrt(n);i++) {
			 for(int j=2*i;j<=n;j+=i) {
				 isPrime[j]=false;
			 }
		 }
		 
		 return isPrime;
		 
	 }
	 
	 static long modulo(long a,long b,int n) {
		 long res=1;
		 
		 while(b>0) {
			 if((b&1)==1) {
				 res=(res*(a%n))%n;
			 }
			 a=((a%n)*(a%n))%n;
			 b=b>>1;
			 
		 }
		 return res;
	 }
	 
	 static class Pair  implements Comparable<Pair>{
		 int a;
		 int b;
		 
		
	
		 
		 Pair(int tim,int index){  
			 this.b=index;
			  
			 this.a=tim;  
		
			 }  
		 public int compareTo(Pair st){  
			 if(a==st.a)  
			 return b-st.b;  
			 else if(a>st.a)  
			 return 1;  
			 else  
			 return -1;  
			 }  
	 } 
	 
	
	 static boolean isPalindrome(String s) {
	        int length = s.length();
	        for (int i = 0; i < length / 2; i++) {
	            if (s.charAt(i) != s.charAt(length - 1 - i)) {
	                return false;
	            }
	        }
	        return true;
	    }
	 
	 	static void reverseArray(int[] arr) {
	        for (int i = 0; i < arr.length / 2; i++) {
	            int temp = arr[i];
	            arr[i] = arr[arr.length - 1 - i];
	            arr[arr.length - 1 - i] = temp;
	        }
	    }
	 
	    static void reverseArray(String[] arr) {
	        for (int i = 0; i < arr.length / 2; i++) {
	            String temp = arr[i];
	            arr[i] = arr[arr.length - 1 - i];
	            arr[arr.length - 1 - i] = temp;
	        }
	    }
	 
	    static void reverseArray(char[] arr) {
	        for (int i = 0; i < arr.length / 2; i++) {
	            char temp = arr[i];
	            arr[i] = arr[arr.length - 1 - i];
	            arr[arr.length - 1 - i] = temp;
	        }
	    }

	    
	    static long LCM( long a, long b)
	    {
	    	long g = gcd(a, b);
	    	a /= g;
	    	return a*b;
	    }
	    public static void main(String[] args)
	    {
	        FastReader sc = new FastReader();
//	        System.out.println();	
	    
	        
	        
	        
	        int t=sc.nextInt();
	        while(t-->0) {
	        	int n=sc.nextInt();
	        	long arr[]=new long[n];
	        	
	        	long sum=0;
	        	for(int i=0;i<n;i++) {
	        		arr[i]=sc.nextLong();
	        		sum+=arr[i];
	        	}
		    	
	        	
	        	long avg=sum/n;
	        	
	        	long num=(sum-(avg*n));
	        	
	        	long ans=num*(n-num);
	        	
	        	System.out.println(ans);
		    	
	        	
	        }
	     
	    }
	    
	 
	    
	    static long number(int n, int digit) {
	    	
	    	long var=0;
	    	
	    	for(int i=0;i<digit;i++) {
	    		var+=n;
	    		var*=10;
	    	}
	    	var/=10;
	    	return var;
	    	
	    } 
	        
	 	
	    static int binary(int start,int end,int arr[],int v ) {
	 		int Q=0;
	 	    if (end <= start) 
	 	        { Q=(v >= arr[start])?  start : (start-1); }
	 	    else{
	 	  
	 	    int mid = (start + end)/2; 
	 	  
	 	    if(v == arr[mid]) 
	 	       return mid; 
	 	  
	 	    if(v >arr[mid]) {
	 	        Q= binary( mid+1, end,arr, v); }
	 	    else if(v <arr[mid]){
	 	    Q= binary( start, mid-1,arr, v); }}
	 	    return Q;
	 	}
	        	 
 




       
       
   
    
     static boolean isCycle(boolean vis[], ArrayList<ArrayList<Integer>> adj,int v,int parent)
    {
        vis[v]=true;
        
        
        for(int i:adj.get(v)){
            if(vis[i]&&parent!=i){
                return true;
            }
            else if(vis[i]){
                continue;
            }
            boolean a= isCycle(vis,adj,i,v);
            if(a)return true;
            
            
        }
        return false;
        
    }
}    
	     
	 
	
	
 class pair{
    int a;
    int b;
    pair(int p,int q){
        a=p;
        b=q;
    }
    public boolean equals(Object o) {
        if (o instanceof pair) {
            pair p = (pair)o;
            return p.a == a && p.b == b;
        }
        return false;
    }
    public int hashCode() {
        return new Integer(a).hashCode() * 31 + new Integer(b).hashCode();
    }
}   

     
	  