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
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

#define PLL pair<LL, LL>
// ******************************************************************************

#define no {puts("NIE");return;}
void solve()
  {
  int n;
  scanf("%d", &n);

  LL sum1 = 0, sum2 = 0;
  VPII mam, cel;
  FOR(i, 1, n)
    {
    int l, a, b;
    scanf("%d%d%d", &l, &a, &b);
    sum1 += (LL)l * a;
    sum2 += (LL)l * b;

    mam.PB(MP(a, l));
    cel.PB(MP(b, l));
    }
  if(sum1 != sum2)no;

  sort(ALL(mam));
  sort(ALL(cel));

  LL ile = mam.back().s;
  LL cieplo = mam.back().f * ile;
  mam.pop_back();

  LL cieplo2 = 0;
  LL ile2 = 0;
  
  while(cel.size())
    {
    LL temp_cel = cel.back().f;
    LL ile_cel = cel.back().s;
    LL cieplo_cel = temp_cel * ile_cel;
    // ile <= 1e6 * n

    if(ile <= 0 || cieplo > temp_cel * ile || 
                  (cieplo == temp_cel * ile && ile < ile_cel))
      {
      if(ile2 == 0)
        {
        if(mam.size() == 0)no;

        ile2 = mam.back().s;
        cieplo2 = mam.back().f * ile2;
 
        mam.pop_back();
        }

      LL cieplo_sum = (cieplo+cieplo2);
      LL ile_sum = ile+ile2;

      if(cieplo_sum >= temp_cel * ile_sum)
        {
        cieplo = cieplo_sum;
        ile = ile + ile2; 

        cieplo2 = ile2 = 0;
        }
      else
        {
        LL temp2 = cieplo2 / ile2;
        LL biore = (temp_cel*ile-cieplo) / (temp2-temp_cel); // chce podłogę

        if(ile + biore < ile_cel)no;

        cieplo -= cieplo_cel;
        ile -= ile_cel;
        cel.pop_back();
        }
      }
    else
      {
      if(cieplo != temp_cel * ile)no;

      cieplo -= cieplo_cel;
      ile -= ile_cel;
      if(ile < 0)no;
      cel.pop_back();
      }
    }

  puts("TAK");
  }

int main()
  {
  int z;
  scanf("%d", &z);
  while(z--)solve();
  }