#include <bits/stdc++.h>
using namespace std;
const long long template_array_size = 1e6 + 16614;
void solve(int tc = 0) {
  vector<long long> v;
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    v.push_back(a[i]);
    sum += a[i];
  }
  long long m = (sum / n);
  long long temp = 0;
  sort(v.begin(), v.end());
  long long i = n - 1;
  int g = 0;
  for (int i = 0; i < n; ++i) {
    if (v[i] != 1 & v[i] != 0) g = 1;
  }
  if (g == 1) {
    while (v[i] - m >= 0) {
      temp += v[i] - m;
      v[i] = m;
      --i;
    }
    int flag = 0;
    while (i < 0 & flag != 1) {
      temp = temp - (m - v[i]);
      if (temp < 0) {
        temp = temp + (m - v[i]);
        flag = 1;
      } else
        v[i] = m;
      --i;
    }
    temp = 0;
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; i < n; ++i) {
        temp += abs(v[i] - v[j]);
      };
    cout << temp - 1 << endl;
  } else
    cout << n << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  for (int t = 0; t < tc; t++) solve(t);
  return 0;
}
