from typing import Any class WeightedUnionFind(): T = Any def __init__(self, n: int) -> None: self.num = n self.rs = [1]*n self.ps = list(range(n)) self.ws = [0]*n self.gs = [{i} for i in range(n)] def find(self, x: int) -> int: if x == self.ps[x]: return x else: t = self.find(self.ps[x]) self.ws[x] += self.ws[self.ps[x]] self.ps[x] = t return self.ps[x] def weight(self, x: int) -> T: self.find(x) return self.ws[x] def same(self, x: int, y: int) -> bool: return self.find(x) == self.find(y) def unite(self, x: int, y: int, w: T) -> None: w += self.weight(x) w -= self.weight(y) x, y = self.find(x), self.find(y) if x == y: return if self.rs[x] < self.rs[y]: x, y = y, x w = -w self.rs[x] += self.rs[y] self.ps[y] = x self.ws[y] = w self.gs[x].update(self.gs[y]) self.num -= 1 def diff(self, x: int, y: int) -> T: return self.weight(y) - self.weight(x) def size(self, x: int) -> int: return self.rs[self.find(x)] def count(self) -> int: return self.num def group(self, x: int) -> set: return self.gs[self.find(x)] input = sys.stdin.buffer.readline def main() -> None: N, Q = map(int, input().split()) uf = WeightedUnionFind(N) vs = [0]*N def add(a: int, b: int) -> None: vs[uf.find(a)] += b def query(a: int) -> int: r = uf.find(a) return uf.diff(a, r) + vs[r] def unite(a: int, b: int) -> None: a = uf.find(a) b = uf.find(b) if a == b: return uf.unite(a, b, vs[a] - vs[b]) for _ in range(Q): t, a, b = map(int, input().split()) a -= 1 if t == 1: b -= 1 unite(a, b) if t == 2: add(a, b) if t == 3: print(query(a)) if __name__ == "__main__": main()