The Cheerful Monkey

No attempts yetTime limit1sMemory limit128 MB

Problem

A monkey has found a new game. It arranges nn cages, each holding exactly one animal, in a circle and hops across them. At the start every cage is closed.

The monkey begins on one cage and opens the cage it stands on. After that it always jumps forward by dd cages and opens the cage it lands on. The moment it jumps onto a cage that is already open, the monkey stops.

Every animal in an opened cage seizes the chance and escapes. Determine how many animals escape.

Input

The first line contains one integer zz (1z1061 \le z \le 10^6), the number of data sets. Each of the next zz lines describes one data set.

Each line contains two integers nn and dd (1n,d1091 \le n, d \le 10^9): the number of cages and the length of the monkey's jump, respectively. (d=1d = 1 means the monkey hops to the very next cage.)

Output

For each data set, print on its own line the number of animals that escape.