Starting with any positive integer N, we define the Collatz sequence corresponding to N as the numbers formed by the following operations:

`N` → `N`/2 ( if `N` is even)

`N` → 3`N` + 1 (if `N` is odd)

i.e. If *N* is even, divide it by 2 to get *N* / 2. If *N* is odd, multiply it by 3 and add 1 to obtain 3*N* + 1.

It is conjectured but not yet proven that no matter which positive integer we start with; we always end up with 1.` `

For example, 10 → 5 → 16 → 8 → 4 → 2 → 1

**Note**: The sequence should end at the 1st occurence of integer 1.

The length of the Collatz sequence for some given N is defined as the number of numbers in the sequence starting with N and ending at 1.

Given a positive integer N, the task is to print the maximum Collatz sequence length among all numbers from 1 to N (both included).

**Input: **

The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line containing a positive integer N.

**Output:**

Corresponding to each test case, in a new line, print the maximum Collatz sequence length among all numbers from 1 to N (both included).

**Constraints:**

1 ≤ T ≤ 100

1 ≤ N ≤ 1000000

**Example:**

**Input**

2

3

20

**Output**

8

21

**Explanation:**

__For the 1st test case where N = 3__

For N= 3 we need to check sequence length when sequence starts with 1,2, and 3.

when sequence starts with 1, it's : 1 length = 1

when sequence starts with 2, it's : 2->1, length = 2

when sequence starts with 3, it's : 3->10->5->16->8->4->2->1, length = 8, which is max of all.

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

parass321 | 46 |

YogeshSharma7479 | 46 |

ayushwagdre1 | 44 |

yashranjan74 | 42 |

geekscoder | 41 |

Pulkit__Sharma__ | 806 |

Anirban166 | 682 |

Found_me | 636 |

abducodes | 607 |

Sathvik Avasarala | 526 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.