from sys import setrecursionlimit l=open(0).readline m=lambda:map(int,l().split()) def f(p,a,i): t=p[i] if t<0: return i,0 t,u=f(p,a,t) p[i]=t a[i]+=u return t,a[i] def u(p,a,i,j): i,_=f(p,a,i) j,_=f(p,a,j) if i==j: return p[j]+=p[i] p[i]=j a[i]-=a[j] setrecursionlimit(10**6) N,Q=m() p=[-1]*(N+1) a=[0]*(N+1) r=[] for _ in range(Q): T,A,B=m() if T==1: u(p,a,A,B) elif T==2: j,_=f(p,a,A) a[j]+=B elif T==3: j,k=f(p,a,A) r.append(a[j]+k) print(*r,sep='\n')