#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>
#define min(a, b) ((a) < (b) ? (a) : (b))
const int MAX_N = 1e6;
const int MOD = 1e9 + 7;
int primes[MAX_N + 1], mu[MAX_N + 1], mus[MAX_N + 1], cnt;
bool isNotPrime[MAX_N + 1];
inline void sieve() {
isNotPrime[0] = isNotPrime[1] = true;
mu[1] = 1;
for (int i = 2; i <= MAX_N; i++) {
if (!isNotPrime[i]) {
primes[cnt++] = i;
mu[i] = -1;
}
for (int j = 0; j < cnt && (long long)i * primes[j] <= MAX_N; j++) {
int p = primes[j];
isNotPrime[i * p] = true;
if (i % p == 0) {
mu[i * p] = 0;
break;
} else {
mu[i * p] = -mu[i];
}
}
}
for (int i = 1; i <= MAX_N; i++) mus[i] = mus[i - 1] + mu[i];
}
inline void exgcd(long long a, long long b, long long &g, long long &x, long long &y) {
if (!b) g = a, x = 1, y = 0;
else exgcd(b, a % b, g, y, x), y -= x * (a / b);
}
inline long long inv(long long x) {
long long r, g, tmp;
exgcd(x, MOD, g, r, tmp);
return (r % MOD + MOD) % MOD;
}
long long fib[MAX_N + 1], fibProd[MAX_N + 1], fibProdInv[MAX_N + 1];
inline void prepare() {
fib[0] = 0, fib[1] = 1;
for (int i = 2; i <= MAX_N; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
fibProd[0] = fibProdInv[0] = 1;
for (int i = 1; i <= MAX_N; i++) {
fibProd[i] = fibProd[i - 1] * fib[i] % MOD;
fibProdInv[i] = inv(fibProd[i]);
}
}
inline long long g(int N, int M) {
long long res = 0;
for (int l = 1, r; l <= N; l = r + 1) {
r = min(N / (N / l), M / (M / l));
res += (long long)(mus[r] - mus[l - 1]) * (N / l) * (M / l);
}
return res;
}
inline int pow(long long a, long long n) {
long long res = 1;
for (; n; n >>= 1, a = a * a % MOD) if (n & 1) res = res * a % MOD;
return res;
}
inline int calc(int N, int M, int l, int r) {
return pow(fibProd[r] * fibProdInv[l - 1] % MOD, g(N, M));
}
inline int solve(int n, int m) {
if (n > m) std::swap(n, m);
long long res = 1;
for (int l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
(res *= calc(n / l, m / l, l, r)) %= MOD;
}
return res;
}
int main() {
freopen("product.in", "r", stdin);
freopen("product.out", "w", stdout);
int T_T;
scanf("%d", &T_T);
sieve();
prepare();
while (T_T--) {
int n, m;
scanf("%d %d", &n, &m);
printf("%d\n", solve(n, m));
}
}
1. 1. 1. 1.