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