#include using namespace std; template struct ModInt { int32_t value; ModInt() : value(0) {} ModInt(long long v) : value(v % MOD) { if (value < 0) value += MOD; } ModInt(int32_t v): value(v % MOD) { if (value < 0) value += MOD; } ModInt operator+=(ModInt m) { value += m.value; if (value >= MOD) value -= MOD; return value; } ModInt operator-=(ModInt m) { value -= m.value; if (value < 0) value += MOD; return value; } ModInt operator*=(ModInt m) { value = (value * 1LL * m.value) % MOD; return value; } ModInt power(long long exp) const { if (exp == 0) return 1; ModInt res = (exp & 1 ? value : 1); ModInt half = power(exp >> 1); return res * half * half; } ModInt operator/=(ModInt m) { return *this *= m.power(MOD - 2); } friend std::istream &operator>>(std::istream &is, ModInt &m) { is >> m.value; return is; } friend std::ostream &operator<<(std::ostream &os, const ModInt &m) { os << m.value; return os; } explicit operator int32_t() const { return value; } explicit operator long long() const { return value; } static int32_t mod() { return MOD; } }; templateModInt operator+(ModInt a, ModInt b) { return a += b; } templateModInt operator+(L a, ModInt b) { return ModInt(a) += b; } templateModInt operator+(ModInt a, R b) { return a += b; } templateModInt operator-(ModInt a, ModInt b) { return a -= b; } templateModInt operator-(L a, ModInt b) { return ModInt(a) -= b; } templateModInt operator-(ModInt a, R b) { return a -= b; } templateModInt operator*(ModInt a, ModInt b) { return a *= b; } templateModInt operator*(L a, ModInt b) { return ModInt(a) *= b; } templateModInt operator*(ModInt a, R b) { return a *= b; } templateModInt operator/(ModInt a, ModInt b) { return a /= b; } templateModInt operator/(L a, ModInt b) { return ModInt(a) /= b; } templateModInt operator/(ModInt a, R b) { return a /= b; } templatebool operator==(ModInt a, ModInt b) { return a.value == b.value; } templatebool operator==(L a, ModInt b) { return a == b.value; } templatebool operator==(ModInt a, R b) { return a.value == b; } templatebool operator!=(ModInt a, ModInt b) { return a.value != b.value; } templatebool operator!=(L a, ModInt b) { return a != b.value; } templatebool operator!=(ModInt a, R b) { return a.value != b; } using mint = ModInt<998244353>; //using mint = ModInt<(int32_t)(1e9 + 7)>; //237c16 const int h=(1<<12)*7*17; const int sz=((998244353-1)/h); mint g[1024][2]; mint sum[1024][2]; int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int t;cin>>t;while(t--) { mint a;int n;cin>>a>>n;mint u2=a.power(h);mint cur=1; mint 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; } else { sum[i%1024][1]+=cur1; } cur1*=cur2;cur2*=a2; } mint res=0; for(int i=0;i<1024;++i) { for(int j=0;j<2;++j) { res+=g[i][j]*sum[i][j]; } } cout<