from sys import stdin class UnionFind(): def __init__(self, n): self.parent_or_size = [-1] * n self.weight = [0] * n self.ans = [0] * n self.cnt = [[[0] * n for i in range(30)] for j in range(2)] for i in range(30): for j in range(n): self.cnt[0][i][j] = 1 def leader(self, x): if self.parent_or_size[x] < 0: return x root = self.leader(self.parent_or_size[x]) self.weight[x] ^= self.weight[self.parent_or_size[x]] self.parent_or_size[x] = root return self.parent_or_size[x] def merge(self, x, y, w): rx, ry = self.leader(x), self.leader(y) w ^= self.weight[x] ^ self.weight[y] if self.parent_or_size[rx] > self.parent_or_size[ry]: rx, ry = ry, rx self.parent_or_size[rx] += self.parent_or_size[ry] self.parent_or_size[ry] = rx self.weight[ry] = w self.ans[rx] = 0 for i in range(30): self.cnt[((w >> i) & 1)][i][rx] += self.cnt[0][i][ry] self.cnt[1 ^ ((w >> i) & 1)][i][rx] += self.cnt[1][i][ry] self.ans[rx] += ((self.cnt[0][i][rx] * self.cnt[1][i][rx]) % 998244353) << i self.ans[rx] %= 998244353 def dist(self, x, y): if self.leader(x) != self.leader(y): return -1 return self.weight[x] ^ self.weight[y] N, X, Q = map(int, stdin.readline().split()) uf = UnionFind(N) for _ in range(Q): query = list(map(int, stdin.readline().split())) if query[0] == 1: uf.merge(X, query[1], query[2]) elif query[0] == 2: d = uf.dist(query[1], query[2]) print(d) if d != -1: X += d X %= N elif query[0] == 3: print(uf.ans[uf.leader(query[1])]) else: X += query[1] X %= N