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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
typedef short int sint;
#define FOR(x, b, e) for(int x=(b); x<=(e); ++x)
#define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define ALL(c) c.begin(),c.end()
#define SIZE(x) ((int)((x).size()))
#define PB push_back
#define ST first
#define ND second
// #define DEBUG 0
#define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }}
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));

ll nwd(ll x, ll y) {
	if (y == 0) {
		return x;
	}
	return nwd(y, x % y);
}

struct num {
	ll l, m;
	num() {

	}
	num(ll x): l(x), m(1) {}
	num(ll x, ll y) {
		ll dziel = nwd(x, y);
		l = x / dziel;
		m = y / dziel;
	}

	bool operator < (const num &b) const {
		return l * b.m < b.l * m;
	}
	bool operator==(const num& b) const {
		return l == b.l && m == b.m;
	}
};

num operator-(const num& a, const num& b) {
	return num(a.l * b.m - b.l * a.m, a.m * b.m);
}

num operator+(const num& a, const num& b) {
	return num(a.l*b.m + b.l*a.m, a.m * b.m);
}

num operator*(const num &a, const num& b) {
	return num(a.l * b.l, a.m * b.m);
}

num operator/(const num& a, const num& b) {
	return num(a.l * b.m, a.m * b.l);
}

typedef long double number;
typedef pair<number, number> event;
const int N = 100101;
const number eps = 1e-7;
int n, n1, n2;
event ea[N], eb[N], tea[N], teb[N];

event lacz(const event& a, const event& b) {
	return {(a.ND * a.ST + b.ST * b.ND)/(a.ND + b.ND), a.ND + b.ND};

}

void printOne(int idx, int limit, const event* tab) {
	for (int i = idx; i < limit; ++i) {
		printf("{t: %Lf, l: %Lf} ", tab[i].ST, tab[i].ND);
	}
	printf("\n");
}

void printState(int ida, int idb) {
	printf("A: "); printOne(ida, n1, ea);
	printf("B: "); printOne(idb, n2, eb);
}

bool eq(const number& a, const number&b) {
	return abs(a - b) < eps;
}

void testcase(bool DEBUG) {
	ll s1 = 0, s2 = 0;
	scanf("%d", &n);
	map<int, ll> wyd;
	for (int i = 0; i < n; ++i) {
		int l, a, b;
		scanf("%d %d %d", &l, &a, &b);
		s1 += (ll)a * l;
		s2 += (ll)b * l;
		wyd[a] += l;
		wyd[b] -= l;
	}
	if (s1 != s2) {
		printf("NIE\n");
		return;
	}
	n1 = 0, n2 = 0;
	for (auto elem : wyd) {
		if (elem.ND == 0) {
			continue;
		}
		if (elem.ND > 0) {
			ea[n1++] = {elem.ST, elem.ND};
		} else {
			eb[n2++] = {elem.ST, -elem.ND};
		}
	}
	sort(ea, ea + n1);
	sort(eb, eb + n2);
	int ida = 0, idb = 0;
	while (ida < n1 && idb < n2) {
		if (DEBUG)printf("ida: %d, idb: %d\n", ida, idb);
		if (DEBUG)printState(ida, idb);
		if (eq(ea[ida].ND, 0.)) {
			++ida;
		} else if (eb[idb].ST + eps < ea[ida].ST) {
			if (DEBUG) printf("first early return\n");
			printf("NIE\n");
			return;
		} else if (eq(ea[ida].ST, eb[idb].ST)) {
			if (eq(ea[ida].ND, eb[idb].ND)) {
				if (DEBUG) printf("both same, taking out\n");
				++ida;
				++idb;
			} else if (ea[ida].ND < eb[idb].ND) {
				if (DEBUG) printf("same, less litres on first\n");
				eb[idb].ND = eb[idb].ND - ea[ida].ND;
				++ida;
			} else {
				if (DEBUG) printf("same, less litres on second\n");
				ea[ida].ND = ea[ida].ND - eb[idb].ND;
				++idb;
			}
		} else {
			if (ida + 1 < n1) {
				auto nalane = lacz(ea[ida], ea[ida + 1]);
				// if (DEBUG) printf("If joined first 1and2, temp: %lld/%lld\n", nalane.ST.l, nalane.ST.m);
				if (nalane.ST + eps < eb[idb].ST || eq(nalane.ST, eb[idb].ST)) {
					if (DEBUG) printf("Joining both first is smaller than second, join\n");
					ea[ida + 1] = nalane;
					++ida;
				} else {
					// hard hard hard case
					auto szuk = (ea[ida].ND*(eb[idb].ST - ea[ida].ST)/(ea[ida + 1].ST - eb[idb].ST));
					// if (DEBUG) printf("would need (%lld/%lld) of second\n", szuk.l, szuk.m);
					// tyle potrzeba litrow ea[ida + 1];
					if (ea[ida + 1].ND + eps < szuk) {
						// SHOULD NEVER HAPPEN
						if (DEBUG) printf("3rd early return\n");
						printf("NIE\n");
						return;
					} else {
						// if (DEBUG) printf("joining (%lld/%lld) to first and removing from further\n", szuk.l, szuk.m);
						ea[ida] = {eb[idb].ST, ea[ida].ND + szuk};
						ea[ida+1]={ea[ida+1].ST, ea[ida + 1].ND - szuk};
					}
				}
			} else {
				if (DEBUG) printf("second early return\n");
				printf("NIE\n");
				return;
			}
		}
	}
	while (ida < n1 && eq(ea[ida].ND, 0.)) {
		++ida;
	}
	if (ida == n1 && idb == n2) {
		printf("TAK\n");
	} else {
		printf("NIE\n");
	}
}

int main() {
	int t;
	scanf("%d", &t);
	// t=1;
	FOR(q, 1, t) {
		testcase(false);
	}
	return 0;
}