#include using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--) #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() typedef long long ll; typedef vector vi; typedef vector vl; const ll INF = 1e18; const ll MOD = 1e9 + 7; template T chmax(T& a, const T& b){return a = (a > b ? a : b);} template T chmin(T& a, const T& b){return a = (a < b ? a : b);} #define DEBUG_MODE #ifdef DEBUG_MODE #define dump(x) cout << #x << " : " << x << " " #define dumpL(x) cout << #x << " : " << x << '\n' #define LINE cout << "line : " << __LINE__ << " " #define LINEL cout << "line : " << __LINE__ << '\n' #define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " " #define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl #define STOP assert(false) #else #define dump(x) #define dumpL(x) #define LINE #define LINEL #define dumpV(v) #define dumpVL(v) #define STOP assert(false) #endif #define mp make_pair namespace std { template ostream &operator <<(ostream& out, const pair& a) { out << '(' << a.fi << ", " << a.se << ')'; return out; } } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vi par(n, -1); iota(all(par), 0); vector> ch(n); vi v(n); rep(_, q) { int t, a, b; cin >> t >> a >> b; --a; if(t == 1) { --b; a = par[a]; b = par[b]; if(a == b) continue; if(ch[a].size() > ch[b].size()) swap(a, b); while(ch[a].size()) { int c = ch[a].front(); ch[a].pop(); int d = v[a] - v[c]; ch[b].emplace(c); par[c] = b; v[c] = v[b] - d; } ch[b].emplace(a); par[a] = b; v[a] = v[b] - v[a]; } else if(t == 2) { int p = par[a]; v[p] += b; } else { if(par[a] == a) cout << v[a] << '\n'; else cout << v[par[a]] - v[a] << '\n'; } } return 0; }