Greedy
Two players are engaged in a game called "Greedy". They start with a pile of n
candies, and take turns removing between 1 and k
candies from the pile. The player who takes the last candy loses the game. Your task is to determine how many candies the first player should take on their first turn to ensure a win, assuming both players make optimal moves.
Input
The first line contains the number of games t
(1 ≤ t ≤ 10^5
). Each of the following t
lines provides the details for a game with two integers: n
(1 ≤ n ≤ 10^9
) and k
(1 ≤ k ≤ 10^9
).
Output
For each game, output the number of candies the first player should take to guarantee a win. If the first player is destined to lose regardless of their strategy, output 0. If there are multiple valid answers, you may output any one of them.