Pisano Periods#6390

Pisano Periods

1 second
122.174 megabytes
Easy

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:

For this problem, you must write a program that calculates the length of the repeating subsequence for different modulo values .

Input

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.

Output

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 .

Examples

Example 1

Loading
Loading