#K8071. Josephus Problem with a Twist: The Taco Subset

    ID: 35591 Type: Default 1000ms 256MiB

Josephus Problem with a Twist: The Taco Subset

Josephus Problem with a Twist: The Taco Subset

This problem is a variation of the classic Josephus Problem. You are given T test cases. For each test case, two integers n and k are provided representing the number of players and the step count respectively. The players are arranged in a circle, and the elimination follows the rule:

$$ J(n, k) = \begin{cases} 1, & n = 1 \\ ((J(n-1, k) + k - 1) \mod n) + 1, & n > 1 \end{cases} $$

Your task is to find the winning (safe) position for each test case.

Note: Input is provided via standard input and output should be sent to standard output.

inputFormat

The first line contains an integer T representing the number of test cases. Each of the following T lines contains two space-separated integers n and k, where:

  • n is the number of players.
  • k is the step count at which a player is eliminated.

All input is given via standard input (stdin).

outputFormat

For each test case, output a single line containing the winning position (an integer). The output for each test case should be printed on a new line to standard output (stdout).

## sample
3
5 2
7 3
10 4
3

4 5

</p>