#include #include #include using namespace std; typedef long long ll; ll pw(ll a,ll x,ll mod){ ll ret = 1; while(x){ if(x&1) (ret *= a) %= mod; (a *= a) %= mod; x /= 2; } return ret; } vector di[100010] = {}; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for(int i=2;i<=100000;i++){ for(int j=2*i;j<=100000;j+=i){ di[j].push_back(i); } } while(t){ t--; int i,j,x,v; cin >> v >> x; if(v*x==1){ cout << 1 << endl; continue; } int r = -1,p = x*v + 1; for(i=2;i<=x*v;i++){ bool f = true; for(int num: di[p - 1]){ if(pw(i,num,p)==1) f = false; } if(f){ r = i; break; } } vector ans; for(i=1;i<=x;i++) ans.push_back(pw(r,i*v,p)); sort(ans.begin(),ans.end()); for(i=0;i