Eidam Sand Lair
Time limit1sMemory limit1024 MB
For each test case, find the minimum time for a person at floor Yp to reach floor 0, either walking, riding the lift from floor Lp, or meeting it at an intermediate floor.
- Level
Medium5 of 10
- Topics
- Math, Greedy, Implementation, Brute force
- Solved
- No attempts yet
Problem
In your base you dug through sand and rock to build a deep pit with stairs and a lift. The pit reaches far underground, and on various floors you keep equipment while on the deepest levels you leave cheeses to age, particularly Eidam. You often stroll downstairs, but you wonder whether it is faster to walk up to the surface or to take the lift for part or all of the trip.
You know the position and the speed of yourself and of the lift. The surface floor is numbered 0, and positive numbers denote underground floors by their distance from the surface. You are alone on Mars, so you are the only one who uses the lift. Starting and stopping takes almost no time for you or the lift, so you neglect it. When the lift is called to several floors, it moves to them in the order they were called. The lift is nothing special: once you enter it, you may order it to go to an arbitrary floor. In that case any earlier calls must be handled first.
Input
The first line of input contains a single integer T (1 ≤ T ≤ 104), the number of test cases. The next T lines each contain one test case. Each test case consists of four integers Yp, Lp, Ys, Ls, where 0 ≤ Yp, Lp ≤ 109 are the initial floors of you and the lift, and 0 < Ys, Ls ≤ 106 are the times it takes you and the lift to move one floor.
Output
For each test case, print a single integer: the minimum time it takes you to move from your initial floor to floor 0.
Hint
Sample Input 1 contains two test cases. In the first one you would rather walk to floor 0 than wait for the lift, which is 18 floors away. In the second one, an example of the fastest way is to call the lift and walk one floor up in the meantime, then call the lift to the 9th underground floor and wait for it, after which you take it to floor 0.