This page is still under construction.

Parts of this page are still being built. What you see may change.

That set is a harmful set

Time limit3sMemory limit256 MB

Summary
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.
Level

Medium5 of 10

Topics
Math, Simulation
Solved
No attempts yet

Problem

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

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

Cn={ a+∑i=1nai3i  |  0≤a≤13n,  ai∈{0,2} }(n∈N)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=0∞CnC = \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. (0≤a≤1000000 \le a \le 100000, 1≤b≤1000001 \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.

Examples3

  1. Example 1

    Input
    4
    0 1
    1 2
    1 6
    5 18
    
    Expected output
    -1
    1
    2
    3
    
  2. Example 2

    Input
    9
    1 3
    2 3
    1 9
    2 9
    7 9
    8 9
    1 1
    5 5
    0 7
    
    Expected output
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    
  3. Example 3

    Input
    5
    2 1
    5 4
    100000 1
    100000 99999
    3 2
    
    Expected output
    0
    0
    0
    0
    0