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

#define st first
#define nd second
#define pb push_back

typedef long long   ll;
typedef double      db;
typedef long double ldb;

typedef pair<int, int> pii;
typedef pair<ll,  ll>  pll;
typedef pair<ll,  db>  pdd;
typedef pair<ldb, ldb> pldd;
typedef pair<int, ll>  pil;
typedef pair<ll, int>  pli;

typedef vector<int>  vi;
typedef vector<ll>   vl;
typedef vector<db>   vdb;
typedef vector<ldb>  vldb;
typedef vector<pii>  vpii;
typedef vector<pll>  vpll;
typedef vector<pdd>  vpdd;
typedef vector<pldd> vpldd;
typedef vector<pil>  vpil;
typedef vector<pli>  vpli;

template <typename T>
void   Debug (T begin, T end);
int    si    ();
ll     sl    ();
string ss    (int);
float  sf    ();
db     sd    ();
ldb    sld   ();
void   pi    (int, char = '\n');
void   pl    (ll, char = '\n');
void   pf    (float, char = '\n');
void   pdb   (db, char = '\n');
void   pldb  (ldb, char = '\n');
void   ps    (string, char = '\n');

ll russian(ll, ll, ll);
ll fastpow(ll, ll, ll);
ll slowpow(ll, ll, ll);
ll _gcd(ll, ll);
ll _lcm(ll, ll);
ll extgcd(ll, ll, ll&, ll&);
ll _inv(ll, ll);

const int INF  = 1e9;
const ll  LINF = 1e18;

struct Tee
{
    ll l;
    int a, b;

    bool operator<(const Tee& t) const
    {
        return a < t.a;
    }
};

const int N = 1e6 + 7;
Tee tees[N];
int n;
ll sum;

void read()
{
    n = si();
    for (int i = 0; i < n; ++i)
    {
        ll l = si();
        int a = si(), b = si();
        tees[i] = {l, a, b};
        sum += ll(l) * (a - b);
    }
}

bool solve()
{
    if (sum != 0)
        return false;

    sort(tees, tees + n);



    for (int i = 0; i < n; ++i)
    {
        Tee &t1 = tees[i];

        ll w = 0;
        ll e = 0;

        int it = n - 1;
        while (it >= 0 && tees[it].a >= tees[it].b)
        {
            w += tees[it].l;
            e += tees[it].l * tees[it].a;
            --it;
        }
        while (it >= 0 && w < t1.l)
        {
            ll need = t1.l - w;
            ll have = tees[it].l;
            ll give = min(need, have);
            w += give;
            e += give * tees[it].a;
            --it;
        }

        if (w < t1.l || e < t1.b * w)
            return false;
    }

    return true;
}

void reset()
{
    sum = 0;
}

int main()
{
    int t = si();

    while (t--)
    {
        read();
        if (solve())
            ps("TAK");
        else
            ps("NIE");
        reset();
    }

    return 0;
}

template <typename T>
void Debug(T begin, T end)
{
    printf("\nDEBUG:\n");
    while (begin != end)
        printf("%d ", *begin),
        begin++;
    printf("\n\n");
}

template <typename T>
T scanf_t(string s)
{
    T a;
    scanf(s.c_str(), &a);
    return a;
}

template <typename T>
void printf_t(T a, string s, char end_l)
{
    s.push_back(end_l);
    printf(s.c_str(), a);
}

int   si   ()  { return scanf_t<int>("%d");   }
ll    sl   ()  { return scanf_t<ll>("%lld");  }
float sf   ()  { return scanf_t<float>("%f"); }
db    sdb  ()  { return scanf_t<db>("%f");    }
ldb   sldb ()  { return scanf_t<ldb>("%Lf");  }

void pi   (int a,   char end_l)  { printf_t<int>(a, "%d", end_l);   }
void pl   (ll a,    char end_l)  { printf_t<ll>(a, "%lld", end_l);  }
void pf   (float a, char end_l)  { printf_t<float>(a, "%f", end_l); }
void pdb  (db a,    char end_l)  { printf_t<db>(a, "%lf", end_l);   }
void pldb (ldb a,   char end_l)  { printf_t<ldb>(a, "%Lf", end_l);  }

void ps(string a, char end_l)
{
    string s = "%s";
    s.push_back(end_l);
    printf(s.c_str(), a.c_str());
}

string ss(int NN)
{
    char T[NN + 7];
    scanf("%s", T);
    return (string)T;
}

ll fastpow(ll a, ll k, ll m)
{
    ll r = 1LL;
    while (k)
    {
        if (k & 1LL)
            r = (r * a) % m;
        a = (a * a) % m;
        k >>= 1LL;
    }
    return r;
}

ll slowpow(ll a, ll k, ll m)
{
    ll r = 1LL;
    while (k)
    {
        if (k & 1LL)
            r = russian(r, a, m);
        a = russian(a, a, m);
        k >>= 1LL;
    }
    return r;
}

ll russian(ll a, ll k, ll m)
{
    ll r = 0LL;
    while (k)
    {
        if (k & 1LL)
            r = (r + a) % m;
        a = (a + a) % m;
        k >>= 1LL;
    }
    return r;
}

ll _gcd(ll a, ll b)
{
    while (b)
        swap(a %= b, b);
    return a;
}

ll _lcm(ll a, ll b)
{
    return a / _gcd(a, b) * b;
}

ll extgcd(ll a, ll b, ll& k, ll& l)
{
    if (b == 0)
    {
        k = 1; l = 0;
        return a;
    }
    ll res = extgcd(b, a % b, l, k);
    l -= a / b * k;
    return res;
}

ll _inv(ll a, ll p)
{
    return fastpow(a, p - 2, p);
}