That set is a harmful set

For each rational a/b, print the smallest n from 0 to 10 whose stage set misses it, or -1 when all eleven contain it.

Medium5MathSimulationNo attempts yetTime limit3sMemory limit256 MB

Problem

While studying set theory, Seunghyun ran into the following family of sets.

C0=[0,1]C_0 = [0, 1]

Cn={a+i=1nai3i  |  0a13n,  ai{0,2}}(nN)C_n = \left\{\, a + \sum_{i=1}^{n} \frac{a_i}{3^i} \;\middle|\; 0 \le a \le \frac{1}{3^n},\; a_i \in \{0, 2\} \,\right\} \quad (n \in \mathbb{N})

C=n=0CnC = \bigcap_{n=0}^{\infty} C_n

CC made no sense to him, so he planned two jobs. The first job picks a rational number and decides whether it belongs to CC. If it does not, the second job finds the set with the smallest subscript nn that fails to contain the number.

After doing this by hand a few times he got tired of it and decided to use a computer. Then he got tired of the coding too, so he handed the work to you, his junior. Stuck with the job, you decided to write an incomplete checker to play a trick on him.

If the answers look too strange he will not fall for it, so the output has to stay plausible. The program checks membership only for C0C_0 through C10C_{10}, and if all eleven sets contain the number it declares the number to be in CC. Write that incomplete program.

Input

The first line contains the number of test cases TT.

Each of the next TT lines contains the numerator aa and the denominator bb of a rational number, separated by a space. (0a1000000 \le a \le 100000, 1b1000001 \le b \le 100000)

Output

Print one line per test case.

If some set among C0C_0 through C10C_{10} does not contain ab\frac{a}{b}, print the smallest such subscript nn. If all eleven sets contain it, print -1.

Hint

C1=[0,13][23,1]C_1 = \left[0, \frac{1}{3}\right] \cup \left[\frac{2}{3}, 1\right], and C2=[0,19][29,13][23,79][89,1]C_2 = \left[0, \frac{1}{9}\right] \cup \left[\frac{2}{9}, \frac{1}{3}\right] \cup \left[\frac{2}{3}, \frac{7}{9}\right] \cup \left[\frac{8}{9}, 1\right].

12\frac{1}{2} belongs to C0C_0 but not to C1C_1. 16\frac{1}{6} belongs through C1C_1 and drops out at C2C_2.

A number with numerator 0 lies in every set: take a=0a = 0 and every ai=0a_i = 0 in the definition.

If ab\frac{a}{b} is greater than 1, it already fails at C0C_0, so the answer is 0.