Number Guessing

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Alice has a secret integer yy, which is selected from \[1,1018]\[1,10^{18}]. Bob wants to get the number, so he asks Alice some questions. In each question, Bob gives an integer xx in \[1,1018]\[1,10^{18}] to Alice, and Alice returns 00 if y<xy < x, 11 if y=xy = x, and 22 otherwise.

Everything sounds great, but the communication between Alice and Bob has been tampered with by Eve! Eve wrote a random number generator H\mathcal{H} that generates pseudorandom numbers between 00 and n1n-1. Every time Bob gives the number, Eve will call H\mathcal{H} to generate a pseudorandom number xx, and gives Bob the bitwise exclusive-or sum of xx and the result that Alice returns.

Bob found that the result returned by Alice has been tampered with by someone because he is getting conflicting results. By some means, Bob obtained H\mathcal{H}'s source code, which is shown below.

Fig. 1: How H\mathcal{H} works.

The constants PP and nn are specified in the code, so Bob also knows their values. But he knows nothing about the seed\mathit{seed}, except that he knows the tamperer, Eve, will not modify the value during the interaction. Could you help Bob get the value of Alice's secret number in 100100 queries?

입력

The first line contains a single integer tt, the number of test cases (1t1001 \le t \le 100). Descriptions of the test cases follow.

Each test case starts by a line containing two integers nn and PP: the two constants in H\mathcal{H}. It is guaranteed that 3n43 \le n \le 4, 10P101810 \le P \le 10^{18}, and PP is a prime number.

힌트

In the example, we have seed=0\mathit{seed} = 0, so Bob always receives the correct answer from Alice. It is reasonable for Bob to directly use binary search in this test case, but it does not apply to other possible situations.