#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } template struct static_modint { using mint = static_modint; int x; static_modint() : x(0) {} static_modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} mint& operator+=(const mint& rhs) { if ((x += rhs.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint& rhs) { if ((x += mod - rhs.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint& rhs) { x = (int) (1LL * x * rhs.x % mod); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint pow(long long n) const { mint _x = *this, r = 1; while (n) { if (n & 1) r *= _x; _x *= _x; n >>= 1; } return r; } mint inv() const { return pow(mod - 2); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs.x == rhs.x; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs.x != rhs.x; } friend ostream &operator<<(ostream &os, const mint &p) { return os << p.x; } friend istream &operator>>(istream &is, mint &a) { int64_t t; is >> t; a = static_modint(t); return (is); } }; const unsigned int mod = 998244353; using modint = static_modint; modint mod_pow(ll n, ll x) { return modint(n).pow(x); } modint mod_pow(modint n, ll x) { return n.pow(x); } template struct Comination { vector p, invp; Comination(int sz) : p(sz+1), invp(sz+1) { p[0] = 1; for (int i = 1; i <= sz; ++i) { p[i] = p[i-1] * i; } invp[sz] = p[sz].inv(); for (int i = sz-1; i >= 0; --i) { invp[i] = invp[i+1] * (i+1); } } T comb(int n, int r) { if (r < 0 or n < r) return 0; return p[n]*invp[n-r]*invp[r]; } T big_comb(T n, int r) { T res = invp[r]; for (int i = 0; i < r; ++i) { res *= (n-i); } return res; } }; using Comb = Comination; Comb p(1<<20); int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int q; cin >> q; while (q--) { ll n,k; cin >> n >> k; // x+y = n // (x+1)y = k // (x+1)(n-x) = k // -x^2+(n-1)x+n-k = 0 // x^2-(n-1)x+k-n = 0 ll b = -(n-1), c = k-n; ll d = b*b - 4*c; auto sq = [](ll x) -> ll { ll s = sqrt(abs(x)); for (int i = -2; i <= 2; ++i) { if ((s+i)*(s+i) == x) return abs(s+i); } return 0; }; if (d < 0) { cout << 0 << "\n"; continue; } ll s = sq(d); if (s*s != d) { cout << 0 << "\n"; continue; } ll x1 = (-b+s)/2; ll x2 = (-b-s)/2; modint res = 0; if (x1 >= 0 and (x1+1)*(n-x1) == k) { res += p.comb(n, x1); } if (x2 >= 0 and x2 != x1 and (x2+1)*(n-x2) == k) { res += p.comb(n, x2); } cout << res << "\n"; } }