#include using namespace std; //#pragma GCC optimize("Ofast") #define rep(i,n) for(ll i=0;i=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue,greater> #define all(x) (x).begin(),(x).end() #define CST(x) cout<; using vvl=vector>; using pl=pair; using vpl=vector; using vvpl=vector; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={1,0,-1,0,1,1,-1,-1}; const ll dx[8]={0,1,0,-1,1,-1,1,-1}; template inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } vl ans(300010,-1); vector meet; struct UnionFind { vector par; vector edge; vector fs; vector> com; UnionFind(int n) : par(n, -1),edge(n, 0) { fs.resize(n);com.resize(n); rep(i,n)com[i].insert(i); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } bool merge(int x, int y,ll w) { x = root(x); y = root(y); if (x == y) { edge[x]++; return false; } if (par[x] > par[y]) { swap(x, y); } ll p=size(x),q=size(y); par[x] += par[y]; par[y] = x; edge[x] += edge[y]+1; p*=q;p%=MOD9;p*=w; fs[x]=(fs[x]+fs[y])%MOD9; fs[x]=(fs[x]+p)%MOD9; if(com[x].size()> n >> x >> q;meet.resize(n); UnionFind uf(n); vvl qs(q); ll idx=0; //return 0; rep(i,q){ ll t;cin >> t;qs[i].emplace_back(t); if(t==1){ ll v,w;cin >> v >> w;qs[i].emplace_back(v);qs[i].emplace_back(w); } else if(t==2){ ll u,v;cin >> u >> v; meet[u].push_back({v,idx}); meet[v].push_back({u,idx}); if(u==v)ans[idx]=0; qs[i].emplace_back(idx);idx++; } else if(t==3){ ll v;cin >> v;qs[i].emplace_back(v); } else{ ll v;cin >> v;qs[i].emplace_back(v); } } //return 0; rep(i,q){ ll t=qs[i][0]; if(t==1){ uf.merge(qs[i][1],x,qs[i][2]); } else if(t==2){ ll f=ans[qs[i][1]]; cout << f << endl; if(f!=-1)x+=f;x%=n; } else if(t==3){ cout << uf.query(qs[i][1]) << endl; } else{ ll v=qs[i][1];x+=v;x%=n; } /*rep(i,n){ for(auto p:uf.sts[i])cout << p.first <<" " << p.second <<" ";cout << endl; }*/ } }