#include <bits/stdc++.h>
using namespace std;
int mod(int a) {
  if (a < 0) return -a;
  return a;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, x = 0, sum = 0;
    cin >> n;
    vector<int> a;
    for (int i = 0; i < n; i++) {
      int el;
      cin >> el;
      a.push_back(el);
      sum += el;
    }
    int d, r;
    d = sum / n;
    r = sum % n;
    for (int i = 0; i < n; i++) a[i] = d;
    for (int i = 0; i < r; i++) a[i]++;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        x += mod(a[i] - a[j]);
      }
    }
    cout << x << endl;
  }
}
