#YSS82. 公倍佩尔数-Metric times Pell number

公倍佩尔数-Metric times Pell number

At midnight, I dare not look back when crossing the overpass, where it used to be bustling with traffic, now lies the River of Forgetfulness under my feet.
--- Out of the Mountains

Problem Description

Given two positive integers nn and pp, where pp is a prime number, it is guaranteed that for all positive integers nn, (1+2)n(1+\sqrt{2})^n can be expressed as e(n)+2f(n)e(n) + \sqrt{2} f(n), where e(n)e(n) and f(n)f(n) are integers, and f(n)f(n) is not zero modulo pp. Obviously, (12)n=e(n)2f(n)(1-\sqrt{2})^n = e(n) - \sqrt{2} f(n). Let g(n)=lcm(f(1),f(2),,f(n))g(n) = \operatorname{lcm}(f(1), f(2), \dots, f(n)).

Given nn and pp, calculate the value of i=1ni×g(i)\sum \limits_{i=1}^n i \times g(i) modulo pp.

Input Format

  • The first line contains a positive integer TT, representing the number of test cases.
  • The next TT lines each contain two positive integers nn and pp.

Output Format

For each test case, output a single non-negative integer representing the answer for that test case.

Sample Input

5
1 233
2 233
3 233
4 233
5 233

Sample Output

1
5
35
42
121

Hints

Data Size and Constraints

For 100% of the data, 1T2101 \leq T \leq 210, 1n1061 \leq n \leq 10^6, 2p109+72 \leq p \leq 10^9+7, n3×106\sum n \leq 3 \times 10^6.