#include <bits/stdc++.h>
using namespace std;
long long int csb(long long int x) {
  if (x <= 0) return 0;
  return (x % 2 == 0 ? 0 : 1) + csb(x / 2);
}
long long int tb(long long int n) {
  long long int count = 0;
  while (n) {
    count++;
    n >>= 1;
  }
  return count;
}
bool ip2(long long int n) {
  if (n == 0) return false;
  return (ceil(log2(n)) == floor(log2(n)));
}
long long int countGreater(long long int arr[], long long int n,
                           long long int k) {
  long long int l = 0;
  long long int r = n - 1;
  long long int leftGreater = n;
  while (l <= r) {
    long long int m = l + (r - l) / 2;
    if (arr[m] > k) {
      leftGreater = m;
      r = m - 1;
    } else
      l = m + 1;
  }
  return (n - leftGreater);
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int i, sum = 0, n, r, ans;
    cin >> n;
    vector<long long int> v(n);
    for (i = 0; i < n; i++) {
      cin >> v[i];
    }
    sum = accumulate(v.begin(), v.end(), 0);
    r = sum % n;
    ans = r * (n - r);
    cout << ans << endl;
  }
}
