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
77
78
79
80
81
82
83
84
85
86
#include<cstdio>
#include<stdio.h>
#include<string.h>


#define L 1000005

//int count_in[L];
//int count_out[L];
int count_res[L];

int main() {
	int t;
	int n;

	long long int l, a, b;

	scanf("%d", &t);

	for (int i = 0; i < t; i++) {
		scanf("%d", &n);

		long long int suma_in = 0;
		long long int suma_out = 0;

//		memset(count_in,  0, sizeof(int) * L);
//		memset(count_out, 0, sizeof(int) * L);
		memset(count_res, 0, sizeof(int) * L);

		for (int j = 0; j < n; j++) {
			scanf("%lld %lld %lld", &l, &a, &b);

			suma_in += l * a;
			suma_out += l * b;

//			count_in[a] += l;
//			count_out[b] += l;

			count_res[a] += l;
			count_res[b] -= l;

//			if (a < b) {
//				printf("podgrzac o  %lld %lld\n", l, b - a);
//			} else {
//				printf("ochlodzic o %lld %lld\n", l, a - b);
//			}
		}

		if (suma_in != suma_out) {
			printf("NIE\n");

		} else {
			int first_non_zero = 0;
			int last_non_zero = 0;

//			for (int k = 0; k < L; k++) {
//				if (count_res[k] != 0) {
//					printf("%d %d\n", k, count_res[k]);
//				}
//			}

			for (int k = 0; k < L; k++) {
				if (count_res[k] != 0) {
					first_non_zero = count_res[k];
					break;
				}
			}

			for (int k = L - 1; k >= 0; k--) {
				if (count_res[k] != 0) {
					last_non_zero = count_res[k];
					break;
				}
			}

			if (first_non_zero < 0 || last_non_zero < 0) {
				printf("NIE\n");

			} else {
				printf("TAK\n");
			}
		}
	}

	return 0;
}