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

using namespace std;

#define int long long

#define PB push_back
#define EB emplace_back
#define fst first
#define snd second
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)a.size()
using PII = pair<int, int>;

const int MAXN = 100'005;

int roz[MAXN], pocz[MAXN], kon[MAXN];


void solve() {
	int n;
	cin >> n;
	vector<PII> dostep, oczek;
	int sum1 = 0, sum2 = 0;
	for (int i = 0; i < n; i++) {
		cin >> roz[i] >> pocz[i] >> kon[i];
		dostep.EB(pocz[i], roz[i]);
		oczek.EB(kon[i], roz[i]);
		sum1 += pocz[i] * roz[i];
		sum2 += kon[i] * roz[i];
	}
	//cerr << sum1 << " " << sum2 << endl;
	if (sum1 != sum2) {
		cout << "NIE" << endl;
		return;
	}
	sort(ALL(dostep));
	sort(ALL(oczek));
	int energia_jest = 0, energia_chce = 0, ilosc_jest = 0, ilosc_chce = 0;
	int it = 0, zabrane = 0;
	for (int i = 0; i < n; i++) {
		ilosc_chce += oczek[i].snd;
		energia_chce += oczek[i].snd * oczek[i].fst;
		while (ilosc_jest < ilosc_chce && dostep[it].snd - zabrane + ilosc_jest <= ilosc_chce) {
			int ile = dostep[it].snd - zabrane;
			energia_jest += ile * dostep[it].fst;
			ilosc_jest += ile;
			it++;
			zabrane = 0;
		}
		if (ilosc_jest < ilosc_chce) {
			int dobrac = ilosc_chce - ilosc_jest;
			ilosc_jest += dobrac;
			zabrane += dobrac;
			energia_jest += dobrac * dostep[it].fst;
		}
		if (energia_jest > energia_chce) {
			cout << "NIE" << endl;
			return;
		}
	}


	cout << "TAK" << endl;
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
}