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

using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int> > want;
    vector<pair<int, int> > have;
    for (int i = 0; i < n; i++) {
        int l, a, b;
        cin >> l >> a >> b;
        have.push_back({a,l});
        want.push_back({b,l});
    }

    sort(want.begin(), want.end());
    sort(have.begin(), have.end());

    long long extra = 0;
    int j = 0;
    auto curr = have[j];
    for (auto p : want) {
        while (true) {
            if (curr.second == 0) {
                j++;
                curr = have[j];
            }
            int vol = min(p.second, curr.second);
            p.second -= vol;
            curr.second -= vol;
            int diff = p.first - curr.first;
            if (diff >= 0) {
                long long bonus = 1LL * diff * vol;
                extra += bonus;
            } else {
                long long bonus = 1LL * diff * vol;
                extra += bonus;
                if (extra < 0) {
                    cout << "NIE\n";
                    return;
                }
            }
            if (p.second == 0) {
                break;
            }
        }
    }
    if (extra == 0) {
        cout << "TAK\n";
    } else {
        cout << "NIE\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cout.precision(15);
    cout << fixed;
    cin.tie(0);

    int t;
    cin >> t;
    for (int tst = 0; tst < t; tst++) {
        solve();
    }

    return 0;
}