#include using namespace std; #define int long long const int p=998244353; int po(int a,int b){ int x = a, res = 1; int h = b; while (h > 0) { if (h & 1) {res *= x;res%=p;} x *= x;x%=p; h >>= 1; } return res; } int inverse(int a,int m) { int u = 0, v = 1; while (a != 0) { int t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } return u; } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int h=(1<<12)*7*17;int sz=((p-1)/h+1); int t;cin>>t;while(t--) { int a,n;cin>>a>>n; int invs[sz];for(int i=0;i=n) continue; int o=(n-1-i)/h+1; int l=i*i;int r=i*i+o*i*2*h; int step=2*i*h;if(i) {u*=u2;u%=p;} if(i==0 || (n-i-1)/h!=(n-i)/h) { t=po(a,o*i*2*h);u1=po(a,o*2*h); } else { t*=u1;t%=p; } if(u!=1) {int inva=inverse(u-1,p);int v=(cur1*t)%p;res+=(v-cur1)*inva;res%=p;} else {res+=cur1*o;res%=p;} cur1*=cur2;cur1%=p;cur1*=cur2;cur1%=p;cur1*=a;cur1%=p;cur2*=a;cur2%=p; } cout<<(res%p+p)%p<<'\n'; } return 0; }