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
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#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;
#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 T> ostream& operator<<(ostream& out, vector<T> vec);
template<class T> ostream& operator<<(ostream& out, set<T> vec);
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec);
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<<")"; }
ostream& operator<<(ostream& out, __int128_t x) {
  if (x < 0) { out<<"-"; x = -x; }
  if (x == 0) { return out<<"0"; }
  VI digs;
  while (x) {
    digs.PB(x % 10);
    x /= 10;
  }
  reverse(ALL(digs));
  for (auto d : digs) { out<<d; }
  return out;
}
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...);
}

int n;
int orig_n;
// int Solve1(int a) {
//   return diam >= a;
// }
vector<VPII> slo;
bool MajorizedBySet(int a, int b, set<PII>& ps) {
  auto it = ps.lower_bound({a, 0});
  debug(ps);
  if (it == ps.end()) { return false; }
  return it->nd >= b;
}

// set<PII> all_ps; // TODO
// int Solve2(int a, int b) {
//   if (a > b) { swap(a, b); }
//   return MajorizedBySet(a, b, all_ps);
// }

// PII Delete(vector<PII>& v, int nei) {
//   REP (i, SZ(v)) {
//     if (v[i].st == nei) {
//       swap(v[i], v.back());
//       PII cp = v.back();
//       v.pop_back();
//       return cp;
//     }
//   }
//   return {-1, -1};
// }

int Locate(vector<PII>& v, int nei) {
  REP (i, SZ(v)) {
    if (v[i].st == nei) {
      return i;
    }
  }
  assert(0);
}

int DistToFurthestRec(int v, int par, int dis) {
  int res = dis;
  for (auto [nei, w] : slo[v]) {
    if (nei == par) { continue; }
    maxi(res, DistToFurthestRec(nei, v, dis + w));
  }
  return res;
}

int DistToFurthest(int v) { return DistToFurthestRec(v, -1, 0); }

struct DiamInfo {
  int v, len;
  friend ostream& operator<<(ostream& out, DiamInfo d) {
    return out<<"(v="<<d.v<<", len="<<d.len<<")";
  }
};

void PathToFurthestRec(int v, int par, int cur_dis, int need_dis, vector<DiamInfo>& res) {
  if (cur_dis == need_dis) {
    res.PB({v, need_dis});
    return;
  }
  for (auto [nei, w] : slo[v]) {
    if (nei == par) { continue; }
    PathToFurthestRec(nei, v, cur_dis + w, need_dis, res);
    if (!res.empty()) {
      res.PB({v, cur_dis});
      return;
    }
  }
}



vector<DiamInfo> PathToFurthest(int v) {
  int deepest = DistToFurthest(v);
  vector<DiamInfo> res;
  PathToFurthestRec(v, -1, 0, deepest, res);
  reverse(ALL(res));
  return res;
}

int Deattach(int rt, int nei) { // remove nei->rt, add nei<->fake_rt, return fake_rt
  //PII cp = Delete(slo[nei], rt);
  //assert(cp.st != -1);
  int ind = Locate(slo[nei], rt);
  n++;
  int fake_rt = n;
  slo[nei][ind].st = fake_rt;
  slo.resize(fake_rt + 2);
  //slo[nei].PB({fake_rt, cp.nd});
  slo[fake_rt].PB({nei, slo[nei][ind].nd});
  //cerr<<"deattach "<<nei<<" from "<<rt<<" to "<<fake_rt<<endl;
  return fake_rt;
}

void Reattach(int rt, int nei, int fake_rt) {
  //cerr<<"reattach "<<nei<<" from "<<fake_rt<<" back to "<<rt<<endl;
  //int c = slo[fake_rt][0].nd;
  //assert(slo[fake_rt].back().st == nei);
  //assert(slo[nei].back().st == fake_rt);
  int ind = Locate(slo[nei], fake_rt);
  // slo[fake_rt].pop_back();
  // slo[nei].pop_back();
  // slo[nei].PB({rt, c});
  slo[nei][ind].st = rt;
}

struct Handle {
  int real_rt_ord, son, fake_rt;
};

vector<Handle> GetHandles(vector<DiamInfo>& diam) {
  vector<Handle> handles;
  REP (ii, SZ(diam)) {
    int v = diam[ii].v;
    int left = -1, right = -1;
    if (ii) { left = diam[ii - 1].v; }
    if (ii + 1 < SZ(diam)) { right = diam[ii + 1].v; }
    for (auto [son, c] : slo[v]) {
      if (son == left || son == right) { continue; }
      int fake_rt = Deattach(v, son);
      handles.PB({ii, son, fake_rt});
      // 
      // UnrootedDS2 ur(fake_rt);
      // Reattach(v, son, fake_rt);
    }
  }
  return handles;
};

void ReattachHandles(vector<DiamInfo>& diam, vector<Handle>& handles) {
  for (auto handle : handles) {
    Reattach(diam[handle.real_rt_ord].v, handle.son, handle.fake_rt);
  }
}

struct PrefInfo {
  int len, id, son;
  friend ostream& operator<<(ostream& out, PrefInfo oth) {
    return out<<"(len="<<oth.len<<", id="<<oth.id<<", son="<<oth.son<<")";
  }
};

struct ProcessedDiam {
  vector<DiamInfo> main_diam;
  vector<int> max_descend_at;
  vector<int> max_pref_diam;
  vector<int> max_descend_pref, max_descend_suf;
  vector<PrefInfo> firsts, seconds;
  vector<PrefInfo> rev;
  ProcessedDiam() {}
  ProcessedDiam(vector<DiamInfo> diammmmm) {
    main_diam = diammmmm;
    int D = SZ(main_diam);
    int diam_len = main_diam.back().len;
    max_descend_at.resize(D);
    max_pref_diam.resize(D);
    max_descend_pref.resize(D);
    max_descend_suf.resize(D);
    
    vector<Handle> handles = GetHandles(main_diam);
    vector<vector<Handle>> by_id(SZ(main_diam));
    for (auto handle : handles) {
      by_id[handle.real_rt_ord].PB(handle);
    }
    
    function<void(PrefInfo)> Consider = [&](PrefInfo typek) {
      if (firsts.empty() || typek.len > firsts.back().len) {
        firsts.PB(typek);
      } else if (seconds.empty() || typek.len > seconds.back().len) {
        seconds.PB(typek);
      }
    };
    
    REP (ii, D) {
      PrefInfo plain{main_diam[ii].len, ii, 0};
      Consider(plain);
      if (ii) { max_pref_diam[ii] = max_pref_diam[ii - 1]; }
      for (auto handle : by_id[ii]) {
        int v = handle.fake_rt;
        vector<DiamInfo> inner_path = PathToFurthest(v);
        int deepest = inner_path.back().len;
        maxi(max_descend_at[ii], deepest);
        PrefInfo cand{main_diam[ii].len + deepest, ii, handle.son};
        Consider(cand);
        maxi(max_pref_diam[ii], PathToFurthest(inner_path.back().v).back().len);
      }
    }
    
    FORD (ii, D - 1, 0) {
      int to_end = diam_len - main_diam[ii].len;
      PrefInfo plain{to_end, ii, 0};
      if (rev.empty() || plain.len > rev.back().len) { rev.PB(plain); }
      PrefInfo cand{to_end + max_descend_at[ii], ii, 1};
      if (cand.len > rev.back().len) { rev.PB(cand); }
    }
    //debug(diammmmm);
    //debug(rev);
    
    RE (ii, D - 1) {
      max_descend_pref[ii] = max(max_descend_pref[ii - 1], max_descend_at[ii] + main_diam[ii].len);
    }
    FORD (ii, D - 2, 0) {
      max_descend_suf[ii] = max(max_descend_suf[ii + 1], max_descend_at[ii] + main_diam.back().len - main_diam[ii].len);
    }
    
    ReattachHandles(main_diam, handles);
  }
  PrefInfo GetFirstFromV(vector<PrefInfo>& v, int d) {
    if (v.empty() || v.back().len < d) { return {-1, -1, -1}; }
    int kl = 0, kp = SZ(v) - 1, faj = SZ(v) - 1;
    while (kl <= kp) {
      int aktc = (kl + kp) / 2;
      if (v[aktc].len >= d) {
        faj = aktc;
        kp = aktc - 1;
      } else {
        kl = aktc + 1;
      }
    }
    return v[faj];
  }
  PrefInfo GetFirstAtLeast(int d) {
    return GetFirstFromV(firsts, d);
  }
  PrefInfo GetSecondAtLeast(int d) {
    PrefInfo fir = GetFirstAtLeast(d);
    PrefInfo cand1 = GetFirstAtLeast(fir.len + 1);
    PrefInfo cand2 = GetFirstFromV(seconds, d);
    if (cand1.len == -1) { return cand2; }
    if (cand2.len == -1) { return cand1; }
    if (PII{cand1.id, cand1.son} < PII{cand2.id, cand2.son}) {
      return cand1;
    }
    return cand2;
  }
  PrefInfo GetFromBack(int d) {
    //debug(rev);
    return GetFirstFromV(rev, d);
  }
};

set<PII> FromGathered(VPII gather) {
  sort(ALL(gather));
  VPII filtered;
  for (auto p : gather) {
    while (!filtered.empty() && filtered.back().nd <= p.nd) {
      filtered.pop_back();
    }
    filtered.PB(p);
  }
  return set<PII>(ALL(filtered));
}

struct RootedDS2 {
  int biggest_diam_outside;
  int diam_len;
  RootedDS2() {}
  RootedDS2(int rt) {
    biggest_diam_outside = 0;
    vector<DiamInfo> diam = PathToFurthest(rt);
    diam_len = diam.back().len;
    VPII gather;
    ProcessedDiam processed_diam(diam);
    biggest_diam_outside = processed_diam.max_pref_diam.back();
    REP (ii, SZ(diam)) {
      gather.PB({diam[ii].len + processed_diam.max_descend_at[ii], diam_len - diam[ii].len});
      gather.PB({diam[ii].len, processed_diam.max_descend_at[ii] + diam_len - diam[ii].len});
      if (ii + 1 < SZ(diam)) {
        gather.PB({processed_diam.max_descend_pref[ii], processed_diam.max_descend_suf[ii + 1]});
      }
    }
    //debug(diam);
    gather.PB({diam.back().len, biggest_diam_outside});
    ps = FromGathered(gather);
    //debug("tutaj", ps);
  }
  set<PII> ps; // (x, y) - for a rooted path of length x, the second one can be y
  bool Solve(int a, int b) { // a has to start in the root
    return MajorizedBySet(a, b, ps);
  }
};

struct UnrootedDS2 {
  RootedDS2 inner;
  vector<DiamInfo> diam;
  int big1, big2;
  UnrootedDS2() {}
  UnrootedDS2(int v) {
    diam = PathToFurthest(v);
    v = diam.back().v;
    diam = PathToFurthest(v);
    inner = RootedDS2(v);
  }
  int Solve(int a, int b) {
    debug(a, b);
    return inner.Solve(a, b) || inner.Solve(b, a);
  }
};

struct CandDiam {
  int son, diam;
  bool operator<(const CandDiam& R) const {
    return diam > R.diam;
  }
};

struct Node {
  int backbone, diam, max_pref, max_suf;
  Node() {
    backbone = diam = max_pref = max_suf = 0;
  }
};

Node Join(Node A, Node B) {
  Node r;
  r.backbone = A.backbone + B.backbone;
  r.diam = max(A.diam, B.diam);
  r.max_pref = max(A.max_pref, A.backbone + B.max_pref);
  r.max_suf = max(B.max_suf, B.backbone + A.max_suf);
  maxi(r.diam, A.max_suf + B.max_pref);
  return r;
}

struct Drzewko {
  vector<Node> drz;
  int M;
  int Read(int a, int b) {
    if (a > b) { return 0; }
    a += M;
    b += M;
    Node L = drz[a];
    Node R = drz[b];
    if (a == b) { return L.diam; }
    while (a / 2 != b / 2) {
      if (a % 2 == 0) {
        L = Join(L, drz[a + 1]);
      }
      if (b % 2 == 1) {
        R = Join(drz[b - 1], R);
      }
      a /= 2;
      b /= 2;
    }
    L = Join(L, R);
    return L.diam;
  }
  Drzewko() {}
  Drzewko(vector<Node> base) {
    M = 1;
    while (M <= SZ(base)) {
      M *= 2;
    }
    drz.resize(2 * M + 5);
    REP (i, SZ(base)) {
      drz[i + M] = base[i];
    }
    FORD (i, M - 1, 1) {
      drz[i] = Join(drz[2 * i], drz[2 * i + 1]);
    }
  }
};

bool Clash(PrefInfo& lewo, PrefInfo& prawo) {
  if (lewo.id > prawo.id) { return true; }
  if (lewo.id == prawo.id && lewo.son && prawo.son) { return true; }
  return false;
};


struct OrientedDS3 {
  int diam_len;
  ProcessedDiam processed_diam;
  set<PII> ps;
  vector<RootedDS2*> rooted;
  vector<vector<CandDiam>> cand_diams;
  vector<Node> base;
  Drzewko drz;
  int big1, big2;
  vector<int> best_diam_at;
  OrientedDS3() {}
  // OrientedDS3(const OrientedDS3&) = delete;
  // OrientedDS3& operator=(const OrientedDS3&) = delete;
  OrientedDS3(OrientedDS3&&) = default;
  OrientedDS3& operator=(OrientedDS3&&) = default;
  vector<DiamInfo> main_diam;
  OrientedDS3(vector<DiamInfo> main_diamm) {
    main_diam = main_diamm;
    rooted.resize(orig_n + 2);
    cand_diams.resize(orig_n + 2);
    best_diam_at.resize(orig_n + 2);
    base.resize(2 * SZ(main_diamm));
    big1 = big2 = 0;
    REP (ii, SZ(main_diam) - 1) {
      base[2 * ii].backbone = main_diam[ii + 1].len - main_diam[ii].len;
      base[2 * ii].max_pref = base[2 * ii].backbone;
      base[2 * ii].max_suf = base[2 * ii].backbone;
      base[2 * ii].diam = base[2 * ii].backbone;
    }
    diam_len = main_diam.back().len;
    processed_diam = ProcessedDiam(main_diam);
    vector<Handle> handles = GetHandles(main_diam);
    VPII gather;
    for (auto handle : handles) {
      UnrootedDS2 ur2(handle.fake_rt);
      int deepest = DistToFurthest(handle.fake_rt);
      int inner_diam_len = ur2.diam.back().len;
      cand_diams[handle.real_rt_ord].PB({handle.son, inner_diam_len});
      int base_ind = 2 * handle.real_rt_ord - 1;
      if (inner_diam_len > big1) {
        big2 = big1;
        big1 = inner_diam_len;
      } else if (inner_diam_len > big2) {
        big2 = inner_diam_len;
      }
      //debug(handle.son, inner_diam_len);
      maxi(best_diam_at[handle.real_rt_ord], inner_diam_len);
      maxi(base[base_ind].diam, base[base_ind].max_pref + deepest);
      maxi(base[base_ind].diam, inner_diam_len);
      maxi(base[base_ind].max_pref, deepest);
      maxi(base[base_ind].max_suf, deepest);
      for (auto p : ur2.inner.ps) {
        gather.PB(p);
      }
      rooted[handle.son] = new RootedDS2(handle.fake_rt);
    }
    REP (i, orig_n) {
      sort(ALL(cand_diams[i]));
      if (SZ(cand_diams[i]) > 2) {
        cand_diams[i].resize(2);
      }
    }
    
    ReattachHandles(main_diam, handles);
    ps = FromGathered(gather);
    drz = Drzewko(base);
    REP (i, SZ(base)) {
      debug(base[i].backbone, base[i].max_pref, base[i].max_suf, base[i].diam);
    }
  }
  ~OrientedDS3() {
    REP (i, SZ(rooted)) {
      if (rooted[i]) {
        delete rooted[i];
        rooted[i] = nullptr;
      }
    }
  }
  

  
  bool Solve(int a, int b, int c) { // not necessarilly sorted, go from left to right
    //debug(a, b, c);
    if (c <= diam_len && a <= big1 && b <= big2) {
      //debug("big1 big2");
      return true;
    }
    if (MajorizedBySet(a, b, ps) || MajorizedBySet(b, a, ps)) {
      //debug("unr");
      return true;
    }
    
    PrefInfo lewo = processed_diam.GetFirstAtLeast(a);
    //debug(lewo);
    PrefInfo prawo = processed_diam.GetFromBack(c);
    assert(lewo.son != -1 && prawo.son != -1);
    // if (a.st == -1 || c.st == -1) { continue; }
    if (Clash(lewo, prawo)) { return false; }
    if (lewo.son) {
      if (rooted[lewo.son]->Solve(a - main_diam[lewo.id].len, b)) {
        //debug(a - lewo.len, b);
        //cerr<<"rooted w "<<lewo.son<<"\n";
        return true;
      }
      PrefInfo sec = processed_diam.GetSecondAtLeast(a);
      //debug(sec, prawo, Clash(sec, prawo), best_diam_at[lewo.id]);
      if (sec.len != -1 && !Clash(sec, prawo) && best_diam_at[lewo.id] >= b) {
        //cerr<<"w synie "<<lewo.son<<"\n";
        return true;
      }
    }
    int max_ask = -1;
    if (lewo.son == 0) {
      max_ask = prawo.id;
      if (prawo.son) { max_ask--; }
    } else {
      max_ask = lewo.id - 1;
    }
    if (max_ask >= 0 && processed_diam.max_pref_diam[max_ask] >= b) {
      cerr<<"dlugie diam gdzies\n";
      return true;  
    }
    
    if (lewo.son) {
      for (auto cand : cand_diams[lewo.id]) {
        if (cand.son != lewo.son && cand.diam >= b) {
          cerr<<"akurat pod tym samym "<<main_diam[lewo.id].v<<"\n";
          return true;
        }
      }
    }
    
    debug(a, c);
    debug(lewo.len, lewo.id, lewo.son);
    debug(prawo.len, prawo.id, prawo.son);
    int L = 2 * lewo.id;
    if (lewo.son == 0) { L--; }
    int R = 2 * prawo.id - 2;
    if (prawo.son == 0) { R++; }
    int rd = drz.Read(L, R);
    if (rd >= b) {
      cerr<<"z drzewka Read("<<L<<", "<<R<<")="<<rd<<"\n";
      return true;
    }
    return false;
  }
};

struct UnorientedDS3 {
  OrientedDS3 fir, sec;
  UnrootedDS2 ur2;
  vector<DiamInfo> diam;
  int diam_len;
  UnorientedDS3(int v) {
    diam = PathToFurthest(v);
    v = diam.back().v;
    diam = PathToFurthest(v);
    fir = OrientedDS3(diam);
    diam_len = diam.back().len;
    reverse(ALL(diam));
    for (auto& p : diam) {
      p.len = diam_len - p.len;
    }
    sec = OrientedDS3(diam);
    ur2 = UnrootedDS2(v);
  }
  bool Solve(int a, int b, int c) {
    debug(a, b, c);
    debug(diam_len);
    if (a + b + c <= diam_len) { return true; }
    if (max(a, max(b, c)) > diam_len) { return false; }
    if (ur2.Solve(a + b, c)) { return true; }
    if (ur2.Solve(b + c, a)) { return true; }
    if (ur2.Solve(c + a, b)) { return true; }
    debug("idzie do trojek");
    VI per{a, b, c};
    do {
      if (fir.Solve(per[0], per[1], per[2]) || sec.Solve(per[0], per[1], per[2])) { return true; }
    } while (next_permutation(ALL(per)));
    return false;
  }
};

// DS fir, sec;
// 
// set<PII> outside_diam_ps; // TODO
// int first_biggest_outside_diam; // TODO
// int second_biggest_outside_diam; // TODO
// int Solve3(int a, int b, int c) {
//   if (c > diam) { return false; }
//   if (b <= first_biggest_outside_diam && a <= second_biggest_outside_diam) { return true; }
//   if (MajorizedBySet(a, b, outside_diam_ps)) { return true; }
//   
//   return false;
// }


int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  int q;
  cin>>n>>q;
  orig_n = n;
  slo.resize(n + 2);
  RE (i, n - 1) {
    int a, b, w;
    cin>>a>>b>>w;
    slo[a].PB({b, w});
    slo[b].PB({a, w});
  }
  UnorientedDS3 ds(1);
  RE (_, q) {
    int a, b, c;
    cin>>a>>b>>c;
    //if (Solve1(a + b + c) || Solve2(a + b, c) || Solve2(a + c, b) || Solve2(b + c, a) || Solve3(a, b, c)) {
    if (ds.Solve(a, b, c)) {
      cout<<"TAK\n";
    } else {
      cout<<"NIE\n";
    }
  }
  
  return 0;
}