#include using namespace std; using Int = long long; const char newl = '\n'; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a void drop(const T &x){cout< vector read(size_t n){ vector ts(n); for(size_t i=0;i>ts[i]; return ts; } // find x s.t. a^x = b (x >= 0) // return MOD if not found // MOD can be composite numbers template T mod_log(T a,T b,const T MOD){ using ll = long long; ll g=1; { ll m=MOD; while(m){ g=(ll)g*a%MOD; m>>=1; } } g=__gcd(g,(ll)MOD); ll c=0,t=1; while(t%g){ if(t==b) return c; t=t*a%MOD; c++; } // not found if(b%g) return MOD; t/=g;b/=g; const ll n=MOD/g; ll h=0,gs=1; while(h*h bs; { ll s=0,e=b; while(s T mod_pow(T a,long long n,const T MOD){ using ll = long long; T res(1); while(n){ if(n&1) res=(ll)res*a%MOD; a=(ll)a*a%MOD; n>>=1; } return res; } // mod can be composite numbers template T order(T x,const T MOD){ static map> dp; static map phi; vector &ps=dp[MOD]; if(ps.empty()){ T res=MOD,n=MOD; for(T i=2;i*i<=n;i++){ if(n%i) continue; while(n%i==0) n/=i; res=res/i*(i-1); } if(n!=1) res=res/n*(n-1); phi[MOD]=res; for(T i=2;i*i<=res;i++){ if(res%i) continue; while(res%i==0) res/=i; ps.emplace_back(i); } if(res!=1) ps.emplace_back(res); } T res=phi[MOD]; for(T p:ps){ while(res%p==0){ if(mod_pow(x,res/p,MOD)!=1) break; res/=p; } } return res; } //INSERT ABOVE HERE signed main(){ cin.tie(0); ios::sync_with_stdio(0); const Int MOD = 1e9+7; Int r=2; while(order(r,MOD)!=MOD-1) r++; Int T; cin>>T; while(T--){ Int X,K; cin>>X>>K; cout<