import java.io.*; 
import java.util.Scanner;
import java.util.*;
import java.lang.*;
import java.io.IOException;
public class Codechef
{ 
    public static void main(String[] args) throws IOException
    {
        try 
        {
            System.setIn(new FileInputStream("input.txt"));
            System.setOut(new PrintStream(new FileOutputStream("output.txt")));
        } 
        catch (Exception e) 
        {
            System.err.println("Error");
        }
      
         Scanner sc= new Scanner(System.in);
         int t = sc.nextInt();
         while(t--!=0)
         {
            int n = sc.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for(int i=0;i<n;i++)
            {
                a[i] = sc.nextInt();
                sum = sum + a[i];
            }

            if(sum%n==0)
            {
                System.out.println(0);
            }
            else
            {
                int temp = (int)Math.ceil(sum/n);
                int res  = sum - temp*n;
                System.out.println((n-res)*res);
            }
            
         }

    }

    

    
}
