import java.util.*;
public class CustomizingTheTrack{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tt = sc.nextInt();
        while(tt-->0){
            int n = sc.nextInt();
            long sum = 0;
            for(int i=1;i<=n;i++){
                int a = sc.nextInt();
                sum += a;
            }
            long rem = sum%n;
            long ans = rem*(n-rem);
            System.out.println(ans);
        }
    }
}