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

public class b {
    public static void main(String[] args) throws IOException{
        FastScanner in = new FastScanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        for ( ; t > 0; t--){
            int n = in.nextInt();
            long sum = 0;
            for (int i = 0; i < n; i++){
                sum += in.nextLong();
            }
            long amt = sum / n;
            long rem = sum % n;
            System.out.println((n - rem) * rem);
        }
        out.close();
    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(InputStream i) {
            br = new BufferedReader(new InputStreamReader(i));
            st = new StringTokenizer("");
        }
                
        public String next() throws IOException {
            if(st.hasMoreTokens())
                return st.nextToken();
            else
                st = new StringTokenizer(br.readLine());
            return next();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        //#
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }

}
