import java.util.*;

public class Problem_1543B {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        while(t-- > 0) {
            long totalCars = 0;
            long tracks = s.nextInt();
            for(int i = 0; i < tracks; i++) {
                totalCars += s.nextInt();
            }

            if(totalCars % tracks == 0) {
                System.out.println("0");
            } else {
                long rem = totalCars % tracks;
                long gap = tracks - rem;
                System.out.println(rem * gap);
            }
        }
    }
}
