import java.util.*;
import java.io.*;

public class Test {
  // Fast IO class
  static class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
      boolean env = System.getProperty("ONLINE_JUDGE") != null;
      // env=true;
      if (!env) {
        try {
          br = new BufferedReader(new FileReader("src/input.txt"));
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
      } else br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }

  static FastReader sc = new FastReader();
  static PrintWriter out = new PrintWriter(System.out);

  static int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }

  public static void main(String[] args) {
    int t = sc.nextInt();
    while (t-- > 0) {
      int n = sc.nextInt();
      long[] a = new long[n];
      long sum = 0;
      for (int i = 0; i < n; i++) {
        a[i] = sc.nextLong();
        sum += a[i];
      }
      long right = sum % n;
      out.println(right * (n - right));
    }
    out.flush();
    out.close();
  }
}
