/** author: shobonvip created: 2026.03.20 21:21:14 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); const int mx = 10000050; vector p(mx + 1, true); p[0] = false; p[1] = false; for (int i=2; i<=mx; i++) { if (!p[i]) continue; for (int j=2*i; j<=mx; j+=i) { p[j] = false; } } vector tb; vector pl; for (int i=2; i<=mx; i++) { if (p[i]) pl.emplace_back(i); } for (int i=5; i<=mx-2; i++) { if (p[i] && p[i+2] && i%4==1) { tb.emplace_back(i+2); } } int t; cin >> t; while(t--) { int n, m; cin >> n >> m; if (n == 1) { int L = int(upper_bound(all(pl), m) - pl.begin()); cout << L << '\n'; continue; } int L = int(upper_bound(all(tb), m) - tb.begin()); vector mat(2, vector(2)); mat[0][0] = 0; mat[1][0] = 2 * L; mat[1][1] = 1; mat[0][1] = 1; vector ans(2); ans[0] = 1; ans[1] = 2 * L; n--; while(n > 0) { if (n & 1) { vector nans(2); rep(i,0,2) { rep(j,0,2) { nans[i] += mat[i][j] * ans[j]; } } swap(ans, nans); } vector nmat(2, vector(2)); rep(i,0,2) { rep(j,0,2) { rep(k,0,2) { nmat[i][j] += mat[i][k] * mat[k][j]; } } } swap(nmat, mat); n >>= 1; } cout << (ans[0] + ans[1]).val() << '\n'; } }