#include #include using namespace std; using mint = atcoder::modint998244353; int main(){ ios::sync_with_stdio(false); cin.tie(0); int N, X, Q; cin >> N >> X >> Q; vector parent_or_size(N, -1), difference_weight(N); vector, 30>> cnt(N); vector ans(N); function leader = [&](int v){ if (parent_or_size[v] < 0) return v; int root = leader(parent_or_size[v]); difference_weight[v] ^= difference_weight[parent_or_size[v]]; return parent_or_size[v] = root; }; auto merge = [&](int u, int v, int w){ int x = leader(u), y = leader(v); w ^= difference_weight[u], w ^= difference_weight[v]; if (-parent_or_size[x] < -parent_or_size[y]) swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; ans[x] = 0; for(int i = 0; i < 30; i++){ for(int j = 0; j < 2; j++){ cnt[x][i][j ^ (w >> i & 1)] += cnt[y][i][j]; } ans[x] += cnt[x][i][0] * cnt[x][i][1] * (1 << i); } difference_weight[y] = w; }; auto distance = [&](int u, int v){ int x = leader(u), y = leader(v); if(x != y) return -1; return difference_weight[v] ^ difference_weight[u]; }; for(int i = 0; i < N; i++){ for(int j = 0; j < 30; j++) cnt[i][j][0]++; } while(Q--){ int type, u, v, w; cin >> type; if(type == 1){ cin >> v >> w; merge(v, X, w); }else if(type == 2){ cin >> u >> v; v = distance(u, v); cout << v << '\n'; if(v != -1) (X += v) %= N; }else if(type == 3){ cin >> v; cout << ans[leader(v)].val() << '\n'; }else{ cin >> v; (X += v) %= N; } } }