#define _USE_MATH_DEFINES #include using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector val(n); vector par(n); vector siz(n); for (int i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } while (q--) { int t, a, b; cin >> t >> a >> b; // cerr << endl; // cerr << "type = " << t << " a = " << a << " b = " << b << endl; a--; b--; if (t == 1) { while (par[a] != a) a = par[a]; while (par[b] != b) b = par[b]; if (siz[a] > siz[b]) swap(a, b); if (a == b) continue; siz[b] += siz[a]; val[a] -= val[b]; par[a] = b; //cerr << "a = " << a + 1 << " b = " << b << endl; //cerr << "val[a] = " << val[a] << " val[b] = " << val[b] << endl; } if (t == 2) { while (par[a] != a) a = par[a]; b++; val[a] += b; //cerr << "val[a] = " << val[a] << endl; } if (t == 3) { int ans = val[a]; //cerr << "ans = " << ans << endl; while (par[a] != a) { a = par[a]; ans += val[a]; // cerr << "a = " << a << " val[a] = " << val[a] << endl; // cerr << "ans = " << ans << endl; } cout << ans << '\n'; } } return 0; }