#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; } const int h=(1<<12)*7*17; const int sz=((p-1)/h); int g[1024][2]; int sum[1024][2]; int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int t;cin>>t;while(t--) { int a,n;cin>>a>>n;int u2=po(a,h);int cur=1; int pos[sz];for(int i=0;i=n) continue; int o=(n-1-i)/h+1; if(o==(n-1)/h+1) { sum[i%1024][0]+=cur1;if(sum[i%1024][0]>=p) sum[i%1024][0]-=p; } else { sum[i%1024][1]+=cur1;if(sum[i%1024][1]>=p) sum[i%1024][1]-=p; } cur1*=cur2;cur1%=p;cur2*=a2;cur2%=p; } int res=0; for(int i=0;i<1024;++i) { for(int j=0;j<2;++j) { res+=g[i][j]*sum[i][j]; res%=p; } } cout<<(res%p+p)%p<<'\n'; } return 0; }