#include // #include using namespace std; // using namespace atcoder; using ll = long long; using ull = unsigned long long; using P = pair; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 ll ext_gcd(ll a,ll b,ll &x,ll &y){ if(b == 0){ x = 1,y = 0; return a; } ll d = ext_gcd(b,a%b,x,y); ll z = x-(a/b)*y; x = y,y = z; return d; } ll modpow(ll n,ll r){ if(r < 0)return 1; ll res = 1; while(r){ if(r & 1)res = res*n%MOD; n = n*n%MOD; r >>= 1; } return res; } int main(){ int T; cin >> T; while(T--){ ll x,k;cin >> x >> k; ll a,b; ext_gcd(MOD-1,k,a,b); cout << modpow(x,b+MOD-1) << "\n"; } return 0; }