This page is still under construction.

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

Pair Transformation

Time limit1sMemory limit256 MB

Summary
From (1,1), add one component to the other or swap them, and report the fewest steps that put N in the pair for each query.
Level

Medium7 of 10

Topics
Number theory, BFS, Brute force
Solved
No attempts yet

Problem

Given a pair (a,b)(a, b) of two positive integers, you may apply one of the following three transformations to build a new pair.

  • (a,b)→(a,a+b)(a, b) \to (a, a + b)
  • (a,b)→(a+b,b)(a, b) \to (a + b, b)
  • (a,b)→(b,a)(a, b) \to (b, a)

Start from the pair (1,1)(1, 1) and build a pair that holds NN. A pair holds NN when at least one of its two components equals NN. Find the fewest transformations that do it.

Input

The first line has the number of tests TT. (1≤T≤20)(1 \le T \le 20)

Each of the next TT lines has one integer NN. (1≤N≤106)(1 \le N \le 10^6)

Output

For each test, print the fewest transformations on its own line.

Hint

For N=1N = 1 the starting pair already holds 11, so no transformation is needed.

For N=3N = 3, two transformations are enough: (1,1)→(2,1)→(3,1)(1, 1) \to (2, 1) \to (3, 1).

For N=5N = 5, three are enough: (1,1)→(2,1)→(2,3)→(2,5)(1, 1) \to (2, 1) \to (2, 3) \to (2, 5).

For N=7N = 7, four are enough: (1,1)→(2,1)→(2,3)→(2,5)→(2,7)(1, 1) \to (2, 1) \to (2, 3) \to (2, 5) \to (2, 7).

Examples4

  1. Example 1

    Input
    4
    1
    3
    5
    7
    
    Expected output
    0
    2
    3
    4
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    3
    2
    4
    6
    
    Expected output
    1
    3
    5
    
  4. Example 4

    Input
    12
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    Expected output
    0
    1
    2
    3
    3
    5
    4
    4
    5
    5
    5
    5