//package goldman1;
import java.util.*;
public class CustomisingTheTrack {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int p=0;p<t;p++){
            int n=in.nextInt();
            int arr[]=new int[n];
            long sum=0;
            for(int i=0;i<n;i++){
                arr[i]=in.nextInt();
                sum+=arr[i];
            }
            long rem=sum%n;
            long ans=rem*(n-rem);
            System.out.println(ans);
        }
    }
}
