atcoder#ABC290D. [ABC290D] Marking
[ABC290D] Marking
Score : points
Problem Statement
There are squares indexed through arranged in a line. Snuke is going to mark every square by the following procedure.
- Mark square .
- Repeat the following steps i - iii times.
- Initialize a variable with , where is the index of the square marked last time.
- While square is marked, repeat replacing with .
- Mark square .
Find the index of the square that Snuke marks for the -th time.
Given test cases, find the answer for each of them.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where denotes the -th test case:
Each test case is given in the following format:
Output
Print lines.
The -th line should contain the answer to the -th test case.
9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5
0
2
1
3
0
3
1
4
2
If and , Snuke marks the squares as follows.
- Mark square .
- (-st iteration) Let . Since square is not marked, mark it. (-nd iteration) Let . Since square is marked, let . Since square is not marked, mark it. (-rd iteration) Let . Since square is not marked, mark it.