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

using ll = long long;

void solve_one() {
    int n;
    cin >> n;
    
    vector<pair<ll, ll>> cur(n), want(n);

    for (int i = 0; i < n; i++) {
        ll l, a, b;
        cin >> l >> a >> b;

        cur[i] = make_pair(a, l);
        want[i] = make_pair(b, l);
    }

    sort(cur.begin(), cur.end());
    sort(want.begin(), want.end());
    
    int i = 0, j = 0;
    ll sum_cur = 0, sum_want = 0;
    while (i < n && j < n) {
        ll l_cur = cur[i].second;
        ll l_want = want[j].second;
        
        ll l = min(l_cur, l_want);

        sum_cur += cur[i].first * l;
        sum_want += want[j].first * l;

        if (sum_cur > sum_want) {
            cout << "NIE\n";
            return;
        }

        cur[i].second -= l;
        if (cur[i].second == 0) {
            i++;
        }
        
        want[j].second -= l;
        if (want[j].second == 0) {
            j++;
        }
    }

    if (sum_cur == sum_want) {
        cout << "TAK\n";
    }
    else {
        cout << "NIE\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);

    int t;
    cin >> t;

    while (t--) {
        solve_one();
    }
}