QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 256 MB Points totaux : 100

#841. Binary Indexed Tree

Statistiques

The inhabitants of this world live in a city with an infinite number of houses. Each house has a unique positive integer identifier $n$. The streets of this city are constructed as follows: for any house $n$, there is exactly one street connecting it to a house with a larger identifier, which is $n + \mathrm{lowbit}(n)$.

The function $\mathrm{lowbit}(n)$ is defined as the value obtained by keeping only the lowest set bit in the binary representation of $n$. A convenient bitwise method to calculate this is lowbit(n) = n & -n.

It can be proven that any two houses can reach each other via these bidirectional roads. Your task is to process several queries to calculate the shortest path between two houses, where each edge traversed counts as one unit of length.

Input

The first line contains an integer $q$, representing the number of queries.

Each of the next $q$ lines contains two positive integers $x$ and $y$, representing the two houses in the query.

Output

Output $q$ lines, each containing an integer representing the length of the shortest path.

Examples

Input 1

2
1 3
2 4

Output 1

3
1

Subtasks

For $20\%$ of the data, it is guaranteed that $1 \leq q \leq 10$, $1 \le x,y \le 2^{3}$.

For $30\%$ of the data, it is guaranteed that $1 \leq q \leq 500$, $1 \le x,y \le 2^{9}$.

For $70\%$ of the data, it is guaranteed that $1 \leq q \leq 10^5$, $1 \le x,y \le 2^{20}$.

For $100\%$ of the data, it is guaranteed that $1 \leq q \leq 3 \times 10^5$, $1 \le x, y \le 2^{60}$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.