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
#include <vector>
#include <cstdio>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;

bool tcase() {
	int n;
  scanf("%d", &n);
	vector<int> l(n),a(n),b(n);
	long long sa = 0, sb = 0;
	for (int i = 0; i < n; i++) {
	  scanf("%d %d %d", &l[i], &a[i], &b[i]);
		sa += ((long long)l[i]) * a[i];
		sb += ((long long)l[i]) * b[i];
	}
	if (sa != sb) return false;
	for(int ii = 0; ii < 2; ii++) {
		vector<pair<int,int> > aa(n), bb(n);
		int sl = 0;
		sa = sb = 0;
		for (int i = 0; i < n; i++) {
		  aa[i].first = a[i];
			aa[i].second = l[i];
			bb[i].first = b[i];
			bb[i].second = l[i];
		}
		sort(aa.begin(), aa.end());
		sort(bb.begin(), bb.end());
		for (int i = 0, j = 0; i < n; i++) {
		  while(j < n && sl + aa[j].second <= bb[i].second) {
			  //printf("adding %d %d\n",aa[j].first, aa[j].second);
				sl += aa[j].second;
				sa += ((long long)aa[j].first) * aa[j].second;
				j++;
			}
			if (j < n && sl < bb[i].second) {
			  //printf("partial %d %d\n",bb[i].first, bb[i].second);
			  aa[j].second -= bb[i].second - sl;
				sa += ((long long)aa[j].first) * (bb[i].second - sl);
				sa -= ((long long)bb[i].first) * bb[i].second;
				if (sa > 0) return false;
				sl = 0;
			} else {
			  //printf("full %d %d\n",bb[i].first, bb[i].second);
				sa -= ((long long)bb[i].first) * bb[i].second;
				sl -= bb[i].second;
				if (sa > 0) return false;
			}
		}
	  for(int i = 0; i < n; i++) a[i] *= -1, b[i] *= -1;
	}
	return true;
}

int main() {
	int tt;
	scanf("%d", &tt);
	while(tt--) {
	  if (tcase())
			puts("TAK");
		else
			puts("NIE");
	}
	return 0;
}