import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int test = scn.nextInt();
        while (test-- > 0) {
            long sum = 0;
            long n = scn.nextLong();
            for (int i = 0; i < n; i++) {
                sum += scn.nextLong();
            }
            long l = sum % n;
            long r = n - l;
            System.out.println(1l*l*r);

        }
    }
}
