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

using namespace std;


void test() {
  int n;
  cin >> n;
  
  vector< pair<int, int> > avl;
  vector< pair<int, int> > req;
  
  int64_t int_avl = 0;
  int64_t int_req = 0;
  
  for(int i = 0; i < n; i++) {
    int l, a, b;
    cin >> l >> a >> b;
    avl.push_back(make_pair(a, l));
    req.push_back(make_pair(b, l));
    
    int_avl += l * int64_t(a);
    int_req += l * int64_t(b);
  }
  
  if(int_avl != int_req) {
    cout << "NIE\n";
    return;
  }
  
  sort(avl.begin(), avl.end());
  sort(req.begin(), req.end());
  
  {
    int cold_i = 0;
    int64_t cold_vol = 0, cold_t_num = 0, cold_t_den = 0;
    
    int64_t vol = 0, t_num = 0, t_den = 0;
    for(int i = 0; i < n; i++) {
      vol += req[i].second;
      t_num += req[i].second * req[i].first;
      t_den += req[i].second;
      
      while(cold_i < n and cold_vol + avl[cold_i].second <= vol) {
        cold_vol += avl[cold_i].second;
        cold_t_num += avl[cold_i].second * avl[cold_i].first;
        cold_t_den += avl[cold_i].second;
        cold_i++;
      }
      
      int64_t tmp_vol = cold_vol, tmp_t_num = cold_t_num, tmp_t_den = cold_t_den;
      
      if(tmp_vol < vol) {
        int64_t new_vol = vol - tmp_vol;
        tmp_vol += new_vol;
        tmp_t_num += new_vol * avl[cold_i].first;
        tmp_t_den += new_vol;
      }
      
      if(tmp_t_num * t_den > t_num * tmp_t_den) {
        cout << "NIE\n";
        return;
      }
    }
  }
  
  {
    int hot_i = n - 1;
    int64_t hot_vol = 0, hot_t_num = 0, hot_t_den = 0;
    
    int64_t vol = 0, t_num = 0, t_den = 0;
    for(int i = n; i --> 0;) {
      vol += req[i].second;
      t_num += req[i].second * req[i].first;
      t_den += req[i].second;
      
      while(hot_i >= 0 and hot_vol + avl[hot_i].second <= vol) {
        hot_vol += avl[hot_i].second;
        hot_t_num += avl[hot_i].second * avl[hot_i].first;
        hot_t_den += avl[hot_i].second;
        hot_i--;
      }
      
      int64_t tmp_vol = hot_vol, tmp_t_num = hot_t_num, tmp_t_den = hot_t_den;
      
      if(tmp_vol < vol) {
        int64_t new_vol = vol - tmp_vol;
        tmp_vol += new_vol;
        tmp_t_num += new_vol * avl[hot_i].first;
        tmp_t_den += new_vol;
      }
      
      if(tmp_t_num * t_den < t_num * tmp_t_den) {
        cout << "NIE\n";
        return;
      }
    }
  }
  
  cout << "TAK\n";
}


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int t;
  cin >> t;
  while(t --> 0) test();
  
  return 0;
}