from collections import defaultdict import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) def extgcd(a, b): """ 拡張Euclidの互除法 INPUT: a, b OUTPUT: d: gcd(a, b) (x, y): ax + by = d の解 """ if b == 0: return a, (1, 0) d, (y, x) = extgcd(b, a % b) y -= a // b * x return d, (x, y) def crt(B, M): """ 中国剰余定理 x ≡ b0 (mod M0) x ≡ b1 (mod M1) x ≡ b2 (mod M2) となるxを求める INPUT: B:[b0,b1,..] M:[m0,m1,..] OUTPUT: (x, mod) -> x:最小の答え(0<=x 解がない場合 """ x = 0 mod = 1 for b, m in zip(B, M): d, (p, _) = extgcd(mod, m) if (b - x) % d != 0: return 0, -1 x += (b - x) // d * p % (m // d) * mod mod *= m // d return x % mod, mod def invmod(a, m): """ mを法とするaの逆元、つまり ab = 1 (mod m)となるbを返す """ g, (x, y) = extgcd(a, m) return x % m def euler_phi(N): """ Euler totient function [1,N]の自然数のうちNと互いに素なものの個数 """ res = N for i in range(2, int(N ** 0.5) + 1): if N % i == 0: res -= res // i while N % i == 0: N //= i if N != 1: res -= res // i return res def euler_phi_table(N): res = list(range(N)) for i in range(2, N): if res[i] == i: for j in range(i, N, i): res[j] -= res[j] // i return res def get_generator(p): """素数pの最小の原始根を返す""" fact = [] phi = p - 1 n = phi for i in range(2, int(n ** 0.5) + 1): if n % i == 0: fact.append(i) while n % i == 0: n //= i if n > 1: fact.append(n) for res in range(2, p + 1): ok = True for f in fact: ok &= pow(res, phi // f, p) != 1 if not ok: break if ok: return res return -1 def main(): T = int(input()) mod = 10 ** 9 + 7 for _ in range(T): X, K = map(int, input().split()) _, (a, b) = extgcd(mod - 1, -K) N = pow(X, b, mod) print(N) main()