#include #include #include #include #include #include #include using namespace std; using ll = long long; struct UnionFind { UnionFind(int n) : p(n, -1), s(n, 0) {} int root(int u) { if (p[u] < 0) return u; int r = root(p[u]); if (r != p[u]) { s[u] += s[p[u]]; p[u] = r; } return r; } int size(int u) { return -p[root(u)]; } bool same(int u, int v) { return root(u) == root(v); } int unite(int u, int v) { u = root(u); v = root(v); if (u == v) return u; if (p[u] > p[v]) swap(u, v); p[u] += p[v]; p[v] = u; s[v] -= s[u]; return u; } void add(int u, int a) { s[root(u)] += a; } int sum(int u) { int r = root(u); return r == u ? s[u] : s[u] + s[r]; } vector p, s; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; UnionFind uf(n); for (int _ = 0; _ < q; _++) { int t, a, b; cin >> t >> a >> b; a--; if (t == 1) { b--; if (!uf.same(a, b)) uf.unite(a, b); } else if (t == 2) { uf.add(a, b); } else { cout << uf.sum(a) << '\n'; } } return 0; }