import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;



public class A {
	
	
    public static void main(String[] args) {
    
    	FastScanner scan = new FastScanner();
    	
    	int T = 1;
    	T = scan.nextInt();
    	for(int tt=0; tt<T; tt++) {
    		int n = scan.nextInt();
    		int [] a = scan.readArray(n);
    		long sum = 0;
    		for(int i: a) {
    			sum += i;
    		}
    		
    		long rem = sum % n;
    		long ans = rem * (n - rem);
    		System.out.println(ans);
    	}
    }	
    
    /*
      5 5 5 6 6 6 6 6 6 6
     	
     	7, 7, 7
     	
     	
     */
    
    
   
    public static void sort(int [] a) {
    	ArrayList<Integer> b = new ArrayList<>();
    	for(int i: a) b.add(i);
    	Collections.sort(b);
    	for(int i=0; i<a.length; i++) a[i]= b.get(i);
    }
    
  
    static class FastScanner{
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer("");
    	String next() {
    		while(!st.hasMoreTokens())
    			try {
    				st = new StringTokenizer(br.readLine());
    			} catch (IOException e){
    				e.printStackTrace();
    			}
    		return st.nextToken();
    	}
    	
    	int nextInt() {
    		return Integer.parseInt(next());
    	}
    	
    	int [] readArray(int n) {
    		int [] a = new int[n];
    		for(int i=0; i<n ; i++) a[i] = nextInt();
    		return a;
    	}
    	
    	long nextLong() {
    		return Long.parseLong(next());
    	}
    	
    	
    }
    
   
}

