#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  uint64_t n;
  uint64_t s;
  cin >> t;
  while (t--) {
    s = 0;
    cin >> n;
    for (int i = 0, temp = 0; i < n; i++) {
      cin >> temp;
      s += temp;
    }
    if (n == 1) {
      cout << s << '\n';
    } else {
      cout << (s % n) * (n - (s % n)) << '\n';
    }
  }
  return 0;
}
