#abc228d. [abc228_d]Linear Probing
[abc228_d]Linear Probing
Problem Statement
There is a sequence with terms. Initially, every term is .
Process queries in order. The -th query is described by an integer such that or , and another integer , as follows.
- If , do the following in order.
- Define an integer as .
- While , keep adding to . We can prove that this process ends after finite iterations under the Constraints of this problem.
- Replace the value of with .
- If , print the value of at that time.
Here, for integers and , denotes the remainder when is divided by .
Constraints
- There is at least one such that .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
For each query with , print the response in one line. It is guaranteed that there is at least one such query.
Sample Input 1
4
1 1048577
1 1
2 2097153
2 3
Sample Output 1
1048577
-1
We have , so the first query sets .
In the second query, initially we have , for which , so we add to . Now we have , so this query sets .
In the third query, we print .
In the fourth query, we print .
Note that, in this problem, is a constant and not given in input.