#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    vector<long long int> v(n);
    long long int sum = 0;
    bool falg = true;
    for (auto& x : v) {
      cin >> x;
      sum += x;
    }
    int ee = sum / n;
    int le = sum % n;
    if (le == 0) {
      cout << 0 << '\n';
      falg = false;
    } else {
      cout << le * (n - le) << "\n";
    }
  }
  return 0;
}
