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
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

const string NIE = "NIE";
const string TAK = "TAK";

struct kubek {
    ll l, a, b;

    bool operator< (const kubek& o) const {
        return a < o.a;
    }
};

struct kubek2 {
    ll l, a, b;

    bool operator< (const kubek2& o) const {
        return b < o.b;
    }
};

void st() {
    int n;
    cin >> n;
    ll bilans = 0;
    vector<kubek> d;
    vector<kubek2> g;
    FOR(i,0,n) {
        ll a, b, c;
        cin >> a >> b >> c;
        bilans += a * (b - c);
        if (b < c) d.PB({a, b, c});
        if (b > c) g.PB({a, b, c});
    }
    if (bilans != 0){
        //cerr << "Bo bilans zly." << endl;
        cout << NIE << endl;
        return;
    }
    sort(ALL(d));
    sort(ALL(g));
    ll pe = 0, se = 0;
    FOR(i,0,g.size()) pe += g[i].l * (g[i].a - g[i].b);
    ll we = 0;
    int wsk = 0;
    ll pt = 0, ce;
    FOR(i,0,d.size()){
        ce = d[i].l * (d[i].b - d[i].a);
        we += ce;
        while(wsk < g.size() && g[wsk].b < d[i].a) wsk++;
        se += (d[i].a - pt) * wsk;
        pt = d[i].a;
        if (we < se || ce > pe - se){
            cout << NIE << endl;
            return;
        }
    }
    cout << TAK << endl;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        st();
    }
}