In 1960, Donald Wall of IBM, in White Plains, NY, proved that the series obtained by taking each element of the Fibonacci series modulo was periodic.
For example, the first ten elements of the Fibonacci sequence, as well as their remainders modulo , are:
The sequence made up of the remainders then repeats. Let be the length of the repeating subsequence; in this example, we see k(11) = 10.
Wall proved several other properties, some of which you may find interesting:
If m > 2, is even.
For any even integer n > 2, there exists such that k(m) = n.
k(m) \le m^2 - 1
k(2^n) \le 3 \cdot 2^{n-1}
k(5^n) = 4 \cdot 5^n
k(2 \cdot 5^n) = 6n
If n > 2, k(10n) = 15 \cdot 10^{n-1}
For this problem, you must write a program that calculates the length of the repeating subsequence for different modulo values .
The first line contains the number of data sets (1 \le p \le 1000) that follow. Each data set is a single line that consists of two space separated integer values and , where is the data set number and (2 \le m \le 1000000) is the modulo value.
For each data set there is one line of output. It contains the data set number followed by a single space, followed by the length of the repeating subsequence for - the value of .
Example 1