#include <bits/stdc++.h>
using namespace std;
int t, n;
long long a;
long long jumb = 0;
int rC2(int r) { return ((r) * (r - 1)) / 2; }
int main() {
  scanf("%d", &t);
  for (int q = 0; q < t; q++) {
    jumb = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%lld", &a);
      jumb += a;
    }
    long long hasilin0 = rC2(jumb % n) + rC2(n - jumb % n);
    long long ans = rC2(n) - hasilin0;
    if (n == 1) ans = a;
    cout << ans << endl;
  }
}
