import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0){
            int n=s.nextInt();
            long sum=0,res=0;
            int[] a=new int[n];
            for(int i=0;i<n;i++){
                a[i]=s.nextInt();
                sum+=a[i];
            }
            if(sum%n==0)
                System.out.println(0);
            else{
                res=sum%n;
                res=res*(n-res);
            }
            System.out.println(res);
        }
    }
}