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
#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<pair<int, int>> foo(n);
    vector<pair<int, int>> bar(n);
    for (int i = 0; i < n; ++i) {
      int l, a, b;
      cin >> l >> a >> b;
      foo[i] = make_pair(a, l);
      bar[i] = make_pair(b, l);
    }
    sort(foo.begin(), foo.end());
    sort(bar.begin(), bar.end());
    long long sum = 0;
    bool valid = true;
    int i = 0;
    int j = 0;
    while (i < n && j < n) {
      int take = min(foo[i].second, bar[j].second);
      foo[i].second -= take;
      bar[j].second -= take;
      sum += (long long) take * (foo[i].first - bar[j].first);
      if (sum > 0) {
        valid = false;
        break;
      }
      if (!foo[i].second) {
        ++i;
      }
      if (!bar[j].second) {
        ++j;
      }
    }
    if (sum) {
      valid = false;
    }
    cout << (valid ? "TAK" : "NIE") << "\n";
  }
  return 0;
}