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

	public class B{
			
		BufferedReader br;
		StringTokenizer st;
		
		public B(){ // constructor
			
			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 void sort(int[] arr){
			
	        int n = arr.length;
	        
	        Random rnd = new Random();
	        
	        for(int i = 0; i < n; ++i){
	        	
	            int tmp = arr[i];
	            int randomPos = i + rnd.nextInt(n - i);
	            arr[i] = arr[randomPos];
	            arr[randomPos] = tmp;
	        }
	        
	        Arrays.sort(arr);
	   }

		static int gcd(int a, int b) {
			if(b == 0) {
				return a;
			}
			return gcd(b, a % b);
		}
		
	public static void main(String[] args) throws Exception{
		
		B in = new B();
		PrintWriter out = new PrintWriter(System.out);
		
		// hard work will power dedication
		
		int test = in.nextInt();
		
		while(test-- > 0) {
			
			int n = in.nextInt();
			int arr[] = new int[n];
			
			long sum = 0;
			for(int i = 0; i < n; i++) {
				arr[i] = in.nextInt();
				sum += arr[i];
			}
			
			long num = sum % n;
			long b = n - num;
			
			out.println(num * b);
		}
		
		out.flush();
		out.close();
	}
}