#include using namespace std; #define PII pair #define VI vector #define VPII vector #define LL long long #define f first #define s second #define MP make_pair #define PB push_back #define LD long double #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++) #define FORD(i, b, e) for (int i = (b); i >= (int)(e); i--) #define ll LL #define mp MP #define pb PB #define st f #define nd s #define eb emplace_back const int inf = 1e9 + 7; const LL INF = 1e18L + 7; #define sim template ostream & operator << (ostream &p, pair x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if::value, decltype(y.begin(), p)>::type {int o = 0; for (auto c : y) {if (o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if (p > y) p = y;} sim, class s> void maxi(n &p, s y) {if (p < y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int N = 1<<17; int n, q; int t[N]; int x[N]; int y[N]; int v[N]; int ans[N]; map M; struct E { int a, b, w; }; vector load[2*N]; struct dsu { int rep[N]; int siz[N]; VPII roll; int Find(int a) { if(a!=rep[a]) return Find(rep[a]); else return a; } void Union(int a, int b) { a = Find(a); b = Find(b); roll.eb(a, siz[a]); roll.eb(b, siz[b]); if(a==b) return; if(siz[a]>siz[b]) swap(a, b); rep[a] = b; siz[b] += siz[a]; } void rollback() { rep[roll.back().st] = roll.back().st; siz[roll.back().st] = roll.back().nd; roll.pop_back(); } }; dsu D[10]; void insert(int a, int b, E c, int v = 1, int l = 0, int r = N-1) { if(a>r || l>b) return; if(a<=l && r<=b) { load[v].pb(c); return; } insert(a, b, c, 2*v, l, (l+r)/2); insert(a, b, c, 2*v+1, (l+r)/2+1, r); } void dfs(int u = 1, int l = 0, int r = N-1) { for(auto it:load[u]) { for(int j = it.w; j < 10; ++j) { D[j].Union(it.a, it.b); } } if(l==r && l> n >> q; for(int i = 0; i < q; ++i) { cin >> t[i]; if(t[i]==0) { cin >> x[i] >> y[i] >> v[i]; if(x[i]>y[i]) swap(x[i], y[i]); M[{x[i], y[i]}] = mp(i, v[i]); } else if(t[i]==1) { cin >> x[i] >> y[i]; if(x[i]>y[i]) swap(x[i], y[i]); auto m = M[{x[i], y[i]}]; insert(m.st, i-1, (E){x[i], y[i], m.nd}); M[{x[i], y[i]}] = mp(-1, -1); } else { cin >> x[i] >> y[i]; } } for(auto it:M) { if(it.nd!=mp(-1, -1)) { auto m = it.nd; insert(m.st, q-1, (E){it.st.st, it.st.nd, m.nd}); } } for(int i = 0; i < 10; ++i) { for(int j = 0; j < n; ++j) { D[i].rep[j] = j; D[i].siz[j] = 1; } } dfs(); for(int i = 0; i < q; ++i) { if(t[i]==2) cout << ans[i] << endl; } }