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
#include<bits/stdc++.h>
using namespace std;
vector<pair<long long,long long>> v;
vector<pair<long long,long long>> v2;
void solve()
{
    int a;
    cin>>a;
    int b,c,d;
    long long suma=0;
    for(int x=0;x<a;x++)
    {
        cin>>b>>c>>d;
        suma+=b*c;
        suma-=b*d;
        v.push_back(make_pair(c,b));
        v2.push_back(make_pair(d,b));
    }
    sort(v.begin(),v.end());
    sort(v2.begin(),v2.end());
    if(suma!=0)
    {
        cout<<"NIE"<<'\n';
        v.resize(0);
        v2.resize(0);
        return;
    }
    int where1=0,where2=0;
    while(where1!=a&&where2!=a)
    {
        if(v[where1].second>v2[where2].second)
        {
            suma+=v2[where2].second*(-v[where1].first+v2[where2].first);
            v[where1].second-=v2[where2].second;
            where2++;
        }
        else
        {
            suma+=v[where1].second*(-v[where1].first+v2[where2].first);
            v2[where2].second-=v[where1].second;
            where1++;
        }
        if(suma<0)
        {
            cout<<"NIE"<<'\n';
            v.resize(0);
            v2.resize(0);
            return;
        }
    }
    cout<<"TAK"<<'\n';
    v.resize(0);
    v2.resize(0);
    return;

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