#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long int> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    long long int sum = 0;
    for (int i = 0; i < n; i++) sum += v[i];
    int val = sum % n;
    int res = (n - val) * val;
    if (n > 1)
      cout << res << endl;
    else {
      cout << sum << endl;
    }
  }
  return 0;
}
