#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int t, n, suma;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    suma = 0;
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      suma += x;
    }
    if (n == 1) {
      cout << "0\n";
      continue;
    }
    suma = suma % n;
    if (suma == 0) {
      cout << "0\n";
      continue;
    }
    cout << suma * (n - suma) << endl;
  }
  return 0;
}
