Shuriken Game

Time limit1sMemory limit128 MB

Summary
Two players remove 1 to N shurikens from a pile, but a player cannot repeat the opponent's previous move; find the smallest winning first move.
Level

Medium7 of 10

Topics
Dynamic programming, Game theory, Implementation
Solved
No attempts yet

Problem

Being a ninja means spending a lot of time training. To pass the time between training sessions, ninjas like to play games with their shurikens (a shuriken is a metal star with sharp corners that ninjas throw at enemies).

This is a game for two players featuring a single stack of shurikens. The players take turns. On a turn, a player removes some shurikens from the stack: at least 11 and at most NN. The player who takes the last shuriken(s) wins.

The basic game was quickly solved, so an extra rule was added to make it interesting again: a player may not take the same number of shurikens the opponent just took (you cannot copy your opponent's last move). If the stack holds exactly 11 shuriken and the opponent has just taken 11, the player to move has no legal move and loses.

For a given situation, determine how the player to move can win.

Input

The first line contains the number of test cases TT. Each test case has the following format:

  • One line with three integers SS, NN, and PP (1≤S≤1000001 \le S \le 100000, 2≤N≤1002 \le N \le 100, 1≤P≤N1 \le P \le N): the number of shurikens in the stack, the maximum number of shurikens a player may take, and the number the opponent took on the last move, respectively.

Output

For each test case, print one integer on its own line: the smallest number of shurikens the player to move can take to secure the win. If there is no winning move, print 00.

Examples1

  1. Example 1

    Input
    5
    12 4 1
    5 5 5
    6 6 6
    100 5 5
    100 5 1
    
    Expected output
    2
    0
    3
    1
    2