#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int car[n];
    long long int s = 0;
    for (int i = 0; i < n; i++) {
      cin >> car[i];
      s += car[i];
    }
    int r = s % n;
    cout << (n - r) * r << "\n";
  }
  return 0;
}
