#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const long long inf = 1e18;
const long double pi = acos(-1);
long long power(long long x, long long y) {
  long long res = 1;
  x %= md;
  while (y) {
    if (y & 1) res = (res * x) % md;
    x *= x;
    if (x >= md) x %= md;
    y >>= 1;
  }
  return res;
}
long long inv(long long a) { return power(a, md - 2); }
long long gcd(long long x, long long y) {
  for (long long z; y; z = x % y, x = y, y = z)
    ;
  return x;
}
const int N = 1e5 + 10;
double eps = 1e-6;
void precompute() {}
void Try() {
  long long n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long sum = accumulate(a, a + n, 0);
  if (sum % n == 0) {
    cout << 0 << "\n";
    return;
  }
  long long rem = sum % n;
  cout << rem * (n - rem) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int tc = 1;
  precompute();
  cin >> tc;
  while (tc--) Try();
  return 0;
}
