
import java.util.*;
import java.io.*;
import javafx.util.Pair;
 
public class One {
 
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder out = new StringBuilder();
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
 
    static int I(String s) {
        return Integer.parseInt(s);
    }
 
    static long L(String s) {
        return Long.parseLong(s);
    }
 
    static double D(String s) {
        return Double.parseDouble(s);
    }
 
    static String[] SA() throws IOException {
        return bf.readLine().split(" ");
    }
 
    public static void main(String[] args) throws IOException {
        int t = I(bf.readLine());
        while (t-- > 0) {
               long n = I(bf.readLine());
            StringTokenizer in = new StringTokenizer(bf.readLine());
           long x=0l;
           for(int i=0;i<n;i++){
           x+=L(in.nextToken());
           }
          long mod=x%n;
           
             out.append(mod*(n-mod)+"\n");
        }
        System.out.println(out);
 
    }
 
}