precisely a minutes to swim across the whole pool and return, precisely b minutes for the subsequent swimmer and c minutes for the third. Thus, the main swimmer will be on the left half of the pool after 0, a, 2a, 3a, ... minutes after the beginning time, the subsequent one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left half of the pool after 0, c, 2c, 3c, ... minutes.
Correct answer will be upvoted else downvoted. Computer science.
It takes the principal swimmer precisely a minutes to swim across the whole pool and return, precisely b minutes for the subsequent swimmer and c minutes for the third. Thus, the main swimmer will be on the left half of the pool after 0, a, 2a, 3a, ... minutes after the beginning time, the subsequent one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left half of the pool after 0, c, 2c, 3c, ... minutes.
You went to the left half of the pool precisely p minutes after they began swimming. Decide how long you need to stand by before one of the swimmers shows up at the left half of the pool.
Input
The primary line of the input contains a solitary integer t (1≤t≤1000) — the number of experiments. Next t lines contains experiment portrayals, one for every line.
Each line contains four integers p, a, b and c (1≤p,a,b,c≤1018), time in minutes after the beginning, when you went to the pool and times in minutes it take the swimmers to cross the whole pool and return.
Output
For each experiment, output one integer — how long you need to stand by (in minutes) before one of the swimmers shows up at the left half of the pool.
Step by step
Solved in 3 steps with 1 images