import java.util.*;
import java.text.*;

public class B_Customising_the_Track {
  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();
      }

      if (sum % n == 0) {
        System.out.println("0");
      } else {
        long rem = sum % n;

        // val = sum / n
        // rem number will have value (val+1)
        // n-rem will have value (val)
        long ans = rem * (n - rem);
        System.out.println(ans);

      }

    }

    sc.close();
  }
}