#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
void add(int& a, int b) {
  a += b;
  if (a >= mod) a -= mod;
  if (a < 0) a += mod;
}
int mult(int a, int b) { return a * (long long)b % mod; }
int bp(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}
void solve() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    long long first;
    cin >> first;
    sum += first;
  }
  int cnt = sum % n;
  cout << cnt * (n - cnt) << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
}
