
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
    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 {

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
                FileOutputStream(java.io.FileDescriptor.out), StandardCharsets.US_ASCII), 512);
        FastReader sc = new FastReader();

        int z = sc.nextInt();
        for(int v=0;v<z;v++) {
            int n = sc.nextInt();
            long sum=0;
            long [] arr=new long[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextLong();
                sum+=arr[i];
            }

            long rem= sum%n;
            long rem1=n-rem;
            long ans= rem*rem1;
            out.write(ans+"\n");
            out.flush();

        }

    }
}
