Problem: Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations.

Can you help Heidi estimate each village's population?

Input

Same as the easy version.

Output

Output one line per village, in the same order as provided in the input, containing your (integer) population estimate.

Your answer is considered correct if it is an integer that falls into the interval <image>, where P is the real population of the village, used to create the distribution (either Poisson or uniform) from which the marmots drew their answers.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int v[250];
double getUniformProb(int P) {
  double ans = 250 * log(1.0 / (2 * P + 1));
  return ans;
}
double fact[2001];
double getPoissonProb(int P) {
  double ans = 0;
  for (int i = 0; i < 250; ++i) ans += v[i] * log(P) - P - fact[v[i]];
  return ans;
}
void test() {
  int maximum = 0;
  for (int i = 0; i < 250; ++i) {
    cin >> v[i];
    maximum = max(maximum, v[i]);
  }
  int bestP = 0;
  double best = -1E18;
  string bestType;
  for (int i = 1; i <= 1000; ++i) {
    if (2 * i >= maximum) {
      double val = getUniformProb(i);
      if (val > best) {
        bestP = i;
        best = val;
        bestType = "uniform";
      }
    }
    double val = getPoissonProb(i);
    if (val > best) {
      bestP = i;
      best = val;
      bestType = "poisson";
    }
  }
  cout << bestP << '\n';
}
int main() {
  for (int i = 2; i <= 2000; ++i) fact[i] = fact[i - 1] + log(i);
  int T = 0;
  cin >> T;
  while (T--) test();
  return 0;
}