#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long total = 0;
    for (long long i = 0; i < n; i += 1) {
      long long input;
      cin >> input;
      total += input;
    }
    if (n == 1) {
      cout << total << "\n";
    } else {
      long long each = total / n;
      long long leftOver = total - (each * n);
      vector<long long> track(n, each);
      for (long long i = 0; i < leftOver; i += 1) {
        track[i] += 1;
      }
      long long ans = 0;
      for (long long i = 0; i < n - 1; i += 1) {
        for (long long j = i + 1; j < n; j += 1) {
          if (i == j) continue;
          ans += abs(track[i] - track[j]);
        }
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
