#include #include using namespace atcoder; using namespace std; using ll = long long; int main() { int N, Q;cin >> N >> Q; vector R(N); for (int v = 0;v < N;v++) R[v] = v; vector> comp(N); for (int v = 0;v < N;v++) comp[v].push_back(v); vector T(N, 0); vector S(N, 0); vector P(N, 0); while (Q--) { int t, a, b;cin >> t >> a >> b; if (t == 1) { a--;b--; if (R[a] == R[b]) continue; int ra = R[a];int rb = R[b]; if (comp[ra].size() < comp[rb].size()) swap(ra, rb); ll ad = T[rb];ll sa = T[ra]; while (!comp[rb].empty()) { int v = comp[rb].back();comp[rb].pop_back(); P[v] += ad - S[v]; S[v] = sa; R[v] = ra; comp[ra].push_back(v); } T[rb] = 0; } else if (t == 2) { a--; int r = R[a]; T[r] += b; } else { a--; int r = R[a]; ll ans = P[a] + T[r] - S[a]; cout << ans << endl; } } }