#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <random>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << x << '\n'
#define lowbit(x) (x & -x)
#define debug puts("qwq");
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
template <typename T = int>
inline T read() {
T s = 0, w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
return s * w;
}
template <typename T>
inline void read(T &s) {
s = 0;
int w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
s = s * w;
}
template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
read(x), read(arp...);
}
template <typename T>
inline void write(T x, char ch = '\n') {
if (x < 0) x = -x, putchar('-');
static char stk[25];
int top = 0;
do {
stk[top++] = x % 10 + '0', x /= 10;
} while (x);
while (top) putchar(stk[--top]);
putchar(ch);
return;
}
template <typename T>
inline void smax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void smin(T &x, T y) {
if (x > y) x = y;
}
void quit() {
exit(0);
}
} using namespace FastIO;
const int N = 1e6 + 19, mod = 1e9 + 7;
int ksm(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % mod;
b >>= 1; a = 1LL * a * a % mod;
} return res;
}
int isprime[N], prime[N], mu[N], len, f[N], g[N], h[N];
void init() {
f[0] = g[0] = f[1] = g[1] = 1;
mu[1] = 1;
for (int i = 2; i < N; ++i) {
f[i] = (f[i-1] + f[i-2]) % mod;
g[i] = ksm(f[i], mod-2);
h[i] = 1;
if (!isprime[i]) {
prime[++len] = i;
mu[i] = -1;
}
for (int j = 1; j <= len && prime[j] * i < N; ++i) {
isprime[i * prime[j]] = 1;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
for (int i = 1; i < N; ++i) mu[i] += mu[i-1];
for (int i = 1; i < N; ++i) {
if (mu[i]) {
for (int j = i; j < N; j += i) {
h[j] = 1LL * h[j] * (mu[i] > 0 ? f[j / i] : g[j / i]) % mod;
}
}
}
for (int i = 2; i < N; ++i) h[i] = 1LL * h[i] * h[i-1] % mod;
}
signed main() {
int t = read();
while (t--) {
int n, m; read(n, m);
if (n > m) swap(n, m);
int ans = 1;
for (int d = 1, nxt; d <= n; d = nxt + 1) {
nxt = min(n / (n / d), m / (m / d));
int inv = 1LL * h[nxt] * ksm(h[d-1], mod - 2) % mod;
ans = 1LL * ans * ksm(inv, (n / d) * (m / d) % (mod - 1)) % mod;
} write((ans + mod) % mod);
}
return 0;
}