#d1902. 数列

数列

Sequence

Problem Description

Xiaoqi loves sequences very much, so she made a sequence problem.

Given two integers d,md, m, find the number of sequences aa satisfying:

  • the length nn of sequence aa is at least 11 (n≥1n \ge 1);
  • 1≤a1<a2<⋯<an≤d1 \le a_1 < a_2 < \dots < a_n \le d;
  • define an array cc of length nn: c1=a1c_1 = a_1, and for all i>1i > 1, ci=ci−1⊕aic_i = c_{i-1} \oplus a_i, where ⊕\oplus denotes binary XOR. The array cc must satisfy c1<c2<⋯<cnc_1 < c_2 < \dots < c_n.

How many such sequences are there? Since the result may be large, output the answer modulo mm.

Input Format

The first line contains an integer tt (1≤t≤1001 \le t \le 100), the number of test cases.

Each of the next tt lines contains two integers d,md, m (1≤d,m≤1091 \le d, m \le 10^9).

Note: mm is not necessarily prime.

Output Format

Output tt lines, each containing one integer: the answer.

Sample Input

3
5 999996
6 99995
7 9994

Sample Output

17
23
29

Constraints

For all data, 1≤t≤1001 \le t \le 100 and 1≤d,m≤1091 \le d, m \le 10^9.