Under construction

Still being built. Things may change

Circular Board Game

Time limit2sMemory limit512 MB

Summary
For each query with S, M, N, compute the final cell after advancing expo(S) steps on a circular board of N cells, using modular reduction of a huge power tower.
Level

Medium6 of 10

Topics
Number theory, Math, Implementation
Solved
No attempts yet

Problem

A board game called Expo is played on a circular board with NN cells numbered 11 to NN. Cell NN is followed by cell 11 again, so a token that passes the end keeps going from the start.

A player rolls a die, reads the number SS on the top face and moves the token one cell forward expo⁡(S)\operatorname{expo}(S) times, where

expo⁡(S)=S(S−1)(S−2) ⋯ 21\operatorname{expo}(S) = S^{(S-1)^{(S-2)^{\,\cdots^{\,2^{1}}}}}

So expo⁡(1)=1\operatorname{expo}(1) = 1, expo⁡(2)=21=2\operatorname{expo}(2) = 2^{1} = 2, expo⁡(3)=321=9\operatorname{expo}(3) = 3^{2^{1}} = 9 and expo⁡(4)=49=262144\operatorname{expo}(4) = 4^{9} = 262144. A player starts on cell 11 and wins by standing exactly on cell NN once a move is complete. Passing cell NN in the middle of a move does not count.

Brad and Pete are playing this game. Once SS grows even a little, moving the token expo⁡(S)\operatorname{expo}(S) times takes longer than a lifetime. Their grandfathers never finished the game they started in their youth the traditional way, so the two of them play on their behalf. Given the number SS on the die and the cell MM the token stands on, report the cell the token stands on when the move is finished.

Input

The first line contains one integer TT (1≤T≤2561 \le T \le 256), the number of test cases.

Each of the next TT lines contains three space separated integers SS, MM and NN (1≤S≤1091 \le S \le 10^{9}, 1≤M≤N≤1091 \le M \le N \le 10^{9}): the number on the die, the cell the token stands on, and the number of cells on the board.

Output

For each test case print one line with the number of the cell the token stands on after moving expo⁡(S)\operatorname{expo}(S) cells forward from cell MM.

Examples3

  1. Example 1

    Input
    3
    2 1 39
    94 250 265
    5 123 123456789
    
    Expected output
    3
    24
    16317757
    
  2. Example 2

    Input
    1
    1 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    6
    1 1 10
    2 1 10
    3 1 10
    4 1 10
    5 1 10
    6 1 10
    
    Expected output
    2
    3
    10
    5
    6
    7