Roy and Alfi reside in two different cities, Roy in city A and Alfi in city B. Roy wishes to meet her in city B.
There are two trains available from city A to city B. Roy is on his way to station A (Railway station of city A). It will take T_0 time (in minutes) for Roy to reach station A. The two trains departs in T_1 and T_2 minutes respectively. Average velocities (in km/hr) of trains are V_1 and V_2 respectively. We know the distance D (in km) between city A and city B. Roy wants to know the minimum time (in minutes) to reach city B, if he chooses train optimally.
If its not possible to reach city B, print "-1" instead (without quotes).
Note: If the minimum time is not integral, round the value to the least integer greater than minimum time.
Input:
First line will contain integer T, number of test cases.
Second line will contain integers ** T_0, T_1, T_2, V_1, V_2, D ** (their meanings are mentioned in problem statement)
Output:
Print the integral result, minimum time (in minutes) to reach city B in a new line.
Constraints:
1 ≤ T ≤ 10000
1 ≤ T_0,T_1,T_1 ≤ 1000
1 ≤ V_1,V_2 ≤ 500
1 ≤ D ≤ 5000
SAMPLE INPUT
1
5 5 8 100 90 320
SAMPLE OUTPUT
197
Explanation
Roy reaches station A in 5 minutes, First train departs in 5 minutes and second train departs in 8 minutes, he will be able to catch both of them. But he has to make an optimal choice, which train reaches city B in minimum time. For first train \frac{320}{100} =; 3.2; hrs; = 192 minutes. Total time for first train, 5+192 = 197 minutes
For second train \frac{320}{90} = 3.555556; hrs; = 213.33336 minutes. Least integer greater than 213.33336; is ;214. Total time for second train 214+8 = 222 minutes. So optimal choice is to take first train, and hence the minimum time is 197 minutes.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the problem. Input: T test cases. For each test case, one line with six integers: T0, T1, T2, V1, V2, D. T0: time (minutes) to reach station A (the departure station). Trains depart in T1 and T2 minutes from now? Actually "There are two trains available from city A to city B. Roy is on his way to station A. It will take T_0 time (in minutes) for Roy to reach station A. The two trains departs in T_1 and T_2 minutes respectively." Typically, it means the trains depart T1 and T2 minutes from the current moment? Or from now? The phrasing: "The two trains departs in T_1 and T_2 minutes respectively." Since Roy is en route, T0 is time to reach the station. So at current time t=0, Roy is somewhere; he will arrive at station at time T0. Train 1 departs at time T1 from now? Train 2 at T2 from now? So if T0 <= T1, he can catch train 1. Similarly if T0 <= T2, he can catch train 2. If T0 > departure time, he misses it.
But note: what if T0 is exactly equal? He can catch the train as he arrives at the same time; likely allowed. So condition: if T0 <= T_i, then possible to catch train i. The total time to reach city B if he catches train i = T_i (wait until departure?) Actually careful: If he arrives at time T0, and the train departs at T_i, then he must wait until departure. So total time from now to arrival at city B = T_i (since departure time from now) + travel time. But is it T_i or do we consider waiting? If he arrives at T0, then waits (T_i - T0) if T_i > T0, but total time from now to departure is T_i (since train leaves at T_i). So yes, total time = T_i + travel_time_i. However, if T_i < T0, he cannot catch it.
But also note: Could he catch a train that departs earlier than his arrival? No.
So for each train i, if T0 <= T_i, compute total_time_i = T_i + (D / V_i) in hours? But D in km, V_i in km/hr. So travel time in minutes: (D / V_i) * 60. Because V_i km/hr gives hours; multiply by 60 to get minutes.
So total_time_i_min = T_i + (D / V_i) * 60.
If T0 > T_i, then that train is not catchable.
If both are not catchable, output -1.
Otherwise, among catchable trains, we take the minimum total_time_i_min. But note: The total_time_i_min might be fractional because D/V_i * 60 may not be integer. The problem says: "If the minimum time is not integral, round the value to the least integer greater than minimum time." So we need to compute the minimum among the total times (real numbers) then round up to the next integer. But careful: The rounding instruction: "If the minimum time is not integral, round the value to the least integer greater than minimum time." This likely applies to the overall answer: the minimum time among options, if not integer, we round up. However, we need to interpret: They might mean we compute the minimal total time (real number) and then round it up to the nearest integer. But also note that for each train, the total time might be non-integer; we might need to round up each individually? The problem statement: "Roy wants to know the minimum time (in minutes) to reach city B, if he chooses train optimally. If its not possible to reach city B, print "-1" instead (without quotes). Note: If the minimum time is not integral, round the value to the least integer greater than minimum time." This suggests we compute the minimal possible time (maybe real-valued) among options and then round that up if needed. But to be precise, if we have two options with total times that may be non-integer, the minimum among them could be non-integer; the final answer should be the ceiling of that minimal value. Alternatively, we might need to compute per-train total time as a real number and then for each, if we choose that train, the time we actually get? But the note says: "If the minimum time is not integral, round the value to the least integer greater than minimum time." So the output should be an integer, the ceiling of the minimal total time. Let's verify with sample: T0=5, T1=5, T2=8, V1=100, V2=90, D=320.
For train1: T0<=5, total = 5 + (320/100)60 = 5 + 3.260 = 5 + 192 = 197 exactly integer. For train2: total = 8 + (320/90)60. (320/90 = 3.555555...)60 = 213.333333... So total = 221.33333? Wait 8 + 213.33333 = 221.33333. But they said 214+8 = 222? Let's recalc: 320/90 = 3.555555... hours. Multiply by 60 gives minutes: 3.55555560 = 213.33333 minutes. So total = 8 + 213.33333 = 221.33333 minutes. But they said "Total time for second train 214+8 = 222 minutes". Why 214? Because they computed travel time: (320/90) = 3.555556 hours = 3 hours + 0.555556 hours = 360=180 min + 0.55555660=33.33336 min total 213.33336 min? Actually 0.55555660 = 33.33336, so 180+33.33336 = 213.33336. Then they say "Least integer greater than 213.33336 is 214. Total time for second train 214+8 = 222 minutes." Wait they rounded the travel time individually? That seems odd. They said "If the minimum time is not integral, round the value to the least integer greater than minimum time." In their example, the minimum total time is 197 (integer) from train1, so answer 197. But they computed second train's total as 222, which is 8 + ceil(213.33336) = 8 + 214 = 222. So they rounded up the travel time individually? But they didn't need to, because they compare to 197; but they did compute second train's total as rounded up. Actually they wrote: "For second train \frac{320}{90} = 3.555556; hrs; = 213.33336 minutes. Least integer greater than 213.33336; is ;214. Total time for second train 214+8 = 222 minutes." So they rounded the travel time to the ceiling before adding? But the note about rounding says: "If the minimum time is not integral, round the value to the least integer greater than minimum time." That suggests that for each possible train, we might consider the actual time as T_i + travel_time, and if that sum is not integer, we round it up? But the note says "the minimum time", which could refer to the minimum among these rounded-up times? Or it might be that the time to reach city B is the sum of waiting and travel; but travel time might be fractional and we may need to round up to the next minute because maybe you cannot arrive at fractional minutes? Or maybe they treat arrival time as continuous? The problem statement is ambiguous.
Let's read again: "Roy wants to know the minimum time (in minutes) to reach city B, if he chooses train optimally. Note: If the minimum time is not integral, round the value to the least integer greater than minimum time." Could be interpreted as: compute the minimum time (real) among options, then if that real number is not integer, output its ceiling. In sample, minimum real time is 197 exactly, so output 197. For second train, the real time is 221.333... but they computed 222? That is for illustration only to show that first train is better. But they could have just compared 197 vs 221.333 and see 197 is less. But they purposely rounded second train's travel time. However, the note about rounding might be about the final answer, not per train. In sample, the answer is integer, so no rounding needed. They computed second train's total as rounded up to illustrate that even after rounding it's still larger? But they wrote "Least integer greater than 213.33336 is 214. Total time for second train 214+8 = 222 minutes." That could be just them approximating the travel time in minutes as a ceiling, but not necessarily part of algorithm. Actually typical contest problems often require that you output the total time in minutes as an integer, and if the exact time is fractional, you should consider that arrival can happen at fractional minutes? But they want integer output. Usually they ask to round up the total time if not integer. The note: "If the minimum time is not integral, round the value to the least integer greater than minimum time." So we should compute the minimal total time across options, where each total time is the sum of departure waiting (integer minutes) and travel time (in minutes, possibly fractional). Then if that minimal value is not integer, output its ceiling. That matches the note: "the minimum time" is the real number, and we round it up if not integral. However, what about the per train rounding? Could be they intend that the travel time is in minutes and we should convert to minutes by multiplying D/V * 60, which could be fractional, but the actual arrival time might be at that exact fractional moment, so the minimum time is that real number. Then output integer by rounding up. That is typical. But why did they round travel time individually? Possibly because they were explaining that the second train total is about 221.333... but they wanted to show that even if you round up it's 222, which is >197. They could have said "total time is 221.33 minutes, which is still greater than 197". But they gave integer 222, maybe because they wanted to show the 'least integer greater than' the travel time? Hmm.
Let's examine constraints: all inputs are integers. T_i, T0 are minutes. V_i km/h, D km. Travel time in minutes = (D / V_i) * 60 = (60 * D) / V_i. So it's rational number. The total time = T_i + (60 * D) / V_i. This may be fractional. The final answer should be integer. So we need to compute min_total = min_{i: T0 <= T_i} (T_i + (60*D)/V_i). Then output ceil(min_total). But if both trains not catchable, output -1.
Now check sample: For train1: T1=5, (60320)/100 = 19200/100? Actually 60320=19200, /100=192 exactly integer. So total = 5+192=197. For train2: (60*320)/90 = 19200/90 = 640/3? 19200/90 = 1920/9 = 640/3 ≈ 213.3333. So total = 8+640/3 = (24+640)/3? Actually 8 = 24/3, so total = (24+640)/3 = 664/3 ≈ 221.3333. Minimum = 197. Ceil(197)=197.
That matches output.
Thus interpretation: compute min real total, output its ceil.
But need to be cautious about rounding errors because of floating point. We can compute exactly using rational numbers: total_i = T_i + (60 * D) / V_i. To find min and then ceil, we can compare fractions. Or we can compute each as a fraction and find the minimum.
We can compute using integer arithmetic: Represent total_i as rational number with denominator V_i: total_i = (T_i * V_i + 60 * D) / V_i. Because (60D)/V_i added to T_i gives (T_iV_i + 60*D)/V_i.
But careful: T_i is minutes; it's integer. So total_i = (T_i * V_i + 60 * D) / V_i.
Now we need to find the minimum among those fractions for eligible i. Then compute ceil of that minimum.
We can compute min fraction and then compute its ceiling integer. Since we need to output integer, we can compute the minimal total_i as a rational, then compute the smallest integer >= that rational.
Alternatively, we can compute for each eligible train the minimal integer time: ceil(total_i) = floor( (T_i * V_i + 60 * D - 1) / V_i ) + 1? Actually ceil( (numerator) / denominator ) = floor( (numerator + denominator - 1) / denominator ). So ceil(total_i) = floor( (T_iV_i + 60D + V_i - 1) / V_i ). But careful: total_i = (T_iV_i + 60D) / V_i, not necessarily integer. Then ceil = ( (T_iV_i + 60D) + (V_i - 1) ) // V_i, if numerator and denominator are positive.
But we need the minimal among ceil(total_i)? Or minimal among total_i and then ceil? Because ceil(min) is not necessarily equal to min(ceil(...)). Example: total1 = 5.1, total2 = 5.6. min = 5.1, ceil = 6. But min(ceil(5.1), ceil(5.6)) = min(6,6) = 6 same. Another example: total1 = 5.1, total2 = 5.9, ceil(min)=6, min(ceil)=min(6,6)=6. If they are close: total1=5.1, total2=5.01, ceil(min)=ceil(5.01)=6, min(ceil)=min(6,6)=6. Actually they are both 6. Let's find example where ceil(min) != min(ceil). Suppose total1 = 5.0 exactly, total2 = 5.5. min = 5.0, ceil(min)=5. min(ceil)=min(5,6)=5. Same. If total1=5.1, total2=5.0, min=5.0, ceil(min)=5, min(ceil)=min(6,5)=5. So they are equal? Let's think: For any set of real numbers, ceil(min) <= min(ceil). Because min(x) <= each x, so ceil(min) <= ceil(x) for each x, so ceil(min) <= min(ceil(x)). But is it strictly less possible? Consider total1 = 5.0, total2 = 5.0: ceil(min)=5, min(ceil)=5. Consider total1 = 4.2, total2 = 4.8: ceil(min)=5, min(ceil)=ceil(4.2)=5, ceil(4.8)=5, min=5. So equal. Actually ceil(min) can be less than min(ceil) only if min is exactly integer? Wait: Suppose min = 5.0 exactly, then ceil(min)=5. All other numbers >=5.0, so ceil of others is at least 5. So min(ceil) = 5. So equal. Suppose min = 4.999..., ceil(min)=5. Others >=4.999... so others' ceil >=5. So min(ceil) >=5, but ceil(min)=5, so min(ceil) >=5. Could be >5 if all others are >5? But if min is 4.999... (just below 5), then ceil(min)=5. For any other number x > min, x could be 5.001, ceil(x)=6, min(ceil)=5? Actually if there is another number x that is exactly 5.0? If x=5.0, ceil=5, then min(ceil)=5. If all other x are >5, ceil(x) >=6, then min(ceil)=5? No, we have two values: ceil(min)=5 and ceil(x) maybe 6. Then min(ceil) = min(5,6) = 5. So again equal. Because ceil(min) is the ceil of the minimum, which is one of the ceil values? Actually ceil(min) is not necessarily a ceil of one of the numbers? It is the ceil of the smallest number. The smallest number's ceil is one of the ceil values. So min(ceil) <= ceil(min) because the ceil of the min is itself a ceil of a number in the set, and min(ceil) is the minimum among all ceils, so min(ceil) <= ceil(min). Wait, careful: Let A = {x_i}. Let y_i = ceil(x_i). Then min(y_i) is the smallest among those ceilings. Since min(x) is some x_j (the argmin), then y_j = ceil(x_j) = ceil(min(x)). So ceil(min(x)) is one of the y_i. Therefore, min(y_i) <= y_j = ceil(min(x)). Actually min(y_i) is less than or equal to each y_i, so min(y_i) <= y_j. So we have min(ceil) <= ceil(min). They are not necessarily equal: min(ceil) could be smaller if there is another x_k > min(x) but with ceil(x_k) < ceil(min(x)). Is that possible? ceil(x_k) is integer. Suppose min(x)=4.2, ceil(min)=5. Another x_k = 4.8, ceil=5 as well. So min(ceil)=5. Suppose min=4.2, another x_k=4.0? but then 4.0 would be min. So not. For ceil(x_k) to be less than ceil(min), we need x_k < min? But min is smallest, so that's impossible. So ceil(x_k) >= ceil(min) if x_k >= min? Actually if min=4.2, ceil(min)=5. For x_k=4.0, that would be smaller, so not possible. So indeed ceil(min) is the smallest ceil among the set? Let's test: min=4.6, ceil=5. Any other number x_k >4.6: its ceil could be 5 if x_k <=5, or 6 if >5. So ceil(x_k) >=5? If x_k =4.7, ceil=5. So ceil(min)=5, and ceil(x_k)=5, so min(ceil)=5. If min=4.0 exactly, ceil=4, x_k=4.2 ceil=5, min(ceil)=4. So min(ceil) could be less than ceil(min) if min is integer? Actually ceil(min)=4. x_k's ceil=5. So min(ceil)=4, which equals ceil(min) (since 4=4). So still equal. So indeed min(ceil) = ceil(min). Because the smallest number's ceil is always the smallest among all ceilings? Prove: Let m = min_i x_i. Then ceil(m) is integer c. For any i, x_i >= m, so ceil(x_i) >= ceil(m) if? Not always: Consider m = 4.0 exactly, ceil(m)=4. x_i could be 3.9? No because m is min, so x_i >= 4.0. So ceil(x_i) >= 4. So min_i ceil(x_i) >= 4. Also ceil(m) = 4, and since m is one of the x_i (or maybe not exactly if there is tie, but there is at least one x_i = m, we can consider if there is an x_j = m exactly? Not necessarily: the minimum might be achieved by a unique x_j; but even if not exactly achieved, we can consider a sequence approaching m but not equal? But in our set, we have finite numbers; the minimum is attained by at least one? Since it's a finite set, there is some index j such that x_j = m (if we consider min of a finite set, the minimum is attained). Actually if we have real numbers, the minimum of a finite set exists and is equal to one of the numbers (since it's a finite set). So there is some j with x_j = m. So ceil(x_j) = ceil(m) = c. Therefore, min_i ceil(x_i) <= c (since j is in set). And we have min_i ceil(x_i) >= c? Let's check: For any i, x_i >= m, but does x_i >= m imply ceil(x_i) >= ceil(m)? Not always: If m = 5.0, ceil(m)=5. x_i=5.0, ceil=5. If m=5.0, x_i=5.1, ceil=6 >=5. If m=5.0, x_i=5.0, ceil=5. So ceil(x_i) >= ceil(m) holds? What about m=5.0? x_i=5.0 -> ceil=5. So >=5. So yes, if m is integer, ceil(m) = m. For x_i >= m, ceil(x_i) >= m = ceil(m). If m is not integer, say m=4.2, ceil(m)=5. For x_i >=4.2, the smallest possible ceil is 5 (if x_i=4.2, ceil=5; if x_i=4.9, ceil=5; if x_i=5.0, ceil=5). So ceil(x_i) >=5 = ceil(m). So indeed ceil(x_i) >= ceil(m) for all i. Hence min_i ceil(x_i) >= ceil(m). Also we have ceil(m) is one of the ceil(x_j). So min_i ceil(x_i) <= ceil(m). Therefore equality holds: min_i ceil(x_i) = ceil(m). So indeed the min of the ceilings equals the ceiling of the min. So we can safely compute per-train ceil(total) and then take min among eligible trains, and output that. That avoids dealing with fractional comparisons. Because we need to output integer, we can compute for each catchable train: total_ceil = ceil( T_i + (60*D)/V_i ). Then answer = min among those. If none, -1.
Now need to compute ceil(total_i) correctly with integer arithmetic to avoid floating errors. total_i = T_i + (60D)/V_i. So ceil(total_i) = smallest integer >= T_i + (60D)/V_i.
Let numerator = T_i * V_i + 60 * D. Then total_i = numerator / V_i. Ceil(total_i) = floor( (numerator + V_i - 1) / V_i ), provided V_i > 0. Because integer division rounding up.
But careful: if numerator is exactly divisible by V_i, then (numerator + V_i - 1) // V_i = (numerator // V_i) + ((V_i - 1) // V_i)? Actually if numerator % V_i == 0, then numerator / V_i is integer; ceil = that integer. But (numerator + V_i - 1) // V_i = (kV_i + V_i -1)//V_i = k + (V_i-1)//V_i = k+0? Actually (kV_i + V_i - 1) // V_i = k + (V_i - 1) // V_i = k + 0? Wait, integer division: (k*V_i + V_i - 1) divided by V_i = k + floor((V_i - 1)/V_i) = k, because V_i -1 < V_i. So floor = k. So it gives k, which is correct. So formula works.
Thus for each train i, if T0 <= T_i, compute:
total_ceil_i = (T_i * V_i + 60 * D + V_i - 1) // V_i
Then answer = min(total_ceil_i) among eligible. If none, -1.
Now check sample: train1: T1=5, V1=100, D=320: T1V1=500, 60D=19200, sum=19700. V1=100. (19700+99)//100 = (19799)//100 = 197? 19799/100=197.99? Actually integer division: 19799 // 100 = 197? Wait compute: 100197 = 19700, remainder 99. So 19799 // 100 = 197? Because 197100=19700, 198*100=19800 >19799, so floor = 197. So total_ceil_i=197. That's correct.
Edge Cases: T0 exactly equal to T_i is catchable. T0 > T_i, not catchable.
Multiple test cases up to 10^4, each with small numbers. So O(1) per test case.
We must also note: The problem statement says "First line will contain integer T, number of test cases. Second line will contain integers ..." That implies there is exactly one test case? But sample input shows T=1 and then next line the six numbers. So for multiple test cases, there will be T lines each containing the six numbers? Actually the description: "First line will contain integer T, number of test cases. Second line will contain integers ** T_0, T_1, T_2, V_1, V_2, D **". That's ambiguous; it says second line will contain integers. Probably it means for each test case, you read a line with six numbers. But the phrasing "Second line will contain integers ..." might be mistake; in many problems they say "Each of the next T lines contains ...". So we need to read T, then loop T times reading lines with six ints. The sample input only shows one test case.
Thus implementation: read T, then for i from 1 to T: read T0, T1, T2, V1, V2, D. Compute answer.
Check constraints: T up to 10^4, values up to 5000, so 60D up to 300k, T_i * V_i up to 1000500=500k, sum up to 800k, within int. But to be safe, use Python int (no overflow). In C++ we could use long.
Now we must output answer for each test case.
Now potential pitfalls: Might there be trains that depart at same time? It doesn't matter; we consider both.
Also note: Could Roy reach city B without taking train? The problem says "Roy wishes to meet her in city B. There are two trains available from city A to city B." So only trains. So if cannot catch either, -1.
Now confirm with additional tests.
Example: T0=10, T1=5, T2=15, V1=100, V2=100, D=100. Train1 departs at 5, before Roy arrives at 10, so cannot catch. Train2 at 15, catchable. Travel = (60*100)/100=60 min. total = 15+60=75. Ceil =75. Answer 75.
Example: Both catchable but one is faster: T0=5, T1=10, T2=20, V1=50, V2=100, D=100. Train1: total =10+120=130. Train2: total=20+60=80. Answer min ceil =80.
If travel not integer: T0=0, T1=0, V1=3, D=1. Travel =60/3=20 minutes exactly? 60/3=20, integer. Not fractional. For fractional: V1=7, D=1 => travel = 60/7 ≈ 8.5714, total=8.5714. Ceil =9. So answer 9.
Now test with both fractional but one smaller real but after ceiling maybe equal? e.g., T0=0, train1: T1=0, V1=3, D=1 => total=20? Actually 60/3=20. Train2: T2=1, V2=4, D=1 => total=1+15=16? Actually 60/4=15, so total=16. So min ceil =16.
Now test scenario where min real is fractional and both trains yield same ceil? Works.
Thus algorithm is straightforward.
But need to consider: Is it always T0 <= T_i to be catchable? Could Roy catch a train that departs exactly at the same time he arrives? Yes.
Now what about possibility that Roy could reach city B by taking a combination? No.
Now confirm that the problem does not mention any other mode of transport.
Thus solve.
Now implement in Python.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
T0 = int(next(it)); T1 = int(next(it)); T2 = int(next(it)); V1 = int(next(it)); V2 = int(next(it)); D = int(next(it))
# compute for each train
ans = None
if T0 <= T1:
total1 = (T1 * V1 + 60 * D + V1 - 1) // V1
if ans is None or total1 < ans:
ans = total1
if T0 <= T2:
total2 = (T2 * V2 + 60 * D + V2 - 1) // V2
if ans is None or total2 < ans:
ans = total2
if ans is None:
out.append("-1")
else:
out.append(str(ans))
sys.stdout.write("\n".join(out))
Check constraints: T up to 10000, reading all at once is fine.
Now test sample input: "1\n5 5 8 100 90 320\n" -> output 197.
Test other scenarios.
Let's test fractional case: Input: 1\n0 0 0 7 7 1\n Both catchable, T0=0 <=0. D=1, V=7: travel =60/7 ≈8.5714, total=8.5714, ceil=9. So answer 9.
Now test where both trains not catchable: T0=100, T1=50, T2=80, V1=10, V2=10, D=100. Output -1.
Works.
Now check edge: T0 = T1 but T1=0? Then catchable. Compute.
Now test multiple test cases: Input:
2
5 5 8 100 90 320
0 0 0 7 7 1
Expected output:
197
9
Our code should produce that.
Now final note: Could there be any nuance about average velocities? They are given in km/hr. Travel time = D / V_i hours, multiply by 60 to minutes. That's correct.
Now let's consider rounding: "If the minimum time is not integral, round the value to the least integer greater than minimum time." We concluded that min of ceil of each is same as ceil of min. So we can compute min(ceil(...)). But to be absolutely sure, we need to verify that they define "minimum time" as the minimal total time among the options, and then we round that up. That matches our math.
Thus answer.
Now implement.
But wait: The problem statement says: "The two trains departs in T_1 and T_2 minutes respectively." Could it mean that the trains depart at times T1 and T2 minutes from now? Yes. So we are consistent.
Now one more nuance: Could Roy catch a train that departs earlier if he arrives later? Obviously no. So condition T0 <= T_i.
Now about the note: "If its not possible to reach city B, print "-1" instead". That's clear.
Thus solution done.
Now double-check sample explanation: They added T0 (5) to travel time (192) to get 197. That's because they assume he waits exactly? They didn't add wait time? Actually total = T_i + travel. For first train, T1=5, he arrives at 5, train leaves at 5, so wait 0. So total 5+192=197. That matches. So our formula matches.
Thus answer.
Now we'll code.
But I recall similar problem on CodeChef? Possibly "TRAINS" or something. Yes, typical problem.
Now to be thorough, also consider if there is any scenario where rounding per train might differ? Since we computed per-train ceil and then min, it's same as ceil of min. So fine.
Now, I will produce final solution explanation and code.
Now let's think about potential pitfalls: The values T1 and T2 are given, but maybe they are the times of departure from now? Yes. So we treat them as absolute times from current moment.
Now, confirm that T0, T1, T2 are minutes, and velocities in km/h, D in km. So conversion factor: hours to minutes = 60. So travel minutes = (D / V_i) * 60.
Now, we must use integer arithmetic to compute ceil. Using formula: ceil(a/b) = (a + b - 1) // b, for positive a,b. Here a = T_i * V_i + 60*D, b = V_i. All positive.
Edge: V_i can be up to 500, T_i up to 1000, D up to 5000, so a up to 1000500+605000=500000+300000=800000, so safe.
Now we also need to ensure that when we compute 60*D it doesn't overflow, but in Python fine.
Now produce final answer.
I'll now write the code.
But I recall something: There is also possibility that Roy may be able to catch a train if he arrives exactly at departure time. That's what we did.
Thus finalize.
But check if there is any trick: The problem statement in input: "Second line will contain integers ** T_0, T_1, T_2, V_1, V_2, D **". That might mislead that there is only one test case? Actually it says first line contains integer T, number of test cases. So after that, there should be T lines. But they said "Second line will contain integers...". Could be that for each test case, there is a line. Probably it's a misprint. We'll assume T lines.
Now all good.
Let's test some random cases manually: Suppose T0=3, T1=4, T2=10, V1=5, V2=10, D=10. Travel1 = (6010)/5=120 min, total1 = 4+120=124; travel2 = (6010)/10=60 min, total2 = 10+60=70; catchable both? T0=3 <=4 and <=10, yes. Min real =70, ceil=70. Answer 70.
Our code: train1: (45+600+5-1)//5 = (20+600+4)//5=624//5=124? Actually 624/5=124.8? Wait compute: 45=20, 60D=600, sum=620, +V1-1=5-1=4 =>624, //5 =124 (since 5124=620? Actually 5124=620, remainder 4? 5124=620 exactly. 624-620=4, so integer division yields 124? No, 624 // 5 = 124, because 5124 = 620, 5125 = 625 >624, so floor =124. That's ceil? 620/5=124 exactly, but we added V-1 to get 624, which gave 124? Actually total1 real = 4 + 600/5 = 4+120=124 exactly. So ceil=124. That's correct. train2: (1010+600+10-1)//10 = (100+600+9)//10=709//10=70 (since 1070=700, remainder 9). So 70. Min=70. Good.