#include #define REP(i, n) for(int i = 0; (i) < (n); (i)++) using namespace std; long modpow(long a, long n, long mod) { long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long modinv(long a, long mod) { return modpow(a, mod - 2, mod); } int GCD(int a, int b){ if(b == 0) return a; if(a < b) return GCD(b, a); else return GCD(b, a%b); } struct UnionFind { vector par; vector rank; UnionFind(int n = 1){ init(n); } void init(int n = 1){ par.resize(n); rank.resize(n); REP(i, n) par[i] = i, rank[i] = 0; } int root(int x){ if(par[x] == x) return x; else return par[x] = root(par[x]); } bool issame(int x, int y){ return root(x) == root(y); } void merge(int x, int y){ x = root(x); y = root(y); if(x == y) return; if(rank[x] < rank[y]) swap(x, y); if(rank[x] == rank[y]) rank[x]++; par[y] = x; return; } }; int main() { int N, Q; cin >> N >> Q; UnionFind uf(N); int num[500005]; REP(i, N) num[i] = 0; REP(i, Q){ //REP(i, N) cout << num[i] << " "; //cout << endl; int T, A, B; cin >> T >> A >> B; if(T==1){ int nea = uf.root(A-1); int neb = uf.root(B-1); uf.merge(A-1, B-1); if(nea != neb){ if(uf.root(A-1) == nea){ num[neb] -= num[nea]; }else{ num[nea] -= num[neb]; } } }else if(T==2){ num[uf.root(A-1)] += B; }else{ int sum = 0; int first = A-1; for(;;){ sum += num[first]; if(first == uf.par[first]) break; first = uf.par[first]; } cout << sum << endl; } } return 0; }