Multiplication

No attempts yetTime limit5sMemory limit128 MB

Problem

On yet another rainy Saturday, Staś could not go outside to play ball, so he stayed home and did what he enjoys most: multiplication.

Starting from 11, Staś kept multiplying his running product by any natural number of at most five digits that came to mind. To the number obtained this way he finally added 11, and to his amazement that number pp turned out to be prime.

Taking this as a good omen, Staś played on. This time he picked two different natural numbers aa and bb. As before he started from 11, but now he repeatedly multiplied his running product by the same number aa, until the remainder of the product modulo pp became bb. Once he finally managed it, Staś fell asleep, worn out by all the multiplying.

How many multiplications did Staś have to perform in this second game?

Input

The first line contains the number of test sets ZZ (1Z21 \le Z \le 2).

The second line contains the prime pp that Staś obtained in the way described above (2p10182 \le p \le 10^{18}). Because p1p - 1 is a product of natural numbers of at most five digits, every prime factor of p1p - 1 is at most 9999999999.

Each of the following ZZ lines contains two different natural numbers aa and bb (1<a,b<p1 < a, b < p).

Output

For each test set, print on its own line the number of multiplications needed so that, starting from 11 and repeatedly multiplying by aa, the remainder modulo pp becomes bb. Equivalently, this is the smallest positive integer kk with akb(modp)a^k \equiv b \pmod{p}. If reaching bb is impossible, print 1-1.

Hint

In the example above, the prime p=13p = 13 arises as follows. Staś first multiplied by 44 (so he started from 44), which is not prime. He then multiplied by 33 to get 1212, and finally adding 11 gave the prime 1313.

Starting from 11 and repeatedly multiplying by 1212 yields 12,1,12,1,12, 1, 12, 1, \dots, so 99 never appears. Multiplying by 44 instead yields 4,3,12,9,104, 3, 12, 9, 10 in turn, so 1010 is reached after five multiplications.