5310: I love permutation

内存限制:128 MB 时间限制:2.000 S
评测方式:文本比较 命题人:
提交:3 解决:2

题目描述

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

Mr.I creates a sequence bx=ax(modP) of length P1 ,where1xP1

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 bi>bj.
 

输入

The first line contains an integer T(T105) . Then T test cases follow.

Each test case contains two integers a,P(1a<P1018).

输出

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