#include <bits/stdc++.h>
using namespace std;
int nc2(int n) {
  if (n < 2) {
    return 0;
  }
  return (n) * (n - 1) / 2;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t-- > 0) {
    int n;
    cin >> n;
    int arr[n];
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      sum += arr[i];
    }
    int x = sum % n;
    cout << x * (n - x) << endl;
  }
  return 0;
}
