#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ fibc(long long *, long long);
long long gcd(long long, long long);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, long long *, ...);
int main() {
  long long int t, a, b, gc, i;
  long long int fib[1000000];
  for (i = 2; i < 1000000; i++)
    fib[i] = -1;
  fib[0] = 0;
  fib[1] = 1;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld %lld", &a, &b);
    gc = gcd(a, b);
    if (fib[gc] == -1) {
      fibc(fib, gc);
    }
    printf("%lld\n", fib[gc] % (1000000007));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7a1230,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x2,-0x30(%rbp)
cmpq   $0xf4240,-0x30(%rbp)
jge    1189 <main+0x49>
mov    -0x30(%rbp),%rax
movq   $0xffffffffffffffff,-0x7a1230(%rbp,%rax,8)
ff ff ff ff ff
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    115a <main+0x1a>
movq   $0x0,-0x7a1230(%rbp)
movq   $0x1,-0x7a1228(%rbp)
lea    0xe5f(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
je     123e <main+0xfe>
lea    0xe2f(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <gcd@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0xffffffffffffffff,-0x7a1230(%rbp,%rax,8)
ff ff
jne    1214 <main+0xd4>
lea    -0x7a1230(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <fibc@plt>
mov    -0x28(%rbp),%rax
mov    -0x7a1230(%rbp,%rax,8),%rax
ff
mov    $0x3b9aca07,%ecx
cqto
idiv   %rcx
mov    %rdx,%rsi
lea    0xdd6(%rip),%rdi
call   1030 <printf@plt>
jmp    11b1 <main+0x71>
xor    %eax,%eax
add    $0x7a1230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x7a1228,%rsp
lea    0x30(%rsp),%rdi
mov    $0x7a11f0,%edx
mov    $0xff,%esi
call   1040 <memset@plt>
movaps 0xe84(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe8d(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
subq   $0x1,0x8(%rsp)
jae    11b0 <main+0x60>
xor    %eax,%eax
add    $0x7a1228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe59(%rip),%r14
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%r13
movabs $0x89705f3112a28fe5,%r12
5f 70 89
lea    0xe48(%rip),%rbx
jmp    1211 <main+0xc1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rsi,%rax
imul   %r12
add    %rsi,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1d,%rdx
add    %rax,%rdx
imul   $0x3b9aca07,%rdx,%rax
sub    %rax,%rsi
mov    %rbx,%rdi
call   1030 <printf@plt>
subq   $0x1,0x8(%rsp)
jb     119c <main+0x4c>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <scanf@plt>
mov    0x18(%rsp),%rdi
mov    0x10(%rsp),%rsi
call   1070 <gcd@plt>
mov    0x20(%rsp,%rax,8),%rsi
cmp    $0xffffffffffffffff,%rsi
jne    11e0 <main+0x90>
mov    %rax,%rbp
lea    0x20(%rsp),%rdi
mov    %rax,%rsi
call   1050 <fibc@plt>
mov    0x20(%rsp,%rbp,8),%rsi
jmp    11e0 <main+0x90>

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [10 x i8] c"%lld %lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca [1000000 x i64]
store i32 0, i32* %1
store i64 2, i64* %6
br label %8
8: ; preds = %14, %0
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 1000000
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i64, i64* %6
%13 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %7, i64 0, i64 %12
store i64 -1, i64* %13
br label %14
14: ; preds = %11
%15 = load i64, i64* %6
%16 = add nsw i64 %15, 1
store i64 %16, i64* %6
br label %8
17: ; preds = %8
%18 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %7, i64 0, i64 0
store i64 0, i64* %18
%19 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %7, i64 0, i64 1
store i64 1, i64* %19
%20 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
br label %21
21: ; preds = %38, %17
%22 = load i64, i64* %2
%23 = add nsw i64 %22, -1
store i64 %23, i64* %2
%24 = icmp ne i64 %22, 0
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %3, i64* %4)
%27 = load i64, i64* %3
%28 = load i64, i64* %4
%29 = call i64 @gcd(i64 %27, i64 %28)
store i64 %29, i64* %5
%30 = load i64, i64* %5
%31 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %7, i64 0, i64 %30
%32 = load i64, i64* %31
%33 = icmp eq i64 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %7, i64 0, i64 0
%36 = load i64, i64* %5
%37 = call i32 @fibc(i64* %35, i64 %36)
br label %38
38: ; preds = %34, %25
%39 = load i64, i64* %5
%40 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %7, i64 0, i64 %39
%41 = load i64, i64* %40
%42 = srem i64 %41, 1000000007
%43 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %42)
br label %21
44: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [10 x i8] c"%lld %lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca [1000000 x i64]
%5 = bitcast i64* %1 to i8*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast [1000000 x i64]* %4 to i8*
%9 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %4, i64 0, i64 2
%10 = bitcast i64* %9 to i8*
%11 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %4, i64 0, i64 0
%12 = bitcast [1000000 x i64]* %4 to <2 x i64>*
store <2 x i64> <i64 0, i64 1>, <2 x i64>* %12
%13 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %1)
%14 = load i64, i64* %1
%15 = add nsw i64 %14, -1
store i64 %15, i64* %1
%16 = icmp eq i64 %14, 0
br i1 %16, label %35, label %17
17: ; preds = %0, %28
%18 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3)
%19 = load i64, i64* %2
%20 = load i64, i64* %3
%21 = call i64 @gcd(i64 %19, i64 %20)
%22 = getelementptr inbounds [1000000 x i64], [1000000 x i64]* %4, i64 0, i64 %21
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @fibc(i64* %11, i64 %21)
%27 = load i64, i64* %22
br label %28
28: ; preds = %25, %17
%29 = phi i64 [ %27, %25 ], [ %23, %17 ]
%30 = srem i64 %29, 1000000007
%31 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %30)
%32 = load i64, i64* %1
%33 = add nsw i64 %32, -1
store i64 %33, i64* %1
%34 = icmp eq i64 %32, 0
br i1 %34, label %35, label %17
35: ; preds = %28, %0
ret i32 0
}