
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Problem {
    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{
        long mod = (long)(1e9) + 7;
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
                FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);
        FastReader in = new FastReader();

        int t = in.nextInt();
        while(t-- != 0){
            int n = in.nextInt();
//            String s = in.next();
            long[] arr = new long[n];
            long res = 0;
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextLong();
                res += arr[i];
            }
//            System.out.println(res);
            if(res % n == 0){
                out.write("0\n");
            }else{
                long m = res % n;
                long r = n - m;
                long r1 = r*m;
                out.write(r1+ "\n");
            }
        }
        out.flush();
    }
}
