#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int a[n];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  long long x = sum % n;
  long long ans = x * (sum - x);
  cout << ans << "\n";
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
