#P6970. I love permutation

    ID: 5827 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>2021“MINIEYE杯”中国大学生算法设计超级联赛(2)

I love permutation

Problem Description

Mr.I has a positive integer a and an odd prime number P, satisfying a<P.

Mr.I creates a sequence $b_x=ax(mod P)$ of length $P-1$ ,where$1\leq x \leq P-1$。

Now Mr.I wants to know how many reversed pairs there are in this sequence.

Since the answer may be very large, you only need to output the value of the answer pair modulo $2$.

The definition of a reverse pair is a two-tuple $(i, j)$ that satisfies $i<j$ and $b_i>b_j$.

Input

The first line contains an integer $T(T\leq {10}^{5})$ . Then $T$ test cases follow.

Each test case contains two integers $a,P(1\leq a < P \leq {10}^{18})$.

Output

For each test case, output a single line contain the answer for the test case.

4 2 7 3 7 4 7 5 7
0 1 0 1