#include using namespace std; #define int long long const int p=998244353; int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*u)%p;} else {int u=po(a,b-1);return (a*u)%p;}} int inv(int x) {return po(x,p-2);} int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int h=(1<<12)*7*17; int t;cin>>t;while(t--) { int a,n;cin>>a>>n; int res=0; 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;int u=po(a,step); if(u!=1) {int inva=inv(u-1);res+=(po(a,r)-po(a,l))*inva;res%=p;} else {res+=po(a,l)*o;res%=p;} } cout<<(res%p+p)%p<<'\n'; } return 0; }