import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class Main {
    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; 
        } 
    }
    public static void main(String [] args) throws Exception {
        OutputStream outputStream =System.out;
        PrintWriter out =new PrintWriter(outputStream);
        FastReader sc =new FastReader();
        int t = sc.nextInt();
        
        while(t-->0)
        {
            long n = sc.nextLong();
            long sum = 0;
            for(int i=0;i<n;i++)
            {
                long x = sc.nextLong();
                sum+=x;
            }
            sum = sum%n;
            long ans = (n-sum)*sum;
            out.println(ans);
        }
       
        out.close();
    }
    
     
}

     