import java.util.*;

public class Practice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            long sum = 0;
            for(int i=0;i<n;i++) sum += sc.nextInt();
            long mod = sum%n;
            System.out.println(mod*(n-mod));
        }
    }
}

