import java.io.PrintWriter;
import java.util.Scanner;

public class CustomisingTrack {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int t = sc.nextInt();
        while (t != 0) {
            t--;
            int n = sc.nextInt();
            long elements[] = new long[n];
            long sum = 0;
            for (int i = 0; i < n; i++) {
                elements[i] = sc.nextLong();
                sum += elements[i];
            }
            long rem = sum % n;
            long op = rem * (n - rem);
            out.println(op);
        }
        out.flush();
        sc.close();
    }
}