This page is still under construction.

Parts of this page are still being built. What you see may change.

GCD of Fibonacci Numbers

Time limit1sMemory limit256 MB

Summary
Compute the greatest common divisor of the Nth and Mth Fibonacci numbers and print it modulo 1000000007.
Level

Medium5 of 10

Topics
Number theory, Math, Divide and conquer
Solved
No attempts yet

Problem

The Fibonacci sequence is defined by F0=0F_0 = 0, F1=1F_1 = 1, and Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2} for n≥2n \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−(1−52)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+2−1,Fn∣Fkn(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. (1≤T≤10001 \le T \le 1000)

Each of the next TT lines contains two integers NN and MM separated by one space. (0<N,M≤1090 < 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.

Examples1

  1. Example 1

    Input
    2
    7 10
    6 12
    
    Expected output
    1
    8