GCD of Fibonacci Numbers

No attempts yetTime limit1sMemory limit256 MB

Problem

The Fibonacci sequence is defined by F0=0F_0 = 0, F1=1F_1 = 1, and Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} for n2n \ge 2. It starts 0, 1, 1, 2, 3, 5, 8, 13, 21.

Solving the recurrence gives a closed form.

Fn=15{(1+52)n(152)n}F_n = \frac{1}{\sqrt{5}} \left\{ \left( \frac{1 + \sqrt{5}}{2} \right)^n - \left( \frac{1 - \sqrt{5}}{2} \right)^n \right\}

The sequence has many properties. Two of them are the following.

i=1nFi=Fn+21,FnFkn(k=0,1,2,)\sum_{i=1}^{n} F_i = F_{n+2} - 1, \qquad F_n \mid F_{kn} \quad (k = 0, 1, 2, \dots)

This problem asks for the greatest common divisor of two Fibonacci numbers. Given two integers NN and MM, compute gcd(FN,FM)\gcd(F_N, F_M). The value is huge, so print it modulo 109+710^9 + 7. Take the remainder after computing the greatest common divisor.

Input

The first line contains the number of test cases TT. (1T10001 \le T \le 1000)

Each of the next TT lines contains two integers NN and MM separated by one space. (0<N,M1090 < N, M \le 10^9)

Output

For each test case, print gcd(FN,FM)\gcd(F_N, F_M) modulo 109+710^9 + 7 on its own line.