atcoder#ARC150B. [ARC150B] Make Divisible

[ARC150B] Make Divisible

Score : 500500 points

Problem Statement

You are given positive integers AA and BB.

Find the minimum value of X+YX+Y for non-negative integers XX and YY such that B+YB+Y is a multiple of A+XA+X.

You have TT test cases to solve.

Constraints

  • 1T1001 \leq T \leq 100
  • 1A, B1091 \leq A,\ B \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_{1}

case2\mathrm{case}_{2}

\vdots

caseT\mathrm{case}_{T}

Each case is in the following format:

AA BB

Output

Print TT lines. The ii-th line should contain the answer for the ii-th test case.

5
11 23
8 16
4394 993298361
95392025 569922442
8399283 10293
2
0
65
2429708
8388990

For the first test case, if we let X=1X=1 and Y=1Y=1, then B+Y=24B+Y=24 will be a multiple of A+X=12A+X=12. Here, we have X+Y=2X+Y=2, and there is no way to make X+YX+Y smaller, so the answer is 22.

For the second test case, if we let X=0X=0 and Y=0Y=0, then B+Y=16B+Y=16 will be a multiple of A+X=8A+X=8.