#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using psi = pair<string, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
using vpsi = vector<psi>;
using si = set<int>;
using msi = map<string, int>;
ll binexp(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
bool comp(pii &a, pii &b) { return a.second < b.second; }
int dist(int d, int a) {
  int x = a / d;
  return (d * (x + 1) - a);
}
void solve() {
  int n;
  cin >> n;
  vll a(n);
  int second{};
  for (int i = int(0); i <= int(n - 1); i++) {
    cin >> a[i];
  }
  second = accumulate((a).begin(), (a).end(), second);
  if (second % n == 0) {
    cout << "0\n";
  } else {
    ll x = second % n;
    x = x * (n - x);
    cout << x << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
