import java.io.*;
import java.util.*;
public class Main {

    static long mod = 1000000007;
    public static long power(long a, long b){
        if(b == 0)
            return 1;
        long A = power(a,b/2);
        long B = ((A*A)%(mod)*((long)Math.pow(a,b%2)))%mod;
        return B;
    }

    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        int times = Reader.nextInt();
        for(int i=0;i<times;i++) {
            long n = Reader.nextInt();
            long sum  = 0;
            for(int j=0;j<n;j++){
                sum += Reader.nextInt();
                sum = sum % n;
            }
            sum = sum % n;
            if(sum == 0)
                System.out.println(0);
            else
                System.out.println( (n - sum) * sum);
        }
    }

}

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static Float nextFloat() throws IOException {
        return Float.parseFloat( next() );
    }
}
class Point {
    int a;
    int b;
    int val;
    Point(int a, int b, int val){
        this.a = a;
        this.b = b;
        this.val = val;
    }
}