CF1492A.Three swimmers

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.

It takes the first swimmer exactly aa minutes to swim across the entire pool and come back, exactly bb minutes for the second swimmer and cc minutes for the third. Hence, the first swimmer will be on the left side of the pool after 00 , aa , 2a2a , 3a3a , ... minutes after the start time, the second one will be at 00 , bb , 2b2b , 3b3b , ... minutes, and the third one will be on the left side of the pool after 00 , cc , 2c2c , 3c3c , ... minutes.

You came to the left side of the pool exactly pp minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.

输入格式

The first line of the input contains a single integer tt ( 1t10001 \leq t \leq 1000 ) — the number of test cases. Next tt lines contains test case descriptions, one per line.

Each line contains four integers pp , aa , bb and cc ( 1p,a,b,c10181 \leq p, a, b, c \leq 10^{18} ), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.

输出格式

For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.

输入输出样例

  • 输入#1

    4
    9 5 4 8
    2 6 10 9
    10 2 5 10
    10 9 9 9

    输出#1

    1
    4
    0
    8

说明/提示

In the first test case, the first swimmer is on the left side in 0,5,10,15,0, 5, 10, 15, \ldots minutes after the start time, the second swimmer is on the left side in 0,4,8,12,0, 4, 8, 12, \ldots minutes after the start time, and the third swimmer is on the left side in 0,8,16,24,0, 8, 16, 24, \ldots minutes after the start time. You arrived at the pool in 99 minutes after the start time and in a minute you will meet the first swimmer on the left side.

In the second test case, the first swimmer is on the left side in 0,6,12,18,0, 6, 12, 18, \ldots minutes after the start time, the second swimmer is on the left side in 0,10,20,30,0, 10, 20, 30, \ldots minutes after the start time, and the third swimmer is on the left side in 0,9,18,27,0, 9, 18, 27, \ldots minutes after the start time. You arrived at the pool 22 minutes after the start time and after 44 minutes meet the first swimmer on the left side.

In the third test case, you came to the pool 1010 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!

In the fourth test case, all swimmers are located on the left side in 0,9,18,27,0, 9, 18, 27, \ldots minutes after the start time. You arrived at the pool 1010 minutes after the start time and after 88 minutes meet all three swimmers on the left side.

首页