PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000015. Tổ tiên trên cây Fibonacci (Fibonacci)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổ tiên trên cây Fibonacci (Fibonacci)

Fibonacci

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

A breeder observes that rabbit pairs reproduce according to the Fibonacci model: starting in their second month, each pair gives birth to one new pair at the beginning of every month, and no pair ever disappears.

Rabbit pairs are numbered in order of birth, starting from 11. When several pairs are born simultaneously, the offspring of the pair with a smaller label receives the smaller label. All pairs descend from pair 11, forming a rooted family tree.

For each pair of labels (a,b)(a,b), find their lowest common ancestor. Every pair is considered an ancestor of itself.

Input

  • The first line contains the number of queries mm.
  • Each of the next mm lines contains positive integers a,ba,b.

Output

Print the lowest common ancestor label on one line per query.

Subtasks

  • Subtask 1 (20 points): m≤30m\le30; a,b≤100a,b\le100; 1≤m≤30001\le m\le3000; 1≤a,b≤10121\le a,b\le10^{12}.
  • Subtask 2 (30 points): m≤1000m\le1000; a,b≤106a,b\le10^6; 1≤m≤30001\le m\le3000; 1≤a,b≤10121\le a,b\le10^{12}.
  • Subtask 3 (50 points): 1≤m≤30001\le m\le3000; 1≤a,b≤10121\le a,b\le10^{12}.

Examples

Input

3
5 7
1 2
6 6

Output

2
1
6

Explanation

For (5,7)(5,7), the parent chain of 77 reaches 22 and the chain of 55 also reaches 22; their lowest common ancestor is 22. Identical vertices have themselves as LCA.