#include using namespace std; const int INF = 1000000000; const long long MOD = 998244353; struct unionfind{ vector p, t; unionfind(int N): p(N, -1), t(N, INF){ } int root(int x){ if (p[x] < 0){ return x; } else { return root(p[x]); } } bool same(int x, int y){ return root(x) == root(y); } int size(int v){ return -p[root(v)]; } void unite(int x, int y, int w){ x = root(x); y = root(y); if (p[x] < p[y]){ swap(x, y); } p[y] += p[x]; p[x] = y; t[x] = w; } }; int main(){ int N, X, Q; cin >> N >> X >> Q; unionfind UF(N); vector ans(N, 0); for (int i = 0; i < Q; i++){ int t; cin >> t; if (t == 1){ int v, w; cin >> v >> w; int u = X; u = UF.root(u); v = UF.root(v); if (u != v){ long long res = ans[u] + ans[v] + (long long) UF.size(u) * UF.size(v) % MOD * w; res %= MOD; UF.unite(u, v, w); ans[UF.root(u)] = res; } } if (t == 2){ int u, v; cin >> u >> v; if (!UF.same(u, v)){ cout << -1 << endl; } else { int ans; while (u != v){ if (UF.t[u] > UF.t[v]){ swap(u, v); } ans = UF.t[u]; u = UF.p[u]; } cout << ans << endl; X += ans; X %= N; } } if (t == 3){ int v; cin >> v; cout << ans[UF.root(v)] << endl; } if (t == 4){ int value; cin >> value; X += value; X %= N; } } }