#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    long long sum = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      sum += a[i];
    }
    int t = sum % n;
    printf("%d\n", (n - t) * t);
  }
  return 0;
}
