CERI2018J - Euler Totient of factorized integer

The goal of the problem is to compute the Euler totient function φ(N)\varphi(N) for some integers NN.

Assume that number N=p0e0×p1e1×pkekN = p_0^{e_0} \times p_1^{e_1} \times \cdots p_k^{e_k}, where pip_i are prime numbers, and eie_i are positive intergers.

You will be given a prime factorization of NN, you'll have to print φ(N)(modm)\varphi(N) \pmod m.

Input

The first line of the input consist of a single integer number tt which determines the number of tests.

Each test is on two separate lines.

In each test,

  • on the first line, there is two integer numbers kk, and mm.
  • on the second line, there is 2(k+1)2(k+1) integer numbers pip_i and eie_i, with pip_i a prime number.

Constraints

  • 0<t2560 < t \leqslant 256 ;
  • 0k10000 \leqslant k \leqslant 1000 ;
  • 0<m2×1090 < m \leqslant 2\times10^9 ;
  • 1<pi<2×1091 < p_i < 2\times10^9, a prime number ;
  • 0<ei<2×1090 < e_i < 2\times10^9.

Output

For each test case, print φ(N)(modm)\varphi(N) \pmod m.

Example

Input:
3
0 1000
17,1
2 100
2,1 5,1 7,2
1 1000
3,1 1000000007,1
Output:
16
68
12

Explanation

For the first test case, N=171N = 17^1, and φ(N)(mod1000)=16\varphi(N) \pmod {1000} = 16.

For the second test case, N=21×51×72=490N = 2^1 \times 5^1 \times 7^2 = 490, and φ(N)(mod100)=68\varphi(N) \pmod {100} = 68.

For the third test case, N=31×10000000071=3000000021N = 3^1\times 1000000007^1 = 3000000021, and φ(N)(mod1000)=12\varphi(N) \pmod {1000} = 12.


Added by:Francky
Date:2018-05-08
Time limit:1s-3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.