#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t > 0) {
    --t;
    long long n;
    long long sum = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      long long x;
      cin >> x;
      sum += x;
    }
    if (n == 1)
      cout << sum << endl;
    else {
      sum = sum % n;
      cout << sum * (n - sum) << endl;
    }
  }
  return 0;
}
