#include <bits/stdc++.h>
using namespace std;
long long a[200005] = {0};
long long ksm(long long base, long long p) {
  long long r = 1;
  while (p > 0) {
    if (p & 1) {
      r = r * base;
    }
    p >>= 1;
    base = (base * base);
  }
  return r;
}
int main() {
  long long T;
  scanf("%lld", &T);
  while (T--) {
    long long n;
    long long sum = 0;
    scanf("%lld", &n);
    for (long long i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      sum += a[i];
    }
    if (sum % n == 0)
      printf("0\n");
    else {
      long long e = 0x3f3f3f3f;
      long long q = sum / n;
      long long w = sum - q * n;
      for (long long i = 1; i < n; i++) {
        if (w % i == 0) {
          e = min(e, w / i * (n - i) * i);
        }
      }
      printf("%lld\n", e);
    }
  }
  return 0;
}
