Pisano Period

No attempts yetTime limit1sMemory limit128 MB

Problem

In 1960, Donald Wall, who worked at IBM, proved that the remainders of the Fibonacci numbers modulo mm form a repeating cycle.

For example, the first 10 Fibonacci numbers and their remainders modulo 11 are shown below.

nn12345678910
F(n)F(n)11235813213455
F(n)mod11F(n) \bmod 1111235821010

The sequence of remainders repeats the same block over and over. Writing k(m)k(m) for the length of that repeating block, k(11)=10k(11) = 10.

Wall proved several other properties.

  • k(m)k(m) is even whenever m>2m > 2.
  • For every even integer nn greater than 22 there is an mm with k(m)=nk(m) = n.
  • k(m)m21k(m) \le m^2 - 1
  • k(2n)=3×2n1k(2^n) = 3 \times 2^{n-1}
  • k(5n)=4×5nk(5^n) = 4 \times 5^n
  • k(2×5n)=12×5nk(2 \times 5^n) = 12 \times 5^n
  • k(10n)=15×10n1k(10^n) = 15 \times 10^{n-1} when n>2n > 2

Given mm, write a program that computes k(m)k(m).

Input

The first line contains the number of test cases PP.

Each of the next PP lines contains two integers NN and MM separated by a single space. NN is the number of the test case and MM is the mm described above.

Output

For each test case, print the test case number NN and k(M)k(M) on one line, separated by a single space. Keep the order in which the test cases were given.

Constraints

  • 1P1,0001 \le P \le 1{,}000
  • 2M1,000,0002 \le M \le 1{,}000{,}000
  • The sum of k(M)k(M) over all test cases is at most 500,000500{,}000.