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
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ss(x) (int) x.size()
#define fi first
#define se second
#define cat(x) cout << #x << " = " << x << endl

using namespace std;

const int nax = 1e5 + 111;
pair <int,int> A[nax], B[nax];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int z;
	cin >> z;
	while (z--){
		int n;
		cin >> n;
		for (int i = 1; n >= i; ++i){
			cin >> A[i].se >> A[i].fi >> B[i].fi;
			B[i].se = A[i].se;
		}
		
		sort(A+1, A+n+1);
		reverse(A+1, A+n+1);
		
		sort(B+1, B+n+1);
		reverse(B+1, B+n+1);
		
		ll pole = 0;
		int i = 1, j = 1;
		while(i <= n){
			
			int sze = min(A[i].se, B[j].se);
			
			if(A[i].fi > B[j].fi){
				int wys = A[i].fi - B[j].fi;
				pole += 1ll * sze * wys;
			}else{
				int wys = B[j].fi - A[i].fi;
				pole -= 1ll * sze * wys;
				if (pole < 0){
					break;
				}
			}
			
			A[i].se -= sze;
			if(A[i].se == 0)
				i++;
			
			B[j].se -= sze;
			if(B[j].se == 0)
				j++;
		}
		if(i<=n)
			cout << "NIE\n";
		else if(pole == 0)
			cout << "TAK\n";
		else
			cout << "NIE\n";
	}
	return 0;
}