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

using namespace std;

using ll = long long int;
using pll = pair<ll, ll>;

bool testcase() {
    int n;
    scanf("%d", &n);
    vector<pll> av;
    vector<pll> bv;
    for (int i = 1; i <= n; i++) {
        int l, a, b;
        scanf("%d%d%d", &l, &a, &b);
        av.push_back(make_pair(a, l));
        bv.push_back(make_pair(b, l));
    }
    sort(av.begin(), av.end());
    sort(bv.begin(), bv.end());
    ll bal = 0;
    int ptra = 0;
    int ptrb = 0;
    ll vola = av[0].second;
    ll volb = bv[0].second;
    while (ptra < n && ptrb < n) {
        ll am = min(vola, volb);
        bal += am * (bv[ptrb].first - av[ptra].first);
        if (bal < 0) return false;
        vola -= am;
        volb -= am;
        if (vola == 0) {
            ptra++;
            if (ptra < n) vola = av[ptra].second;
        }
        if (volb == 0) {
            ptrb++;
            if (ptrb < n) volb = bv[ptrb].second;
        }
    }
    return true;
}

int main() {
    int $;
    scanf("%d", &$);
    while ($--) {
        puts(testcase() ? "TAK" : "NIE");
    }
}