#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 10;
int g[N];
int main() {
  int n;
  cin >> n;
  while (n--) {
    int t;
    cin >> t;
    int num = 0, s;
    for (int i = 0; i < t; i++) {
      cin >> s;
      num += s;
    }
    if (num <= t) {
      cout << num * (t - num) << endl;
    } else if (num > num / t * t) {
      int tmp = num - num / t * t;
      cout << min(tmp * (t - 1), tmp * (t - tmp)) << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}
