//generated by ChatGPT #include using namespace std; using int64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector count(N + 1, 0); int64 firstValue = 0; for (int i = 0; i < N; ++i) { int64 a; cin >> a; if (i == 0) { firstValue = a; } // N >= 2 の場合、答えは N 以下なので、 // N より大きい値の正確な個数は必要ない。 if (a <= N) { ++count[static_cast(a)]; } } if (N == 1) { cout << max(firstValue, 1) << '\n'; continue; } // 値 k を作れるか判定する。 auto feasible = [&](int k) -> bool { // suffixDeficit = sum_{y=x+1}^{k-1} d_y int64 suffixDeficit = 0; // 初期要求 0, 1, ..., k-1 の個数 int64 requiredLeaves = k; for (int x = k - 1; x >= 2; --x) { // 最終操作用の1個と、 // より大きい値の合成から発生した要求 int64 requiredX = 1 + suffixDeficit; // 初期値 x で足りない分は合成する必要がある int64 deficit = max(0, requiredX - count[x]); // x の要求を 0,...,x-1 に展開すると x-1 個増える requiredLeaves += deficit * (x - 1LL); if (requiredLeaves > N) { return false; } suffixDeficit += deficit; } return true; }; // 2 は必ず作れる。 int low = 2; // N + 1 は作れない。 int high = N + 1; while (high - low > 1) { int mid = low + (high - low) / 2; if (feasible(mid)) { low = mid; } else { high = mid; } } cout << low << '\n'; } return 0; }