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
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;

priority_queue<pair<int,int> > have;
priority_queue<pair<int,int> > want;

int main() {
	ios_base::sync_with_stdio(0);
	int tests, n, l, a, b;
	pair<int,int> w, h;
	cin >> tests;
	while(tests--) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> l >> a >> b;
			have.push(make_pair(-a,l));
			want.push(make_pair(-b,l));
		}
		long long int ws = 0, hs = 0; 
		long long int wa = 0, ha = 0;
		bool cant = false;
		while(!want.empty()) {
			w = want.top();
			want.pop();
			wa = wa + w.second;
			ws = ws - w.second*w.first;
			//cout << "W: " << wa << " " << ws << endl;
			while(ha < wa) {
				h = have.top();
				have.pop();
				if(h.second > wa-ha) {
					//cout << "delta: " << (wa-ha) << endl;
					//cout << h.first << " " << h.second << endl;
					int delta = (wa-ha);
					ha = ha + delta;
					hs = hs - delta*h.first;
					have.push(make_pair(h.first, h.second-delta));
					//cout << "H*: " << ha << " " << hs << endl;
				} else {
					ha = ha + h.second;
					hs = hs - h.second*h.first;
					//cout << "H: " << ha << " " << hs << endl;
				}
			}
			if(ws < hs) {
				cant = true;
			}
		}
		//cout << "FIN: " << ws << " " << hs << endl;
		if(ws != hs) {
			cant = true;
		}
		if(cant) {
			cout << "NIE\n";
		} else {
			cout << "TAK\n";
		}
		//cout << want.size() << endl;
		//cout << have.size() << endl;
		
	}
	return 0;
}