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

using namespace std;

bool sortbysec(const pair<int,int> &a, const pair<int,int> &b){ 
    return (a.second < b.second); 
} 

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

    long long int t;
    cin>>t;

    for(int sth = 0; sth < t; sth++){
        long long int n;
        cin>>n;
        vector< pair <long long int, long long int> > a_array;
        vector< pair <long long int, long long int> > b_array;
        
        for(long long int i = 0; i < n; i ++){
            long long int l, a, b;
            cin>>l>>a>>b;

            a_array.push_back(make_pair(l, a));
            b_array.push_back(make_pair(l, b));
        }

        sort(a_array.begin(), a_array.end(), sortbysec);
        sort(b_array.begin(), b_array.end(), sortbysec);

        long long int a_temp = 0, b_temp = 0;
        long long int a_litres = 0, b_litres = 0;

        bool flag = true;

        while(b_array.size() > 0){
            long long int b = b_array.back().second;
            long long int l_b = b_array.back().first;

            b_array.pop_back();

            b_temp = (b_temp + b * l_b);
            b_litres += l_b;

            while(l_b > 0){
                long long int a = a_array.back().second;
                long long int l_a = a_array.back().first;

                a_array.pop_back();
                
                if(l_b <= l_a){
                    a_temp = (a_temp + a * l_b);
                    a_litres += l_b;

                    l_a -= l_b;
                    l_b = 0;

                    a_array.push_back(make_pair(l_a, a));
                }
                else{
                    a_temp = (a_temp + a * l_a);
                    a_litres += l_a;

                    l_b -= l_a;
                }
            }
            if(b_temp > a_temp){
                flag = false;
                break;
            }
        }

        if(flag && a_temp == b_temp){
            cout<<"TAK"<<endl;
        }
        else{
            cout<<"NIE"<<endl;
        }

    }   


    return 0;
}