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
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef long long ll;

bool solve() {
    int n;
    cin >> n;
    ll pa =0, pb = 0;
    ll cntall = 0;
    vector<pair<ll,ll> > A(n),B(n);
    for(int i=0;i<n;i++) {
        int l,a,b;
        cin >> l >> a >> b;
        cntall += l;
        pa += l*a;
        pb += l*b;
        A[i] = {a,l};
        B[i] = {b,l};
    }
    if(pa != pb) {
        return false;
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    ll cntl = 0,suml = 0, cntr=0,sumr = 0;
    for(int i=0;i<n;i++) {
        sumr += A[i].st*A[i].nd;
        cntr += A[i].nd;
    }

    int j = 0;
    for(int i=0;i<n;i++) {
        if(B[i].st*cntall <= pa) {
            while(j < n && A[j].st <= B[i].st) {
                sumr -= A[j].st*A[j].nd;
                cntr -= A[j].nd;

                suml += A[j].st*A[j].nd;
                cntl += A[j].nd;
                j++;
            }
        }
        else {
            while(j < n && A[j].st < B[i].st) {
                sumr -= A[j].st*A[j].nd;
                cntr -= A[j].nd;

                suml += A[j].st*A[j].nd;
                cntl += A[j].nd;
                j++;
            }
        }
        
        if(B[i].st*cntall <= pa) {
            if((B[i].nd-cntl) > (B[i].st*cntl - suml)*(long double)cntr/(sumr-B[i].st*cntr)) {
                return false;
            }
        }
        else {
            if((B[i].nd-cntr) > (sumr - B[i].st*cntr)*(long double)cntl/(B[i].st*cntl-suml)) {
                return false;
            }
        }
    }
    return true;
}

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--) {
        if(solve())
            cout<<"TAK\n";
        else 
            cout<<"NIE\n";
    }
}