We need to parse the problem correctly. It seems like a classic egg dropping problem: given N floors (heights from 1 to N) and E eggs, find the minimum number of trials (drops) needed in the worst case to determine the highest safe floor (strength H). But the problem statement says: "You cannot make it in time unless you finish the experiment with the minimum number of times. Therefore, you, the brother of Nishikun, decided to write a program to find the maximum number of experiments required when the optimum method was taken so that the number of drops was reduced in order to know the strength of the egg."
This is exactly the classic: find the minimum number of trials needed in the worst case, given N floors and E eggs. So we need to output f(N,E) where f is the minimal worst-case number of drops needed.
Constraints: N up to 1e18, E up to 50, T up to 1000. So we need to compute the function efficiently for large N. Classic egg dropping DP: let dp[m][e] = maximum number of floors that can be determined with m drops and e eggs. Then we need the smallest m such that dp[m][e] >= N. For E up to 50, we can precompute dp for m until it exceeds 1e18. Since N can be huge, we cannot DP over N. Instead we can compute the function f(N,E) as the minimal m such that sum_{i=1}^{E} C(m, i) >= N? Actually the classic result: With m trials and e eggs, the maximum number of floors you can cover is sum_{i=0}^{e} C(m, i). But careful: The classical egg dropping with optimal strategy yields that the maximum number of floors you can distinguish with m drops and e eggs is f(m,e) = sum_{i=0}^{e} C(m, i). This holds when you can drop from any floor and you know that floor 0 is safe and floor N+1 is breaking. Actually the standard result: With m moves and k eggs, the maximum number of floors that can be tested is f(m,k) = sum_{i=0}^{k} C(m,i). And the minimal drops needed for N floors and k eggs is the smallest m such that f(m,k) >= N.
But is this correct for all N? Let's recall: The recurrence: dp[drops][eggs] = 1 + dp[drops-1][eggs-1] + dp[drops-1][eggs]. With base dp[0][]=0, dp[][0]=0. This DP yields exactly sum_{i=0}^{eggs} C(drops, i) for drops >= eggs? Actually it's known that the DP yields f(d, e) = sum_{i=0}^{e} C(d, i). Yes that's a known combinatorial identity. And for d < e, the sum of binomials up to e may exceed but the DP would be limited because you can't use more eggs than drops? Actually the DP formula works for all d, e. The closed form is indeed f(d, e) = sum_{i=0}^{e} C(d, i). But note that when e > d, sum_{i=0}^{e} C(d, i) = 2^d (since C(d,i)=0 for i>d). But the DP would also yield 2^d? Let's check: With d drops and e eggs, if you have more eggs than drops, you essentially have enough eggs to do binary search? Actually the DP recurrence: f(d,e) = f(d-1, e-1) + f(d-1, e) + 1. If e > d, we might get to f(d,d) eventually? But indeed the known closed form holds: f(d,e) = sum_{i=0}^{min(d,e)} C(d,i). Because if e > d, eggs beyond d are not useful because you cannot try more than d breaks (since each break consumes an egg, and you can only break at most d times). So it's sum_{i=0}^{d} C(d,i) = 2^d. So we can take f(d, e) = sum_{i=0}^{min(d,e)} C(d, i). But given that e <= 50, and d may be large (since N up to 1e18, d may be up to something like log2 N if e>=log2 N? But e max 50, and N up to 1e18 ~ 2^60, so d might be up to 60 if e >= 60? But e max 50, so if e=50, then we need d such that sum_{i=0}^{50} C(d,i) >= 1e18. For d ~ 60, sum_{i=0}^{50} C(60,i) is huge. So d won't be too large, maybe up to around 60? But careful: if e=1, then f(d,1)=d (since sum C(d,0)+C(d,1)=1+d). Actually the formula: f(d,1) = 1 + d? Wait DP: with 1 egg, you can only test sequentially, so with d drops you can cover d floors? Let's see: if you have 1 egg, you can drop from floor 1,2,..., until it breaks. In worst case, you might need to go to floor N and it doesn't break? Actually you need to know the exact H such that it breaks at H+1. With 1 egg, the worst-case number of drops needed for N floors is N (you drop from 1,2,... until it breaks; if it never breaks after N, you know H=N, but you needed N drops? Actually to determine H, you drop from floor 1: if breaks, H=0? But we know H is between 0 and N inclusive. Standard egg drop: you have to find the critical floor. With 1 egg, you must test each floor sequentially from 1 upward, worst-case N drops (if it never breaks at N, you need N drops to confirm it's safe up to N). However, sometimes the answer considered is N because you drop until break? But let's confirm: The definition: strength H is such that does not crack when dropped from height H and cracks when dropped from height H+1. We know it doesn't crack at height 0 and cracks at height N+1. So we need to determine H. With 1 egg, the strategy: start from floor 1. If it cracks, H=0. If not, go to floor 2, etc. If it doesn't crack at floor N, then H=N. So worst-case you need N drops (if it never cracks, you drop at all N floors). So minimal worst-case drops = N. That matches f(N,1)=N. And formula for f(d,1) should be d? Actually if we have d drops and 1 egg, the maximum floors you can handle is d? Because you can test at most d distinct floors sequentially, and you can infer the critical floor from the pattern. Yes f(d,1)=d. Our sum formula: sum_{i=0}^{1} C(d,i)=C(d,0)+C(d,1)=1+d. That's 1+d, not d. So there is an off-by-one. Let's re-check the classic result.
In the classic egg dropping problem, we have a building with N floors and we want to find the highest floor from which an egg does not break. The typical DP: Let f(d,e) be the maximum number of floors we can test with d drops and e eggs. The recurrence: f(d,e) = f(d-1, e-1) + f(d-1, e) + 1. Base: f(0,) = 0, f(,0)=0. This recurrence arises from: If we drop at floor x, if egg breaks, we need to test floors below x with d-1 drops and e-1 eggs (so f(d-1, e-1) floors). If it doesn't break, we test floors above x with d-1 drops and e eggs (so f(d-1, e) floors). So total floors covered if we start at floor x is: 1 (current floor) + f(d-1,e-1) + f(d-1,e). The maximum x we can choose is such that the sum equals? Actually to maximize total floors we can test, we set x = f(d-1, e-1) + 1. Then we can cover floors from 1 to f(d-1,e-1)+1+f(d-1,e). So f(d,e) = f(d-1,e-1)+1+f(d-1,e). That's the recurrence.
Now, we want the minimal d such that f(d,e) >= N. For e=1, f(d,1) = f(d-1,0)+1+f(d-1,1). Since f(d-1,0)=0, we get f(d,1) = 1 + f(d-1,1). With f(0,1)=0, so f(d,1)=d. Indeed f(d,1)=d. So the formula sum_{i=0}^{e} C(d,i) must yield d for e=1? But sum_{i=0}^{1} C(d,i)=1+d, not d. So the known closed form is actually f(d,e) = sum_{i=0}^{e} C(d, i). But then for e=1 it gives 1+d. Something's off. Let's verify known results. I recall that with 2 eggs and d drops, the maximum floors is d(d+1)/2, which is sum_{i=0}^{2} C(d,i) = C(d,0)+C(d,1)+C(d,2) = 1 + d + d(d-1)/2 = (d^2+d+2)/2? Wait compute: 1 + d + d(d-1)/2 = (2 + 2d + d(d-1))/2 = (2+2d+d^2 - d)/2 = (d^2 + d + 2)/2. That's not equal to d(d+1)/2 which is (d^2 + d)/2. So it's off by 1. Actually the classic: With 2 eggs and d drops, the maximum number of floors you can test is d(d+1)/2. And sum_{i=0}^{2} C(d,i) = 1 + d + d(d-1)/2 = (d^2 + d + 2)/2. That's exactly (d(d+1)/2) + 1? Let's check: d(d+1)/2 = (d^2+d)/2. So difference = 1. So it's off by 1. So maybe the correct closed form is f(d,e) = sum_{i=1}^{e} C(d,i) (without the 0 term) or something? Let's test: For e=1, sum_{i=1}^{1} C(d,i)=d -> correct. For e=2, sum_{i=1}^{2} C(d,i)=d + d(d-1)/2 = d(d+1)/2 -> correct. For e=3, known maximum floors with 3 eggs and d drops is something like? I think the known formula is sum_{i=0}^{e} C(d,i) but there is nuance: the standard DP yields f(d,e) = sum_{i=0}^{e} \binom{d}{i}? Let's derive manually for small values.
Let's compute f(d, e) via DP.
Base: f(0,e)=0 for any e, f(d,0)=0.
For d>=1:
f(d,1) = f(d-1,0)+1+f(d-1,1) = 0+1+f(d-1,1) => f(1,1)=1, f(2,1)=2, etc => f(d,1)=d.
Now for e=2: f(d,2) = f(d-1,1)+1+f(d-1,2). Let's compute: f(0,2)=0, f(1,2)=? f(1,2)=f(0,1)+1+f(0,2)=0+1+0=1. f(2,2)=f(1,1)+1+f(1,2)=1+1+1=3. f(3,2)=f(2,1)+1+f(2,2)=2+1+3=6. f(4,2)=f(3,1)+1+f(3,2)=3+1+6=10. So f(d,2) = d(d+1)/2? For d=3, d(d+1)/2=6, yes. d=4 -> 10, yes. So f(d,2)=d(d+1)/2.
Now sum_{i=1}^{2} C(d,i) = d + d(d-1)/2 = d(d+1)/2 correct. sum_{i=0}^{2} C(d,i) = 1 + d + d(d-1)/2 = (d^2+d+2)/2 = f(d,2)+1? Actually f(3,2)=6, sum_{i=0}^{2} C(3,i)=1+3+3=7 -> +1. f(4,2)=10, sum=1+4+6=11 -> +1.
For e=3: Let's compute DP. f(d,3) = f(d-1,2)+1+f(d-1,3). f(0,3)=0. f(1,3)=f(0,2)+1+f(0,3)=0+1+0=1. f(2,3)=f(1,2)+1+f(1,3)=1+1+1=3. f(3,3)=f(2,2)+1+f(2,3)=3+1+3=7. f(4,3)=f(3,2)+1+f(3,3)=6+1+7=14. f(5,3)=f(4,2)+1+f(4,3)=10+1+14=25. f(6,3)=f(5,2)+1+f(5,3) => f(5,2)=15? Actually compute f(5,2)=56/2=15. So f(6,3)=15+1+25=41. f(7,3)=f(6,2)+1+f(6,3); f(6,2)=67/2=21; so f(7,3)=21+1+41=63. f(8,3)=f(7,2)+1+f(7,3)=28+1+63=92. So f(d,3) values: d:1->1,2->3,3->7,4->14,5->25,6->41,7->63,8->92,...
Now sum_{i=1}^{3} C(d,i) = C(d,1)+C(d,2)+C(d,3). For d=3: 3+3+1=7 -> matches f(3,3). For d=4: 4+6+4=14 -> matches. For d=5: 5+10+10=25 -> matches. For d=2: 2+1+0=3? Actually C(2,3)=0, so sum=2+1=3 matches f(2,3). For d=1: sum=1 matches. So f(d,3) = sum_{i=1}^{3} C(d,i) works. So the correct closed form is f(d,e) = sum_{i=1}^{e} C(d,i). But wait, for e=2, sum_{i=1}^{2} C(d,i) gave d(d+1)/2 correct. For e=1, sum_{i=1}^{1} C(d,i)=d correct. So the formula is f(d,e) = sum_{i=1}^{min(d,e)} C(d,i). But note that if e > d, then sum_{i=1}^{d} C(d,i) = 2^d - 1. However the DP yields? For e=3 and d=2, we got 3, which equals 2^2 -1 =3. So that matches. For e=3 and d=1, we got 1 = 2^1 -1 =1. So indeed f(d,e) = 2^d - 1 if e >= d? Actually if e >= d, we cannot break more than d times, so the max floors we can test is 2^d -1? Let's check: With 2 eggs and d drops, if d=3, f(3,2)=6 not 7. So when e < d, it's sum of first e binomial coefficients. When e >= d, it's 2^d -1 (since sum_{i=1}^{d} C(d,i) = 2^d -1). So formula: f(d,e) = sum_{i=1}^{min(d,e)} C(d,i). But careful: The classic result is that with m moves and k eggs, the maximum number of floors you can test is Σ_{i=1}^{k} C(m,i). This is standard. However I've also seen the version Σ_{i=0}^{k} C(m,i) that appears sometimes, but that might be for the variant where you also count the 0th floor? Let's double-check: In the egg dropping problem, the floor numbers are from 1 to N. The DP recurrence: f(d,k) = f(d-1,k-1) + f(d-1,k) + 1. And it's known that f(d,k) = Σ_{i=1}^{k} C(d,i). Proof by induction: f(d,k) = Σ_{i=1}^{k} C(d,i) = Σ_{i=1}^{k} [C(d-1,i)+C(d-1,i-1)] = Σ_{i=1}^{k} C(d-1,i) + Σ_{i=1}^{k} C(d-1,i-1) = [Σ_{i=1}^{k} C(d-1,i)] + [Σ_{j=0}^{k-1} C(d-1,j)] = (f(d-1,k) - C(d-1,0)?) Actually careful: Σ_{i=1}^{k} C(d-1,i) = f(d-1,k) - C(d-1,0) = f(d-1,k) - 1? But our target is f(d,k) = f(d-1,k-1)+f(d-1,k)+1. So we need to check if Σ_{i=1}^{k} C(d,i) = Σ_{i=1}^{k-1} C(d-1,i) + Σ_{i=1}^{k} C(d-1,i) + 1? That would be f(d-1,k-1)+f(d-1,k)+1. But Σ_{i=1}^{k} C(d,i) = Σ_{i=1}^{k} (C(d-1,i) + C(d-1,i-1)) = Σ_{i=1}^{k} C(d-1,i) + Σ_{i=1}^{k} C(d-1,i-1) = Σ_{i=1}^{k} C(d-1,i) + Σ_{j=0}^{k-1} C(d-1,j). Now Σ_{j=0}^{k-1} C(d-1,j) = 1 + Σ_{j=1}^{k-1} C(d-1,j) = 1 + f(d-1,k-1). And Σ_{i=1}^{k} C(d-1,i) = f(d-1,k). So sum = f(d-1,k) + 1 + f(d-1,k-1). That's exactly the recurrence. So indeed Σ_{i=1}^{k} C(d,i) satisfies the recurrence with base f(0,k)=0 (since sum over i=1..k C(0,i)=0) and f(d,0)=0 (sum over i=1..0 is 0). So the closed form is f(d,k)=Σ_{i=1}^{k} C(d,i). So that is correct.
Thus the minimal number of drops needed for N floors with e eggs is the smallest d such that Σ_{i=1}^{min(d,e)} C(d,i) >= N.
But wait, the classic problem often has answer as the smallest d such that Σ_{i=1}^{e} C(d,i) >= N, but note that if d < e, then Σ_{i=1}^{e} C(d,i) = 2^d -1, because we sum up to d only. So we can simply compute Σ_{i=1}^{e} C(d,i) but treat C(d,i)=0 for i>d. So the condition is Σ_{i=1}^{e} C(d,i) >= N, where C(d,i)=0 if i>d.
Given constraints, we need to compute for each test case (N,E) the smallest d such that f(d,E) >= N.
E up to 50, N up to 1e18. d won't be too large; we need to find d that satisfies this. Since f(d,E) grows roughly like d^E / E! for d large compared to E, but for E up to 50, d may be up to about 10^18? Actually for E=50, f(d,50) for d around 1000 is huge because binomial coefficients become massive. We need to estimate max d needed. Let's compute approximate growth. For fixed e, f(d,e) ~ d^e / e! for d >> e. To get f(d,e) >= 1e18, we need d^e / e! >= 1e18. For e=50, d^50 / 50! >= 1e18. 50! is about 3.04e64? Actually 50! ~ 3.04e64 (log10(50!) ~ sum log10(i) i=1..50; roughly 64.5? Let's compute: log10(50!) ~ 64.483. So we need d^50 >= 1e82.5 => d >= 10^(82.5/50) = 10^1.65 = about 44.7. So d about 45? That seems surprisingly small. But careful: the approximation d^e / e! works for d >> e but here d might be less than e? Actually e=50, d would need to be at least 50 to have all binomial coefficients non-zero? But if d < e, f(d,e) = 2^d - 1 which grows as 2^d. For d=45, 2^45 ≈ 3.5e13, far less than 1e18. So we need d larger than 50 to use the polynomial growth. For d > 50, the sum is dominated by the highest terms. Let's check for d around 60: d^50 / 50! is huge because d^50 = 60^50 = (6e1)^50 = 6^50 * 10^50. 6^50 ~ 6^10=60 million? Let's compute log10: log10(60)=1.778, times 50 = 88.9, minus log10(50!) ~64.5 => about 24.4, so f(d,50) ~ 10^24.4, which is >1e24, well above 1e18. So d might be about 50-60. But we need to compute exactly.
But careful: For e=1, f(d,1)=d, so d must be at least N, which can be up to 1e18, but e=1 case is trivial: answer = N. However N up to 1e18, that's huge. But we need to output the answer, which could be as large as 1e18. That's fine; we can handle big integers (Python can). So we need to handle e=1 separately: answer = N.
For e >= 2, d will be much smaller, but we still need to compute d efficiently.
We need to compute the minimal d such that S(d, e) = sum_{i=1}^{e} C(d, i) >= N. Since e <= 50, we can iterate over d from 1 upward until condition holds. But for e=2, the minimal d is about sqrt(2N). For N=1e18, sqrt(2e18) ~ 4.47e9, which is large; iterating from 1 to 4e9 is impossible. So we need a better method: binary search on d.
But we also need to compute S(d, e) for large d (up to maybe 1e18 for e=1, but for e>=2 we will binary search d in range [1, something]. For e=2, d max might be around 2e9? Actually solve d(d+1)/2 >= 1e18 => d^2 ~ 2e18 => d~1.414e9. That's up to 1.5e9, which is large for binary search but computing C(d,2) = d(d-1)/2 may overflow 128-bit? Python can handle big integers fine, but we need to be careful with performance: binary search over 1e9 range will involve about 30 steps, each step computing S(d, e) which is sum of up to e binomial coefficients. For e=2, it's trivial: d + d(d-1)/2. For e=3, it's d + d(d-1)/2 + d(d-1)(d-2)/6. That's a cubic, can be computed directly. For larger e, the polynomial degree e, which is at most 50. For each d, we need to compute sum of binomial coefficients, which can be done by computing each term carefully with big integers, but d may be up to something like maybe a few million? Actually for e=50, d might be around 60-70? Let's compute more precisely: We need smallest d such that sum_{i=1}^{50} C(d,i) >= 1e18. For d=50, sum = 2^50 - 1 ~ 1.1259e15. Too low. d=55: 2^55 -1? Actually if d<e, sum = 2^d -1. So d=55 gives ~3.6e16. d=60 gives 2^60-1 1.15e18. That's close: 2^60 = 1.1529215e18, so sum = 1.1529e18 -1 which is >= 1e18. But careful: For d=59, 2^59 = 5.7646e17, less than 1e18. So for e=50, if d<50? Actually e=50, d=59 is > e? No, e=50, d=59 > e, so the sum is not 2^d-1, but sum_{i=1}^{50} C(59,i). For d>e, the sum is less than 2^d - 1, but still close to 2^d for d not much larger than e? Actually sum_{i=1}^{e} C(d,i) for e=50, d=59, the binomial coefficients for i up to 50 include many terms. But the tail from i>50 is missing. However 2^d = sum_{i=0}^{d} C(d,i). So sum_{i=1}^{50} C(59,i) = 2^59 - sum_{i=51}^{59} C(59,i). Since binomial coefficients symmetric, sum_{i=51}^{59} C(59,i) = sum_{i=0}^{8} C(59,i). So it's 2^59 minus a relatively small amount. For d=59, missing sum of first 9 terms (including i=0). So sum = 2^59 - (sum_{i=0}^{8} C(59,i)). That missing part is small relative to 2^59. So 2^59 ~ 5.76e17, sum_{i=0}^{8} C(59,i) is sum of first 9 binomial coefficients. The largest among those is C(59,8). C(59,8) maybe ? Let's estimate: C(59,8) is about 59^8/8! ~ (5.9e1)^8 = (5.9e1)^8 = (5.9^8)*10^8. 5.9^8 ≈ (5.9^4)^2, 5.9^2=34.81, ^4 ≈ 34.81^2=1211, ^8≈1.47e6, times 1e8 => 1.47e14. So sum_{i=0}^{8} maybe around 1e15? That would subtract 1e15 from 5.76e17 -> 5.75e17, still less than 1e18. So d=59 likely still < 1e18. Actually we need sum >= 1e18. So d needs to be at least 60. For d=60, 2^60 ~ 1.15e18, sum_{i=1}^{50} C(60,i) = 2^60 - sum_{i=51}^{60} C(60,i) = 2^60 - sum_{i=0}^{9} C(60,i). Sum_{i=0}^{9} is small relative to 2^60? C(60,9) ? 60^9/9! approximate: 60^9 = (6e1)^9 = 6^9 *10^9 = 10077696 * 1e9 = 1.00777e16? Actually 6^9 = 10077696, times 10^9 gives 1.00777e16. 9! = 362880, so C(60,9) ~ 2.78e10? Wait that's off: 60^9 is huge but binomial coefficient C(60,9) = 60 choose 9 is product 6059...*52 / 9! . Rough estimate: 60 choose 9 ~ 1.16e9? Actually let's compute properly: 60 choose 9 = 60!/(9!51!) . There are known values: 60 choose 9 = 60C9 = ? I recall 60C5=5,461,512, 60C650 million, 60C7386 million, 60C82.5e9, 60C91.4e10? Let's approximate: 60 choose 8 is about 2.5e9, 60 choose 9 is about 1.6e10? Actually I can compute quickly: binomial coefficients for n=60:
C(60,0)=1
C(60,1)=60
C(60,2)=1770
C(60,3)=34220
C(60,4)=487635
C(60,5)=5461512
C(60,6)=50063860? Actually 60C6 = 50063860? Let's compute: 60C6 = (605856 (53)/(8) etc. But we don't need exact. Sum_{i=0}^{9} is dominated by C(60,9) which is on the order of 1e10, much smaller than 2^60 (~1.15e18). So sum_{i=1}^{50} C(60,i) is about 1.15e18 minus about 1e10, which is still >1e18? Actually 2^60 = 1.1529215e18. Subtract sum_{i=0}^{9} which is maybe around 1e10, so remains 1.1529215e18 - 1e10 = 1.1529205e18, still >1e18. So d=60 works. So for e=50, answer d around 60. For e=49 similar. So for e>=2, d is at most maybe a few thousand? Actually for e=2, d up to ~1.5e9, which is large, but we can binary search. For e=3, need to solve cubic: d^3/6 >= N => d ~ (6N)^{1/3}. For N=1e18, (6e18)^{1/3} = (6)^{1/3}1e6 = 1.8e6. So d up to about 2 million for e=3. That's manageable for binary search with O(log N) steps, each computing sum of binomials.
But for e=2, d up to about 1.5e9, binary search with ~30-40 iterations, each computing O(e) terms, which is fine.
For e=1, we treat separately.
But careful: The problem constraints say E_i up to 50, N_i up to 1e18. So we need to handle cases where E is large (>= log2 N) but N is also huge; e.g., N=1e18, E=50, answer is about 60; that's small. For E=50, binary search range could be low (maybe up to 1000). But for E=2, binary search up to ~2e9 is okay.
But we must be careful about overflow in intermediate computations when computing binomial coefficients for large d and moderate e (e up to 50). Directly computing C(d, i) for d up to 2e9 and i up to 50 could result in huge numbers, but we can use Python's big integers; they can handle numbers up to ~1e18? Actually C(2e9, 2) = 2e9*(2e9-1)/2 ≈ 2e18, fits within 64-bit but Python big integers are fine. C(2e9, 3) ~ (2e9)^3 /6 ≈ 1.33e27, which is huge (27 digits) but Python can handle. However we need to compare to N (1e18). So we can cap the sum at N+1 to avoid giant numbers. Since we only need to check if sum >= N, we can compute each term with early termination if sum exceeds N. That will keep numbers manageable.
But caution: for e=2, sum = d + d(d-1)/2. d up to 1.5e9, d(d-1)/2 is about 1e18, which fits in 64-bit? 1.5e9 squared is 2.25e18, divided by 2 is 1.125e18, fits within 64-bit unsigned? 2^63-1 is about 9.22e18, so safe. For e=3, d^3/6 with d~2e6 gives (8e18)/6 1.33e18, also fits in 64-bit. For e=4, d^4/24, to reach 1e18, d (24e18)^{1/4} = (2.4e19)^{0.25} ~ (2.4e19)^{0.25} = (2.4)^{0.25} * (1e19)^{0.25} = ~1.25 * 1e4.75? Actually 1e19^{0.25}=10^{19/4}=10^{4.75}=5.62e4, times 1.25 ≈ 7e4. So d up to 70k. Then C(d,4) ~ d^4/24 ~ 1e18. So numbers within 64-bit. For e=5, d ~ (5! * 1e18)^{1/5} = (120e18)^{1/5} = (1.2e20)^{0.2} = 10^{20.0792/5? Actually log10(120e18)= log10(120)+18 = 2.079+18=20.079, /5 = 4.0158, so d ~ 1.04e4 = 10,400. So numbers within 64-bit? d^5 ~ 10^{20}, divided by 120 gives 10^{18}. So fits. As e increases, d decreases, and the largest term C(d, e) will be around d^e/e! which is about N. So magnitude of C(d,e) is about N, and other terms are smaller. So all intermediate numbers are at most about N * something? Actually for d up to 2e9, C(d,2) can be up to ~2e18. For higher e, d is smaller, so binomials are about N. So we can safely compute using Python's integers without overflow concerns, but we should still cap at N to avoid huge intermediate values for e=2? Actually for e=2, C(d,2) could be up to ~ (1.5e9)^2/2 ~ 1.125e18, safe. For e=1, we already treat separately.
But what about e=2 when N is 1e18, d ~ 1.5e9, sum = d + d(d-1)/2. That's about 1.125e18 + 1.5e9, which is ~1.1265e18, still within 64-bit. However if N is slightly smaller, d might be smaller. So using Python's integers is fine. But we need to be careful about potential overflow in languages with fixed width, but we are using Python.
Thus algorithm: For each test case (N, E):
If E == 1: answer = N.
Else:
- Binary search d from low = 1 to high = something, such that f(d,E) >= N.
Need to set upper bound appropriately. For E>=2, we can compute an upper bound using e.g., while f(high,E) < N, double high. For E=2, f(d,2)=d(d+1)/2. Solve d^2/2 ~ N => d ~ sqrt(2N). For N=1e18, sqrt(2e18) ~ 1.414e9. So high can start at 1, then double until f(high,E) >= N. Starting high = 1, doubling takes about log2(1.4e9) ~ 31 steps, fine. But we also need to compute f(high,E) each time, which is O(E). That's okay.
Binary search: while low < high:
mid = (low+high)//2
if f(mid, E) >= N: high = mid
else: low = mid+1
Answer = low.
Now we need to implement f(d, E) efficiently for large d up to maybe 2e9, E up to 50. Compute sum_{i=1}^{E} C(d, i) with early exit if sum >= N. Compute binomial coefficient incrementally or directly using product and division. Since E is small, we can compute each C(d,i) using iterative multiplication and division, being careful about integer division and overflow. But computing C(d,i) directly as product_{j=1..i} (d - j + 1) / j can be done using Python's integers. For d up to 2e9 and i up to 50, numerator product may become huge (i=50 product of 50 numbers around 2e9 => (2e9)^50 = 2^50 * 1e450 huge). But we will cap at N to avoid huge numbers. So better to compute term = term * (d - i + 1) // i iteratively and stop if term > N or sum > N. Because Python big integers can handle huge numbers, but performance might degrade if we compute huge numbers (like thousands of digits) unnecessarily. For d=2e9, i=50, C(2e9,50) is astronomically huge (~ (2e9)^50 / 50! = 2^50 * 1e450 / ~3e64 = 1e386? Actually 1e450/1e64 = 1e386, huge number with 386 digits). That would be extremely slow if we compute it fully. However we can cap: we only need to know if sum >= N. So we can compute each term but if term exceeds N, we can set sum = N (or a large number) and break early, or just keep adding but using Python's big ints might still be okay? But 386-digit numbers are not that big for Python, they are fine. But consider d=1e9, i=50: C(1e9,50) is about (1e9)^50/50! = 1e450/3e64=3.3e385, a 386-digit number. Python can handle that but performing many such operations (for each binary search step, we compute up to 50 terms) might be a bit heavy but probably still fine given T up to 1000 and binary search about 30 steps, total about 1.5 million binomial calculations, each involving multiplication of up to 50 numbers? That's 75 million multiplications, each on big integers of up to maybe 400 digits. Could be borderline but maybe okay in Python with optimized code? But we can do better: Instead of computing full binomial coefficients, we can compute f(d,E) using recurrence or using DP but that would also involve big numbers. There is known approach: For given E, f(d,E) is a polynomial in d of degree E, but we can evaluate using iterative method with early truncation.
Alternative: Since E is small (<=50), we can precompute a table of binomial coefficients for each d? Not possible because d varies.
Better: Use the property that we can compute f(d,E) by iterating i from 1 to E and computing term = term * (d - i + 1) // i, and if term > N, we can cap term to N+1 to keep numbers small, but careful: we need accurate sum to compare to N. Actually if term > N, then adding term to sum will likely make sum exceed N, but we still need to know if sum >= N. However we could set term = N+1 (or some sentinel) and add, but we must ensure we don't overflow. But Python big ints are fine. However we can also break early: if sum + term >= N, we can return True.
But to avoid huge term, we can compute term using floating point approximation? But we need exact comparison. However we can compute term incrementally and if term becomes larger than N (or if term times something > N), we can cap? But we need the sum of all terms, but if any single term > N, sum > N, so we can return True immediately. However careful: It's possible that the sum of many smaller terms could exceed N even if each term individually <= N? But if any term > N, sum >= that term > N, so we can return True. So we can early exit: if term > N, we can set sum = N+1 and break. But we need to compute term exactly to know if it > N. But term might be huge, but we can compute it step by step and if at any point during the iterative multiplication the intermediate product exceeds N (times some factor?), we can early exit the term computation itself. For example, while computing C(d,i) iteratively, we maintain a running product. If at any step the product exceeds N * denominator? But denominator not applied yet. Since we need to compare the final rational value to N, we can compute term gradually, dividing by denominator as we multiply to keep number small? There's technique to compute binomial coefficient with early exit: compute term = 1; for j in 1..i: term = term * (d - j + 1) // j. But this keeps term integer and exact, but intermediate values may become large before division? Actually if we do term = term * (d - j + 1) // j, due to integer division, order matters. To keep term small, we can do floating point? But integer division after multiplication will reduce size because we divide by j each step. However the intermediate term after multiplication might be large before division. For d=2e9, i=50, after a few multiplications, term might become huge. But we can also incorporate an early break: if term already > N and all remaining multiplications will only increase it (since we multiply by positive numbers and divide by positive numbers, but division could reduce). Actually after each step, term is an integer. If term exceeds N, we can stop computing further terms because the final binomial coefficient will be at least term (since subsequent multiplications and divisions may increase or decrease? Let's think: We compute term sequentially: term = 1; for j=1..i: term = term * (d - j + 1) // j. Because division by j happens after multiplication, term stays integer. After each step, term may increase or decrease? Typically, for j <= d/2, term increases. But after j > d/2, term decreases. But we are computing for i up to E, where E <= 50, and d may be up to 2e9, so i << d, so term monotonically increases as we add factors? For fixed d, as i increases, C(d,i) increases until i = floor(d/2). Since i <= 50 and d huge, i is far from d/2, so C(d,i) is increasing in i. So the term we compute sequentially will increase with i. But note that our method computes each C(d,i) separately. For each i, we compute term_i = C(d,i). We can compute them sequentially using recurrence: C(d,i) = C(d,i-1) * (d - i + 1) / i. So we can compute all terms iteratively: start with term=1 (C(d,0)=1). Then for i from 1 to E: term = term * (d - i + 1) // i. Then sum += term. This avoids recomputing from scratch each time. That's efficient and each step involves multiplication by (d - i + 1) then division by i. The numbers involved: for i up to 50, term grows to C(d,i) which for d large can be huge. But we can incorporate early exit: if sum >= N, break; also if term > N, we can set sum = N (or sum += term but term is huge). However, term could become astronomically huge (like 10^386) but Python can handle it. But if we do this for each d in binary search, we might be creating huge big ints. However number of such huge big ints is limited: for d=2e9, term for i=50 is huge, but computing it exactly may be somewhat expensive. But maybe still acceptable given only 30 * 1000 = 30k evaluations? Actually worst-case: T=1000, each binary search about 40 steps, each step evaluating sum up to E=50 terms. So 100050 = 2,000,000 term updates. Each term update involves multiplication and division of big ints of size up to maybe 400 digits. That's okay in Python? 2 million operations on 400-digit ints may be a few seconds but likely within time limit if optimized? But we also have to consider the worst-case d for E=2, where d is up to 1.5e9, but for E=2 we only compute two terms: d + d(d-1)/2. That's simple. For E=3, d up to 2e6, computing C(d,3) involves numbers around (2e6)^3 ~ 8e18, which is 19 digits, trivial. For E=4, d up to 1e4, term small. So the only case where term becomes astronomically large is when d is large and E is also large. But if E is large, d is small (since to cover 1e18 with many eggs, d is around 60). For d=60 and E=50, C(60,50) is C(60,10) actually because of symmetry but we only sum up to 50, but for d=60, i up to 50, the largest term is C(60,30) which is about 1.18e17, not huge. So numbers are at most around 1e17-1e18. So indeed for all combinations, the numbers we compute will be at most around 1e18 or maybe a bit larger? Let's check worst-case: For E=2, d ~ 1.5e9, C(d,2) ~ 1.125e18, fits in 64-bit. For E=3, d70k, C(d,4) ~ (70k)^4/24 ≈ (2.4e19)/24 = 1e18, okay. For E=5, d~1e4, C(d,5) ~ 1e20/120 = 8e17, okay. For E=10, what d is needed? Solve sum_{i=1}^{10} C(d,10) ≈ C(d,10) (dominant) ~ d^10/10! >= 1e18 => d^10 >= 10! * 1e18 = 3,628,800 * 1e18 = 3.63e24 => d >= (3.63e24)^(0.1) = 10^(24.56/10)=10^2.456 ≈ 286. So d around 300. Then C(d,10) with d=300 is C(300,10) which is about 300^10/10! which is huge? Actually 300^10 is 3e2^10 = 3^10 * 10^20 = 59049 * 1e20 = 5.9e24, divided by 3.6e6 gives 1.6e18, so again around 1e18. So numbers remain around 1e18. For E=20, d even smaller (maybe around 60). So all binomial coefficients we compute will be at most around 10^18, maybe slightly more but not astronomically large. Therefore we can safely compute exact binomials using Python integers without overflow issues.
The only potential large d is when E=1, but we treat that separately. For E=2, d up to 1.5e9, C(d,2) ~ 1e18, fine. For E=3, d up to 2e6, C(d,3) ~ 8e18, still < 2^63 (9.22e18) maybe slightly above? Actually 2e6^3 = 8e18, /6 = 1.33e18, so still below 2^63. For E=4, (7e4)^4 = (7e4)^2=4.9e9, squared again = 2.4e19, /24 = 1e18, still okay. So all within 64-bit range? Actually 2e6^3 = 8e18, /6 = 1.33e18, okay. But for E=2, d may be 2e9, (2e9)^2/2 = 2e18, which is > 2^63-1? 2^63-1 ≈ 9.22e18, so 2e18 is still less than 9e18. So within 64-bit. So we can even use Python int but it's fine.
Thus we can simply compute sum_{i=1}^{E} C(d,i) exactly using integer arithmetic without worrying about huge numbers.
Implementation details:
Define function max_floors(d, e, N): compute sum_{i=1}^{min(d,e)} C(d,i). Stop early if sum >= N. Use iterative computation:
if d == 0: return 0
term = 1 # C(d,0)
total = 0
for i in range(1, min(d,e)+1):
term = term * (d - i + 1) // i
total += term
if total >= N:
return total
return total
But we need to ensure we don't exceed i > d: when d < e, we sum up to d. So min(d,e).
Edge cases: d may be less than e, but we sum only up to d. That gives total = 2^d - 1. But careful: When d is small, we sum C(d,i) for i=1..d. That equals 2^d -1. That's correct.
Now binary search bounds:
Lower bound low = 1 (since at least one drop needed if N>0? But N >=1). However if N=0? According to problem, N >=1. So low=1.
Upper bound: we need to ensure f(high, E) >= N. For E=1, we treat separately. For E>=2, we can find high by doubling until condition met. Since d might be as high as N for E=1, but we handle that separately. For E>=2, the maximum d needed is when E=2, N=1e18 => d ~ 1.5e9. So we can start high = 1, while f(high, E) < N: high *= 2. That will take about 31 iterations for 1.5e9. That's fine.
But we also need to consider case where E is large relative to N, e.g., N=1, E=50. Then minimal d is 1 because f(1,E)=1 (since C(1,1)=1). So binary search works.
We must be careful with f(d,E) for d=0? Not needed.
Implementation steps:
- Read T.
- For each test case:
- Parse N, E as integers (N up to 10^18, E up to 50).
- If E == 1: answer = N
- Else:
low = 1
high = 1
compute an upper bound
while max_floors(high, E, N) < N:
high *= 2
binary search
while low < high:
mid = (low + high) // 2
if max_floors(mid, E, N) >= N:
high = mid
else:
low = mid + 1
answer = low
- Print answer.
But we need to define max_floors(d, e, N) to compute sum of binomials up to e, but we pass N for early exit. However the function should also be used in the doubling phase; we need to compute exact f(d,E) to compare with N. But we can implement a function that returns True if f(d,E) >= N, without computing full sum if early exit. That may be more efficient. But we can just compute sum and compare.
Potential issues: For E=2, d may be up to 1.5e9, but in max_floors we iterate i up to min(d,e). For d=1.5e9, e=2, we loop i=1..2, which is trivial. For e=50 and d=60, loop 1..50, fine.
But what about d very large like 1e9 and e=50? Then min(d,e)=50, we loop 1..50, each computing term. However term for i up to 50 will become huge. For d=1e9, C(1e9, 50) is astronomically huge (approx (1e9)^50/50! = 1e450/3e64 ≈ 3e385). That's a 386-digit number. Python can handle it, but is it necessary? But would we ever need to compute f(d,50) for d=1e9? That would be for E=50, but the binary search will never go that high because f(d,50) for d=60 already exceeds 1e18. So the binary search range for E=50 is up to maybe 100. So we won't encounter large d for large E. However what about E=30 and N=1e18? Then d will be about? Solve C(d,30) ~ d^30/30