1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define st first
#define nd second
#define rd third
#define int long long
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

const int N = 2e5 + 5;

#ifdef LOCAL
const int B = 3000;
#else
const int B = 1500;
#endif

struct Query {
  int typ;
#warning ll
  long long a, b, c; // 
  int tm;
};

struct Edge {
  int nei;
  long long d;
  int L, R;
  bool operator<(const Edge& oth) const {
    if (nei != oth.nei) { return nei < oth.nei; }
    if (L != oth.L) { return L < oth.L; }
    if (R != oth.R) { return R < oth.R; }
    return d < oth.d;
  }
  friend ostream& operator<<(ostream& out, Edge e) {
    return out<<"("<<e.nei<<", "<<e.d<<", ("<<e.L<<", "<<e.R<<"))";
  }
};

set<Edge> slo1[N];
set<Edge> slo2[N];
int imp[N];
int vis[N];
int id[N];
int wh_col[N];
int last_colored1[N];
int last_colored2[N];
struct Ball {
  int t;
  long long rad;
  bool operator<(const Ball& oth) const {
    return rad > oth.rad;
  }
  friend ostream& operator<<(ostream& out, Ball b) {
    return out<<"(t="<<b.t<<", r="<<b.rad<<")";
  }
};
vector<Ball> balls[N];
int forbid[N];
int sz[N];
vector<Ball> gathered[N];
LD Timestamp() {
  return 1. * clock() / CLOCKS_PER_SEC;
}
int t;
int visited;
int all_visited;
int all_work;
int ziom[N];
void Maluj(int v, long long rem, bool new_cc) {
  if (new_cc) {
    while (!balls[v].empty() && balls[v].back().rad <= rem) {
      balls[v].pop_back();
    }
    balls[v].PB({t, rem});
  }
  visited++;
  all_visited++;
  last_colored2[v] = t;
  for (auto& e : slo2[v]) {
    if (e.R < t || e.L > t || e.d > rem) { continue; }
    if (last_colored2[e.nei] == t) { continue; }
    Maluj(e.nei, rem - e.d, e.L != 0);
  }
}
vector<Edge> dump1[N];
vector<Edge> dump2[N];
void MalujDuzo(int v, bool new_cc) {
  if (new_cc) {
    ziom[v] = t;
  }
  last_colored2[v] = t;
  for (auto& e : dump2[v]) {
    if (e.R < t || e.L > t) { continue; }
    if (last_colored2[e.nei] == t) { continue; }
    MalujDuzo(e.nei, e.L != 0);
  }
}
int lol;
struct CentDecomp {
  
  /* Start of general part */
  vector<VPII> slo;
  int n;
  VI sz, dis;
  struct Info {
    int cent, dist, subtr;
  };
  vector<vector<Info>> cents;
  vector<set<PII>> balls;
  CentDecomp(vector<VPII>& slo_, int n_) {
    slo = slo_;
    n = n_;
    sz.resize(n + 2);
    dis.resize(n + 2);
    cents.resize(n + 2);
    balls.resize(n + 2);
    Rec(1, n);
  }
  
  void Rec(int v, int size) {
    v = CntSzCent(v, size, -1, 0, 0);
    dis[v] = 0;
    CntSzCent(v, size, -1, v, SZ(slo[v]));
    for (int i = 0; i < SZ(slo[v]); i++) {
      int nei = slo[v][i].st;
      DelEdge(nei, v);
      Rec(nei, sz[nei]);
    }
  }
  
  int CntSzCent(int v, int size, int par, int root, int subtr) {
    sz[v] = 1;
    if (root) {
      cents[v].PB({root, dis[v], subtr});
    }
    int ret = 0;
    for (int i = 0; i < SZ(slo[v]); i++) {
      int nei = slo[v][i].st;
      if (v == root) {
        subtr = i;
      }
      if (par == nei) { continue; }
      dis[nei] = dis[v] + slo[v][i].nd;
      int cnt_rec = CntSzCent(nei, size, v, root, subtr);
      sz[v] += sz[nei];
      if (cnt_rec) {
        ret = cnt_rec;
      }
    }
    if (ret == 0) {
      if (sz[v] >= size / 2) {
        return v;
      } else {
        return 0;
      }
    } else {
      return ret;
    }
  }
  
  void DelEdge(int v, int nei) {
    for (int i = 0; i < SZ(slo[v]); i++) {
      if (slo[v][i].st == nei) {
        swap(slo[v][i], slo[v].back());
        slo[v].pop_back();
        return;
      }
    }
  }
  /* End of general part */
  
  /* Start of less general part */
  void AddBall(int v, int r, int tm) {
    for (auto tr : cents[v]) {
      set<PII>& secik = balls[tr.cent];
      if (r < tr.dist) { continue; }
      while (!secik.empty() && secik.begin()->st <= r - tr.dist) {
        secik.erase(secik.begin());
      }
      secik.insert({r - tr.dist, tm});
    }
  }
  
  int Query(int v) {
    int res = 0;
    for (auto tr : cents[v]) {
      auto it = balls[tr.cent].lower_bound({tr.dist, 0});
      if (it != balls[tr.cent].end()) {
        maxi(res, it->nd);
      }
    }
    return res;
  }
  /* End of less general part */
};

LD spojne;
int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(2);
  cerr << fixed << setprecision(2);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  int n, m, qcnt;
  cin>>n>>m>>qcnt;
  RE (i, m) {
    int a, b;
    long long d;
    cin>>a>>b>>d;
    slo1[a].insert({b, d, 0, qcnt});
    slo1[b].insert({a, d, 0, qcnt});
  }
  vector<Query> qs;
  int round = 0;
  LD centroidy = 0;
  LD dfsy = 0;
  LD virty = 0;
  function<void()> Process = [&]() {
    round++;
    VI imps;
    int d = 0;
    int pushuj = 1;
    function<void(int)> MakeImp = [&](int v) {
      if (imp[v] == round) { return; }
      imp[v] = round;
      d++;
      slo2[d].clear();
      balls[d].clear();
      id[v] = d;
      last_colored2[d] = 0;
      if (pushuj) {
        imps.PB(v);
      }
    };
    int mt = qs[0].tm;
    for (auto q : qs) {
      if (q.typ <= 2) {
        MakeImp(q.a);
        MakeImp(q.b);
        if (q.typ == 1) {
          slo1[q.a].insert({(int)q.b, q.c, q.tm, qcnt});
          slo1[q.b].insert({(int)q.a, q.c, q.tm, qcnt});
          slo2[id[q.a]].insert({id[q.b], q.c, q.tm, qcnt});
          slo2[id[q.b]].insert({id[q.a], q.c, q.tm, qcnt});
        } else {
          REP (tr, 2) {
            auto it1 = slo1[q.a].lower_bound({(int)q.b, 0, 0, 0});
            assert(it1 != slo1[q.a].end());
            assert(it1->nei == q.b);
            auto it2 = slo2[id[q.a]].lower_bound({id[q.b], 0, qcnt, qcnt});
            int zrob_nowa = 0;
            if (it2 == slo2[id[q.a]].begin()) {
              zrob_nowa = 1;
            } else {
              it2--;
              if (it2->nei != id[q.b]) {
                zrob_nowa = 1;
              }
            }
            if (!zrob_nowa) {
              auto eee = *it2;
              slo2[id[q.a]].erase(it2);
              eee.R = q.tm;
              slo2[id[q.a]].insert(eee);
            } else {
              slo2[id[q.a]].insert({id[q.b], it1->d, mt, q.tm});
            }
            slo1[q.a].erase(it1);
            swap(q.a, q.b);
          }
        }
      } else {
        MakeImp(q.a);
      }
    }
    pushuj = 0;
    int nowe = 0;
    function<pair<int, long long>(int)> CreateVirtual = [&](int v) {
      vis[v] = round;
      vector<pair<int, long long>> sons;
      for (auto e : slo1[v]) {
        if (!(e.L < mt && e.R == qcnt)) { continue; }
        if (vis[e.nei] == round) { continue; }
        pair<int, long long> son = CreateVirtual(e.nei);
        if (son.st) {
          sons.PB({son.st, son.nd + e.d});
        }
      }
      if (SZ(sons) >= 2) {
        if (imp[v] != round) {
          nowe++;
        }
        MakeImp(v);
      }
      //debug(v, sons);
      if (imp[v] == round) {
        for (auto son : sons) {
          slo2[id[v]].insert({son.st, son.nd, 0, qcnt});
          slo2[son.st].insert({id[v], son.nd, 0, qcnt});
        }
        return pair<int, long long>{id[v], 0ll};
      } else if (SZ(sons) == 1) {
        return sons[0];
      } else {
        return pair<int, long long>{0, 0ll};
      }
    };
    virty -= Timestamp();
    //debug("pre", SZ(imps));
    for (auto v : imps) {
      if (vis[v] == round) { continue; }
      CreateVirtual(v);
    }
    debug("post", SZ(imps) + nowe);
    virty += Timestamp();
// #ifdef LOCAL
//     RE (v, n) {
//       if (imp[v] == round) {
//         cerr<<v<<"->"<<id[v]<<endl;
//       }
//     }
//     RE (i, d) {
//       debug(i, slo2[i]);
//     }
// #endif
    visited = 0;
    int naive = 0;

    dfsy -= Timestamp();
    for (auto q : qs) {
      if (q.typ == 3) {
        naive += SZ(imps) + nowe;
        t = q.tm;
        //Maluj(id[q.a], q.tm, q.b, true);
        Maluj(id[q.a], q.b, true);
      } else if (q.typ == 4) {
        int when = max(last_colored1[q.a], last_colored2[id[q.a]]);
        cout<<wh_col[when]<<"\n";
      }
    }
    dfsy += Timestamp();
    //debug(visited, naive);
    function<void(int, int)> GetSz = [&](int v, int par) {
      sz[v] = 1;
      for (auto& e : slo1[v]) {
        if (e.L >= mt || e.nei == par) { continue; }
        GetSz(e.nei, v);
        sz[v] += sz[e.nei];
      }
    };
    int work = 0;
    function<int(int, int, int)> GetCent = [&](int v, int size, int par) {
      work += SZ(slo1[v]);
      sz[v] = 1;
      int ret = 0;
      for (auto& e : slo1[v]) {
        if (e.L >= mt || e.nei == par || forbid[e.nei] == round) { continue; }
        int cnt_rec = GetCent(e.nei, size, v);
        sz[v] += sz[e.nei];
        if (cnt_rec) {
          ret = cnt_rec;
        }
      }
      if (ret == 0) {
        if (sz[v] >= size / 2) {
          return v;
        } else {
          return 0ll;
        }
      }
      return ret;
    };
    
    int cent;
    function<void(int, int, long long)> GatherBalls = [&](int v, int par, long long dis) {
      if (imp[v] == round) {
        for (auto b : balls[id[v]]) {
          if (b.rad >= dis) {
            gathered[cent].PB({b.t, b.rad - dis});
          }
        }
      }
      work += SZ(slo1[v]);
      sz[v] = 1;
      for (auto& e : slo1[v]) {
        if (e.L >= mt || forbid[e.nei] == round || e.nei == par) { continue; }
        GatherBalls(e.nei, v, dis + e.d);
        sz[v] += sz[e.nei];
      }
    };
    
    function<void(int, int, long long, int)> KunczWasc = [&](int v, int par, long long dis, int last) {
      if (dis > gathered[cent][0].rad) { return; }
      if (dis > gathered[cent][last].rad) {
//         while (dis > gathered[cent][last].rad) {
//           last--;
//         }
        int kl = 1, kp = last - 1, faj = 0;
        while (kl <= kp) {
          int aktc = (kl + kp) / 2;
          if (dis <= gathered[cent][aktc].rad) {
            faj = aktc;
            kl = aktc + 1;
          } else {
            kp = aktc - 1;
          }
        }
        last = faj;
      }
      maxi(last_colored1[v], gathered[cent][last].t);
      for (auto& e : slo1[v]) {
        if (e.L >= mt || e.nei == par || forbid[e.nei] == round) { continue; }
        KunczWasc(e.nei, v, dis + e.d, last);
      }
    };  
    
    function<void(int, int)> Rec = [&](int v, int size) {
      int orv = v;
      v = GetCent(v, size, -1);
      forbid[v] = round;
      cent = v;
      //debug(orv, size, cent);
      GatherBalls(v, -1, 0);
      sort(ALL(gathered[v]));
      //debug(orv, size, v, gathered[v]);
      vector<Ball> filtered;
      for (auto b : gathered[v]) {
        if (filtered.empty() || filtered.back().t < b.t) {
          filtered.PB(b);
        }
      }
      swap(filtered, gathered[v]);
      if (!gathered[v].empty()) {
        KunczWasc(v, -1, 0, SZ(gathered[v]) - 1);
      }
      for (auto& e : slo1[v]) {
        if (e.L >= mt || forbid[e.nei] == round) { continue; }
        Rec(e.nei, sz[e.nei]);
      }
      vector<Ball> emp;
      swap(emp, gathered[v]);
    };
    
    //debug("!!!!!!!!!!", mt);
    centroidy -= Timestamp();
    RE (v, n) {
      if (forbid[v] != round) {
        //debug("cc", v);
        GetSz(v, -1);
        Rec(v, sz[v]);
      }
    }
    all_work += work;
    if (qs[0].tm == 1) {
      debug(work / 1e6);
    }
    centroidy += Timestamp();
// #ifdef LOCAL
//     cerr<<"last colored1:\n";
//     RE (v, n) {
//       cerr<<last_colored1[v]<<" ";
//     }
//     cerr<<endl;
// #endif
    
    
    qs.clear();
  };
  
  function<void()> ProcessDupa = [&]() {
    round++;
    VI imps;
    int d = 0;
    int pushuj = 1;
    function<void(int)> MakeImp = [&](int v) {
      if (imp[v] == round) { return; }
      imp[v] = round;
      d++;
      slo2[d].clear();
      balls[d].clear();
      id[v] = d;
      ziom[d] = 0;
      last_colored2[d] = 0;
      if (pushuj) {
        imps.PB(v);
      }
    };
    int mt = qs[0].tm;
    for (auto q : qs) {
      if (q.typ <= 2) {
        MakeImp(q.a);
        MakeImp(q.b);
        if (q.typ == 1) {
          slo1[q.a].insert({(int)q.b, q.c, q.tm, qcnt});
          slo1[q.b].insert({(int)q.a, q.c, q.tm, qcnt});
          slo2[id[q.a]].insert({id[q.b], q.c, q.tm, qcnt});
          slo2[id[q.b]].insert({id[q.a], q.c, q.tm, qcnt});
        } else {
          REP (tr, 2) {
            auto it1 = slo1[q.a].lower_bound({(int)q.b, 0, 0, 0});
            assert(it1 != slo1[q.a].end());
            assert(it1->nei == q.b);
            auto it2 = slo2[id[q.a]].lower_bound({id[q.b], 0, qcnt, qcnt});
            int zrob_nowa = 0;
            if (it2 == slo2[id[q.a]].begin()) {
              zrob_nowa = 1;
            } else {
              it2--;
              if (it2->nei != id[q.b]) {
                zrob_nowa = 1;
              }
            }
            if (!zrob_nowa) {
              auto eee = *it2;
              slo2[id[q.a]].erase(it2);
              eee.R = q.tm;
              slo2[id[q.a]].insert(eee);
            } else {
              slo2[id[q.a]].insert({id[q.b], it1->d, mt, q.tm});
            }
            slo1[q.a].erase(it1);
            swap(q.a, q.b);
          }
        }
      } else {
        MakeImp(q.a);
      }
    }
    RE (i, n) {
      dump1[i] = vector<Edge>(ALL(slo1[i]));
    }
    pushuj = 0;
    int nowe = 0;
    function<int(int)> CreateVirtual = [&](int v) {
      vis[v] = round;
      vector<int> sons;
      for (auto& e : dump1[v]) {
        if (!(e.L < mt && e.R == qcnt)) { continue; }
        if (vis[e.nei] == round) { continue; }
        int son = CreateVirtual(e.nei);
        if (son) {
          sons.PB(son);
        }
      }
      if (SZ(sons) >= 2) {
        if (imp[v] != round) {
          nowe++;
        }
        MakeImp(v);
      }
      //debug(v, sons);
      if (imp[v] == round) {
        for (auto& son : sons) {
          slo2[id[v]].insert({son, 0, 0, qcnt});
          slo2[son].insert({id[v], 0, 0, qcnt});
        }
        return id[v];
      } else if (SZ(sons) == 1) {
        return sons[0];
      } else {
        return 0ll;
      }
    };
    virty -= Timestamp();

    //debug("pre", SZ(imps));
    for (auto v : imps) {
      if (vis[v] == round) { continue; }
      CreateVirtual(v);
    }
    //debug("post", SZ(imps) + nowe);
    virty += Timestamp();
    
    RE (i, n) {
      dump2[i] = vector<Edge>(ALL(slo2[i]));
    }
    visited = 0;
    int naive = 0;
    
    dfsy -= Timestamp();
    for (auto q : qs) {
      if (q.typ == 3) {
        naive += SZ(imps) + nowe;
        t = q.tm;
        //Maluj(id[q.a], q.tm, q.b, true);
        MalujDuzo(id[q.a], true);
      } else if (q.typ == 4) {
        int when = max(last_colored1[q.a], last_colored2[id[q.a]]);
        cout<<wh_col[when]<<"\n";
      }
    }
    dfsy += Timestamp();
    //debug(visited, naive);
    
//     function<int(int)> SimpleDfs = [&](int v) {
//       lol++;
//       vis[v] = round;
//       int ret = 0;
//       if (imp[v] == round - 1) {
//         ret = ziom[id[v]];
//       }
//       for (auto& e : slo1[v]) {
//         if (e.L >= mt || vis[e.nei] == round) { continue; }
//         maxi(ret, SimpleDfs(e.nei));
//       }
//       return ret;
//     };
//     function<void(int, int, int)> Kuncz = [&](int v, int huh, int par) {
//       lol++;
//       last_colored1[v] = huh;
//       for (auto& e : slo1[v]) {
//         if (e.L >= mt || e.nei == par) { continue; }
//         Kuncz(e.nei, huh, v);
//       }
//     };
    round++;
    spojne -= Timestamp();
    RE (v, n) {
      if (vis[v] == round) { continue; }
      vector<int> que{v};
      vis[v] = round;
      int huh = 0;
      for (int ii = 0; ii < SZ(que); ii++) {
        int u = que[ii];
        if (imp[u] == round - 1) {
          maxi(huh, ziom[id[u]]);
        }
        for (auto& e : dump1[u]) {
          if (e.L < mt && vis[e.nei] != round) {
            que.PB(e.nei);
            vis[e.nei] = round;
          }
        }
      }
      if (huh) {
        for (auto u : que) {
          last_colored1[u] = huh;
        }
      }
      //int huh = SimpleDfs(v);
//       if (huh) {
//         Kuncz(v, huh, -1);
//       }
    }
    spojne += Timestamp();
    
    qs.clear();
  };
  
  
  int ccs = 5;
  int kulki = 5;
  int R = 6e6;
  int any_change = 0;
  vector<Query> all_qs;
  int any_small_ball = 0;
  const int kInf = 1e15;
  RE (i, qcnt) {
    int typ;
    cin>>typ;
    Query query;
    query.typ = typ;
    query.tm = i;
    if (typ == 1 || typ == 3) {
      cin>>query.a>>query.b>>query.c;
#ifdef LOCAL
      if (typ == 3) {
        query.b = kInf;
      }
#endif
      if (typ == 3 && query.b != kInf) {
        any_small_ball = 1;
      }
      wh_col[i] = query.c;
    } else if (typ == 2) {
      cin>>query.a>>query.b;
    } else {
      cin>>query.a;
    }
    if (query.typ <= 2) {
      any_change = 1;
    }
    all_qs.PB(query);
  }
  if (any_change && any_small_ball) {
    for (auto query : all_qs) {
      if (query.typ <= 2 || query.typ == 4) {
        ccs++;
      }
      if (query.typ == 3) {
        kulki++;
      }
      qs.PB(query);
      if (ccs * kulki > R || query.tm == qcnt) {
        Process();
        ccs = 5;
        kulki = 5;
      }
    }
    debug(virty, dfsy, centroidy, round, all_visited / 1e6, all_work / 1e6);
  } else if (any_small_ball) {
    debug("cent decomp");
    vector<VPII> slo(n + 2);
    RE (i, n) {
      for (auto& e : slo1[i]) {
        slo[i].PB({e.nei, e.d});
      }
    }
    CentDecomp cent_decomp(slo, n);
    for (auto q : all_qs) {
      if (q.typ == 3) {
        cent_decomp.AddBall(q.a, q.b, q.tm);
      } else {
        int tm = cent_decomp.Query(q.a);
        cout<<wh_col[tm]<<"\n";
      }
    }
  } else {
    debug("I got big balls (1e15 only)");
    for (auto query : all_qs) {
      if (query.typ <= 2 || query.typ == 4) {
        ccs++;
      }
      if (query.typ == 3) {
        kulki++;
      }
      qs.PB(query);
      if (query.tm % 5000 == 0 || query.tm == qcnt) {
        ProcessDupa();
        ccs = 5;
        kulki = 5;
      }
    }
    
    debug(virty, dfsy, spojne, round, all_visited / 1e6, all_work / 1e6, lol / 1e6);
  }
    
    
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}