#include using namespace std; #define rep(i, n) for(int i = 0; i < (int)n; ++i) #define FOR(i, a, b) for(int i = a; i < (int)b; ++i) #define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i) using ll = long long; using ld = long double; const ll INF = 1e18; const int Inf = 1e9; const double EPS = 1e-9; const int MOD = 1e9 + 7; vector > g; class DisjointSet { public: vector rank, p, size, sum; DisjointSet() {} DisjointSet(int s) { rank.resize(s, 0); p.resize(s, 0); size.resize(s, 0); sum.resize(s, 0); rep (i, s) init(i); } void init(int x) { p[x] = x; rank[x] = 0; size[x] = 1; } bool isSame(int x, int y) { return root(x) == root(y); } void makeset(int x, int y) { if (isSame(x, y)) return; link(root(x), root(y)); } void link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; size[x] += size[y]; int diff = sum[y] - sum[x]; sum[y] = 0; for (auto z : g[y]) { sum[z] += diff; g[x].push_back(z); } g[y].clear(); } else { p[x] = y; size[y] += size[x]; if (rank[x] == rank[y]) { rank[y]++; } int diff = sum[x] - sum[y]; sum[x] = 0; for (auto z : g[x]) { sum[z] += diff; g[y].push_back(z); } g[x].clear(); } } int root(int x) { if (x != p[x]) { p[x] = root(p[x]); } return p[x]; } int getSize(int x) { return size[root(x)]; } void add(int x, int v) { int p = root(x); sum[p] += v; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(0); int n, q; cin >> n >> q; g.resize(n); DisjointSet dj = DisjointSet(n); rep (i, n) g[i].push_back(i); rep (i, q) { int t, a, b; cin >> t >> a >> b; if (t == 1) { dj.makeset(a - 1, b - 1); } else if (t == 2) { dj.add(a - 1, b); } else { a--; int p = dj.root(a); if (p == a) cout << dj.sum[a] << endl; else cout << dj.sum[a] + dj.sum[p] << endl; } } return 0; }