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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int t, n, l_, a_, b_;
pair<int, int> a[1000002], b[1000002]; // a ~ {temp, vol}, b ~ {expected, vol}
int main() {
  ios::sync_with_stdio(false);

  cin >> t;
  while(t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> l_ >> a_ >> b_;
      a[i] = {a_, l_};
      b[i] = {b_, l_};
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    bool git = true;
    int vol = 0, vol_a = 0, curr_a = n;
    long long temp_sum = 0, temp_sum_a = 0;
    for (int i = n; i >= 1; i--) {
      vol += b[i].second;
      temp_sum += b[i].second*b[i].first;
      while (curr_a >= 1 && vol_a + a[curr_a].second <= vol) {
        vol_a += a[curr_a].second;
        temp_sum_a += a[curr_a].second*a[curr_a].first;
        curr_a--;
      }
      long long temp = (vol_a == vol) ? temp_sum_a : (temp_sum_a + (vol-vol_a)*a[curr_a].first);
      if (temp < temp_sum) {
        git = false;
        break;
      }
    }
    if (git == false) {
      cout << "NIE" << endl;
      continue;
    }
    vol = 0;
    vol_a = 0;
    curr_a = 1;
    temp_sum = 0;
    temp_sum_a = 0;
    for (int i = 1; i <= n; i++) {
      vol += b[i].second;
      temp_sum += b[i].second*b[i].first;
      while (curr_a <= n && vol_a + a[curr_a].second <= vol) {
        vol_a += a[curr_a].second;
        temp_sum_a += a[curr_a].second*a[curr_a].first;
        curr_a++;
      }
      long long temp = (vol_a == vol) ? temp_sum_a : (temp_sum_a + (vol-vol_a)*a[curr_a].first);
      if (temp > temp_sum) {
        git = false;
        break;
      }
    }
    if (git == false) {
      cout << "NIE" << endl;
    } else {
      cout << "TAK" << endl;
    }
  }


  return 0;
}