#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power(long long x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    int ans = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
      ans += a[i];
    }
    int x = ans / n;
    int dif = ans - (x * n);
    for (int i = 0; i < n; i++) {
      if (dif != 0) {
        a[i] = x + 1;
        dif--;
      } else
        a[i] = x;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        sum += llabs(a[i] - a[j]);
      }
    }
    cout << sum << endl;
  }
}
