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
#include <stdio.h>
#include <vector>
#include <map>
#include <unordered_set>
#include <queue>
#include <unordered_map>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <functional>
#include <iterator>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

// <temperature, amount>
vector<pair<ll,ll>> exists;
vector<pair<ll,ll>> needs;

bool additional_check_isok() {
  int n_idx = 0;
  int e_idx = 0;

  ll need_temp = 0;
  ll need_amount = 0;

  ll exists_temp = 0;
  ll exists_amount = 0;

  bool check = true;
  while (n_idx < needs.size() || e_idx < exists.size()) {
    if (check) {
      if (exists[e_idx] > needs[n_idx]) {
        return false;
      }
      check = false;
    }

    if (e_idx < exists.size() && (n_idx == needs.size() || exists[e_idx].first < needs[n_idx].first)) {
      exists_temp += exists[e_idx].first * exists[e_idx].second;
      exists_amount += exists[e_idx].second;
      e_idx++;
      if (need_temp == exists_temp && need_amount == exists_amount) {
        check = true;
      }
    } else { //must be >
      // expected temp b bigger than current temp + 1
      need_temp += needs[n_idx].first * needs[n_idx].second;
      need_amount += needs[n_idx].second;
      n_idx++;
      if (need_temp == exists_temp && need_amount == exists_amount) {
        return false;
      }
      ll b_amount = need_amount - exists_amount;
      if (b_amount > 0) {
        float tb = (1.0f * need_temp / need_amount * (exists_amount + b_amount) - exists_amount * (1.0f * exists_temp / exists_amount)) / b_amount;
        if (tb < exists[e_idx].first - 1) {
          return false;
        }
      }
    }
  }

  return need_temp == exists_temp;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);


  int T;
  cin >> T;
  while (T --> 0) {
    int n;
    ll li, ai, bi;
    cin >> n;

    exists.clear();
    needs.clear();

    vector<pair<ll,ll>> e1(n), e2;
    vector<pair<ll,ll>> n1(n), n2;

    for (int i=0; i<n; ++i) {
      cin >> li >> ai >> bi;
      e1[i] = mp(ai, li);
      n1[i] = mp(bi, li);
    }

    sort(e1.begin(), e1.end());
    sort(n1.begin(), n1.end());

    for (auto p: e1) {
      if (e2.size() > 0 && e2.back().first == p.first) {
        e2.back().second += p.second;
      } else {
        e2.pb(p);
      }
    }

    for (auto p: n1) {
      if (n2.size() > 0 && n2.back().first == p.first) {
        n2.back().second += p.second;
      } else {
        n2.pb(p);
      }
    }

    int n_idx = 0;
    int e_idx = 0;
    while (n_idx < n2.size() || e_idx < e2.size()) {
      if (n_idx == n2.size()) { exists.pb(e2[e_idx++]); }
      else if (e_idx == e2.size()) { needs.pb(n2[n_idx++]); }
      else {
        if (n2[n_idx].first < e2[e_idx].first) { needs.pb(n2[n_idx++]); }
        else if (n2[n_idx].first > e2[e_idx].first) { exists.pb(e2[e_idx++]); }
        else {
          if (n2[n_idx].second < e2[e_idx].second) {
            exists.pb(mp(e2[e_idx].first, e2[e_idx].second - n2[n_idx].second));
            n_idx++;
            e_idx++;
          } else if (n2[n_idx].second > e2[e_idx].second) {
            needs.pb(mp(n2[n_idx].first, n2[n_idx].second - e2[e_idx].second));
            n_idx++;
            e_idx++;
          } else {
            n_idx++;
            e_idx++;
          }
        }
      }
    }

    bool possible = additional_check_isok();

    if (possible) {
        cout << "TAK" << endl;
    } else {
        cout << "NIE" << endl;
    }
  }

  return 0;
}