#union and query import sys class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * (n+1) def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) input = sys.stdin.readline def main(): N, Q = map(int, input().split()) Uni = UnionFind(N) members = [[i] for i in range(N+1)] root_weight = [0 for i in range(N+1)] node_weight = [0 for i in range(N+1)] command = [tuple(map(int, input().split())) for i in range(Q)] for demand, a, b in command: if demand == 1: #unite if Uni.same(a, b): pass else: A, B = Uni.find(a), Uni.find(b) Uni.union(A, B) if Uni.find(A) == A: # A's member was smaller #merging members[A] = members[A] + members[B] for node in members[B]: node_weight[node] += root_weight[B] - root_weight[A] else: members[B] = members[A] + members[B] for node in members[A]: node_weight[node] += root_weight[A] - root_weight[B] elif demand == 2: A = Uni.find(a) root_weight[A] += b else: A = Uni.find(a) print(root_weight[A] + node_weight[a]) return if __name__ == "__main__": main()