import java.util.*;
import java.io.*;
public class P3 {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int t = scn.nextInt();
        while(t-->0){
            int n = scn.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for(int i=0;i<n;i++){
                a[i] = scn.nextInt();
                sum += a[i];
            }
            int r = sum%n;
            int left = n-r;
            System.out.println(r*left);
        }

    }
}