#include <bits/stdc++.h>
using namespace std;
long long int solve(vector<long long int>& vec, long long int n) {
  long long int sum = accumulate(vec.begin(), vec.end(), 0);
  int eq = sum / n;
  int rem = sum % n;
  int ze = n - rem;
  if (rem == 0) return 0;
  return ze * rem * 1ll;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> vec(n);
    for (int i = 0; i < n; i++) cin >> vec[i];
    cout << solve(vec, n);
  }
}
