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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

const int N = 100100;
pii a[N], b[N];

bool test() {
	int n;
	scanf("%d", &n);
	FOR(i,n) {
		int tl,ta,tb;
		scanf("%d%d%d", &tl, &ta, &tb);
		a[i] = mp(ta,tl);
		b[i] = mp(tb,tl);
	}
	sort(a,a+n);
	sort(b,b+n);
	int j = 0;
	long long energy = 0, units = 0;
	FOR(i,n) {
		while (j < n && (energy + 1LL * a[i].fi * a[i].se) > 1LL * b[j].fi * (units + a[i].se)) {
			if (1LL * b[j].se * (a[i].fi-b[j].fi) > units*a[i].fi-energy) return false;
			energy -= 1LL * b[j].fi * b[j].se;
			units -= 1LL * b[j].se;
			j++;
		}
		energy += 1LL * a[i].fi * a[i].se;
		units += a[i].se;
	}
	return true;
}

int main() {
	int t;
	scanf("%d", &t);
	FOR(i,t) printf("%s\n", test() ? "TAK" : "NIE");
	return 0;
}