Gopal and his cycles

3.5

8 votes
Trees, Easy
Problem

Gopal is teaching data structures to his students. He told the students that in a tree adding one more edge will definitely form a cycle. After the class a student went to Gopal and asked the length of the cycle formed by adding an edge in a tree. But Gopal doesn't know the answer to the problem. He needs a day to answer them. He needs your help in finding the solution. The problem is as follows.

Gopal has an infinite binary tree. Now he joins two nodes A and B. He wants to know the length of the cycle formed. The tree is numbered as follows.

root -> 1
left child -> 2 x parent
right child -> 2 x parent + 1
You need to help him by telling him the length of the cycle formed.

Note: The line joining A and B will not cross any other node.

Input:
First line of the input contains T denoting the number of test cases. Then T lines follow each of the line containing two integers A and B.

Output:
output the required answer in a new line for each test case

Constraints;
1<=A, B<=1018

Sample Input:

3
2 3
2 7
5 15
Sample Output:
3
4
6
Explaination
Explaination for sample input 1 Explaination for sample input 2

Sample Input
3
2 3
9 10
5 15
Sample Output
3
5
6
Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?