E. Fiboxor

Time limit: 1s
Memory limit: 64MB
Input:
Output:

Consider the following integer sequence ff:

f1=f2=1f_1 = f_2 = 1, fk=fk1fk2(fk1+fk2)f_k = |f_{k-1} - f_{k-2}| \oplus (f_{k - 1} + f_{k - 2}), for k3k \geq 3.

Now, you are wondering, for qq triples of integers (l,r,Ml, r, M), what is the sum of all fif_i, lirl \leq i \leq r, modulo MM. That is, find (i=lrfi)\displaystyle(\sum_{i = l}^rf_i) and print it's remainder modulo MM.

By x|x| we denote the absolute value of xx and by \oplus we denote the bitwise XOR operation.

Input data

In the first line of input, you will read one integer qq (1q21051 \leq q \leq 2 \cdot 10^5).

In the next qq lines, you will read three integers ll, rr (1lr1091 \leq l \leq r \leq 10^9) and MM (108<M<10910^8 < M < 10^9, MM is prime).

Output data

Output qq lines, the ithi^{th} of them representing the answer for the ithi^{th} triple (l,r,Ml, r, M).

Example

stdin

4
1 3 998244353
2 4 998244353
4232324 12345678 998244353
92345678 998244353 998244353

stdout

4
5
652671816
367684397

Explanation

The first four values of ff are: f1=1,f2=1,f3=2,f4=2f_1 = 1, f_2 = 1, f_3 = 2, f_4 = 2.

Thus, the answer for the first question is 1+1+2=41 + 1 + 2 = 4 and the answer for the second question is 1+2+2=51 + 2 + 2 = 5.

Log in or sign up to be able to send submissions!