#include using namespace std; #if __has_include() #include using namespace atcoder; using mint = modint998244353; // using mint = modint1000000007; // using mint = double; #endif //define using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using vi = vector; using vll = vector; #define rep(i,l,r) for (int i = (int)(l); i < (int)(r); i++) #define rrep(i,l,r) for (int i = (int)(r-1); i >= (int)(l); i--) #define len(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define elif else if #define pb push_back #define eb emplace_back #define fi first #define se second const int inf = 1e9; const long long infl = 1LL<<60; const int mod = 998244353; ll pow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } template bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; } template bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; } template using spq = priority_queue, greater>; templateistream& operator>>(istream& i, vector& v) {for(int j = 0; j < (int)(v).size(); j++) i >> v[j]; return i;} struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); } } iosetup; #include using namespace std; using namespace boost::multiprecision; using Int = cpp_int; Int extgcd(Int a, Int b, Int &x, Int &y) { Int r0 = a, r1 = b; Int x0 = 1, x1 = 0; Int y0 = 0, y1 = 1; while (r1 != 0) { Int q = r0 / r1; Int r_next = r0 % r1; Int x_next = x0 - q * x1; Int y_next = y0 - q * y1; r0 = r1; r1 = r_next; x0 = x1; x1 = x_next; y0 = y1; y1 = y_next; } x = x0; y = y0; return r0; } pair crt(Int r1, Int m1, Int r2, Int m2) { Int p, q; Int g = extgcd(m1, m2, p, q); if ((r2 - r1) % g != 0) { return {0, 0}; } Int lcm = (m1 / g) * m2; Int m2_div_g = m2 / g; Int tmp = (r2 - r1) / g; Int k = (tmp * p) % m2_div_g; Int r = (r1 + m1 * k) % lcm; if (r < 0) r += lcm; return {r, lcm}; } int main() { int Q; cin >> Q; vector> st; st.pb({0, 1}); int idx = 1; rep(i, 0, Q) { int t; cin >> t; if (t == 1) { ll m, r; cin >> m >> r; const auto& [pr, pm] = st.back(); if (pm != 0) { pair res = crt(pr, pm, r, m); st.pb(move(res)); } else { st.eb(0, 0); } } else if (t == 2) { int k; cin >> k; idx -= k; st.resize(st.size() - k); } else { ll m; cin >> m; const auto& [nr, nm] = st.back(); if (nm != 0) { cout << (nr % m) << "\n"; } else { cout << "-1\n"; } } } return 0; }