What a sequence!

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Let a_na\_{n} be a sequence defined by the recursive formula:

\begin{align\*} a\_{n+2} &= k\cdot a\_{n+1} + a\_{n} \\\ a\_{0} &= 0 \\\ a\_{1}  &= 1 \end{align\*}

Given a certain k1,3,5,7k \in \\{1,3,5,7\\} and an odd prime number pp, your task is to find the value of a_pmodpa\_{p} \bmod{p}.

입력

In the first line one integer Z106Z \le 10^6 is given, denoting number of testcases described in following lines.

For each test case, first and the only input line contains two natural numbers pp and kk, pp being an odd prime number.

출력

For each test case you should print exactly one line containing the value of a_pmodpa\_{p} \bmod{p}.

제한

  • k1,3,5,7k \in \\{1,3,5,7\\}
  • The total length of the numbers pp in the all testcases doesn't exceed 10610^{6}.