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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iosfwd>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <iostream>
#include <limits>
using namespace std;

class Rational
{
    long long p;
    long long q;

    constexpr Rational& simplify();

public:
    constexpr Rational(long long p = 0, long long q = 1);
    constexpr Rational(const Rational&);

    // assignment operators
    constexpr Rational& operator=(const Rational&);
    constexpr Rational& operator+=(const Rational&);
    constexpr Rational& operator-=(const Rational&);
    constexpr Rational& operator*=(const Rational&);
    constexpr Rational& operator/=(const Rational&);

    // arithmetic operators
    constexpr Rational operator+(const Rational&) const;
    constexpr Rational operator-(const Rational&) const;
    constexpr Rational operator*(const Rational&) const;
    constexpr Rational operator/(const Rational&) const;
    constexpr Rational operator+(long long x) const;

    // comparison operators
    constexpr bool operator==(const Rational&) const;
    constexpr bool operator<(const Rational&) const;
    constexpr bool operator<(long long x) const;
    constexpr bool operator>(const Rational&) const;
    constexpr bool operator>(long long x) const;
    constexpr bool operator>=(const Rational&) const;

    // increment and decrement operators
    constexpr Rational operator++(int);
    constexpr Rational operator--(int);
    constexpr Rational& operator++();
    constexpr Rational& operator--();

    // type conversion
    constexpr explicit operator double();

    // stream operators
    friend std::ostream& operator<<(std::ostream&, const Rational&);
    friend std::istream& operator>>(std::istream&, Rational&);

    // arithmetic functions
    constexpr Rational pow(unsigned exp) const;
    constexpr Rational inverse() const;
};

constexpr Rational operator""_r(unsigned long long p)
{
    return p;
}

namespace {
    constexpr long long gcd(long long p, long long q)
    {
        return q ? gcd(q, p%q) : p;
    }
}

constexpr Rational::Rational(long long p, long long q):p{p}, q{q} {}

constexpr Rational::Rational(const Rational& rat)
    : Rational{rat.p, rat.q} {}

constexpr Rational& Rational::simplify()
{
    if (q < 0) {
        p = -p;
        q = -q;
    }

    // Reduce by greatest common divisor
    const auto denom = gcd(p, q);
    p /= denom;
    q /= denom;

    return *this;
}

constexpr Rational& Rational::operator=(const Rational& rat)
{
    p = rat.p;
    q = rat.q;
    return *this;
}

constexpr Rational& Rational::operator+=(const Rational& rat)
{
    p = p * rat.q + q * rat.p;
    q *= rat.q;
    return simplify();
}

constexpr Rational& Rational::operator-=(const Rational& rat)
{
    p = p * rat.q - q * rat.p;
    q *= rat.q;
    return simplify();
}

constexpr Rational& Rational::operator*=(const Rational& rat)
{
    p *= rat.p;
    q *= rat.q;
    return simplify();
}

constexpr Rational& Rational::operator/=(const Rational& rat)
{
    if (rat.p == 0)
        throw std::domain_error{"Division by zero not allowed"};
    return *this *= rat.inverse();
}

constexpr Rational Rational::operator+(const Rational& rat) const
{
    return Rational{*this} += rat;
}

constexpr Rational Rational::operator+(long long x) const
{
    return Rational{*this} += Rational(x);
}

constexpr Rational Rational::operator-(const Rational& rat) const
{
    return Rational{*this} -= rat;
}

constexpr Rational Rational::operator*(const Rational& rat) const
{
    Rational result(*this);

    result *= rat;
    result.simplify();

    return result;
}

constexpr Rational Rational::operator/(const Rational& rat) const
{
    Rational result(*this);

    result /= rat;
    result.simplify();

    return result;
}

constexpr bool Rational::operator==(const Rational& rat) const
{
    return p == rat.p && q == rat.q;
}

constexpr bool Rational::operator<(const Rational& rat) const
{
    return p * rat.q  <  q * rat.p;
}

constexpr bool Rational::operator<(long long x) const
{
    return p  <  q * x;
}

constexpr bool Rational::operator>(long long x) const
{
    return p > q * x;
}

constexpr bool Rational::operator>(const Rational& rat) const
{
    return p * rat.q  >  q * rat.p;
}

constexpr bool Rational::operator>=(const Rational& rat) const
{
    return p * rat.q  >= q * rat.p;
}


constexpr Rational Rational::operator++(int) // Postfix
{
    Rational temp{*this};
    p += q;
    return temp;
}

constexpr Rational Rational::operator--(int) // Postfix
{
    Rational temp{*this};
    p -= q;
    return temp;
}

constexpr Rational& Rational::operator++()
{
    return *this += 1;
}

constexpr Rational& Rational::operator--()
{
    return *this -= 1;
}

constexpr Rational::operator double()
{
    return static_cast<double>(p) / q;
}


std::ostream& operator<<(std::ostream& os, const Rational& rat)
{
    return os << rat.p << ":" << rat.q;
}

std::istream& operator>>(std::istream& is, Rational& rat)
{
    long long p, q;
    char sep;
    if (is >> p >> sep >> q && sep == ':')
        rat = {p, q};
    return is;
}

constexpr Rational Rational::pow(unsigned exp) const
{
    auto x = *this;
    Rational r{1};
    for (;  exp;  exp /= 2) {
        if (exp%2) r *= x;
        x *= x;
    }
    return r;
}

constexpr Rational Rational::inverse() const
{
    return {q, p};
}

bool solve() {
  long n;
  vector<pair<Rational, Rational>> before;
  vector<pair<Rational, Rational>> after;
  long long e_before = 0;
  long long e_after = 0;
  scanf("%ld", &n);
  for (int i=0; i<n; i++) {
    long l, a, b;
    scanf("%ld %ld %ld\n", &l, &a, &b);
    before.push_back(make_pair(Rational(a), Rational(l)));
    after.push_back(make_pair(Rational(b), Rational(l)));
    e_before += a*l;
    e_after += b*l;
  }
  if (e_after != e_before) {
    return false;
  }

  sort(before.begin(), before.end());
  sort(after.begin(), after.end());

  if (before[0].first > after[0].first) { 
    //printf("no because 0\n");
    return false;
  }
  if (before[n-1].first < after[n-1].first) {
    //printf("no because n-1\n");
    return false;
  }
 // cout << endl;
  int j = n-1;
  Rational hot_volume(0);
  Rational hot_temp(0);
  Rational zero(0);
  for (int i=n-1; i>=0; i--) {
 //   cout << "i=" << i <<endl;
 //   cout << "hot_volume=" << hot_volume << endl;
//    cout << "before[j].first == after[i].first=" << (before[j].first == after[i].first) << endl;
    if ((hot_volume == zero) && (before[j].first == after[i].first)) {
    //  cout << "branch1" <<endl;
      if (before[j].second == after[i].second) {
     //   cout << "branch1x" <<endl;
        j --;
      } else if (before[j].second >= after[i].second) {
   //     cout << "branch1a" <<endl;
        before[j].second -= after[i].second;
      } else {
    //    cout << "branch1b" <<endl;
        after[i].second -= before[j].second;
        j --;
      }
    } else {
      //cout << "branch2" <<endl;
      Rational hot_volume_test = hot_volume + before[j].second;
      Rational hot_temp_test = (hot_volume*hot_temp + before[j].second * before[j].first) / hot_volume_test;
  //    cout << "i=" << i << " " << hot_volume_test << " " << hot_temp_test << endl;
      while (hot_temp_test > after[i].first) {
        j --;
        if (j < 0) {
          return false;
       //   cout << "hot_temp_test=" << hot_temp_test << endl;
       //   cout << "after[i].first=" << after[i].first << endl;
        }
        hot_volume = hot_volume_test;
        hot_temp = hot_temp_test;
        hot_volume_test = hot_volume + before[j].second;
  //      cout << "j=" << j << ", before[j].second=" << before[j].second <<endl;
  //      cout << "hot_volume=" << hot_volume << ", hot_volume_test=" << hot_volume_test <<endl;
        hot_temp_test = (hot_volume*hot_temp + before[j].second * before[j].first) / hot_volume_test;
   //     cout << "hot_temp_test=" << hot_temp_test <<endl;
      }
 //     cout << "hot_volume_test=" << hot_volume_test << ", hot_temp_test=" << hot_temp_test <<endl;
  //    cout << "hot_volume=" << hot_volume << ", hot_temp=" << hot_temp <<endl;
//      cout << "after[i].first=" << after[i].first << ", before[j].first=" <<before[j].first <<endl;
      Rational volume_to_make_temp_balance = hot_volume * (hot_temp - after[i].first) / (after[i].first - before[j].first);
 //     cout << "volume_to_make_temp_balance=" << volume_to_make_temp_balance << endl;
      if (volume_to_make_temp_balance > before[j].second) {
        return false;
      }
   //   cout << "continue1" << endl;
      if (volume_to_make_temp_balance + hot_volume < after[i].second) {
        return false;
      }
     // cout << "continue2" << endl;
      hot_temp = after[i].first;
      hot_volume =  (hot_volume + volume_to_make_temp_balance) - after[i].second;
   //   cout << "final: " << "hot_volume=" << hot_volume << ", hot_temp=" << hot_temp <<endl;
      before[j].second -= volume_to_make_temp_balance;
 //     cout << "final: " << "j=" << j << ", before[j].second=" << before[j].second <<endl;
    }
  }

  return true;
}



int main() {
  long t;
  scanf("%ld\n", &t);
  for(int i=0; i<t; i++) {
    bool b = solve();
    if (b) {
      printf("TAK\n");
    } else {
      printf("NIE\n");
    }
  }

  return 0;
}