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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
#include <map>

#define ll long long
#define pll pair<long long, long long>
#define ld long double

using namespace std;

ll n, eBeg, eReq;

//first  - litry, second - temp;
pair<ld,ll> minBeg, minReq, maxBeg, maxReq;

pair<ld, ld> tempKubek;
vector< pair<ld, ld> > prefixKubki, prefixAkt;
 
ld l, a, b;

// map[temp] = l;
map<ld, ld> beg, req;

void prepare()
{
	eBeg=eReq=0;
	minBeg.first = minBeg.second = maxBeg.first = maxBeg.second = 0;
	minReq.first = minReq.second = maxReq.first = maxReq.second = 0;
	tempKubek.first = tempKubek.second = 0;
	prefixKubki.clear();
	beg.clear();
	req.clear();
	prefixAkt.clear();
}

void updateMin()
{
	if(minBeg.second == a)
		minBeg.first += l;
		
	if(minBeg.second > a)
		minBeg.first = l, minBeg.second = a;
		
	if(minReq.second == b)
		minReq.first += l;
		
	if(minReq.second > b)
		minReq.first = l, minReq.second = b;
}

void updateMax()
{
	if(maxBeg.second == a)
		maxBeg.first += l;
	
	if(maxBeg.second < a)
		maxBeg.first = l, maxBeg.second = a;
		
	if(maxReq.second == b)
		maxReq.first += l;
		
	if(maxReq.second < b)
		maxReq.first = l, maxReq.second = b;
}

void update()
{
	eBeg += l*a;
	eReq += l*b;
	updateMin();
	updateMax();
	
	beg[a] += l;
	req[b] += l;
	
	pair<ld,ld> temp;
	temp.first = b;
	temp.second = l;
	prefixKubki.push_back(temp);
}

bool isPossible()
{
	if(eBeg != eReq)
		return 0;
		
	if(maxBeg.second == maxReq.second && maxBeg.first < maxReq.first)
		return 0;
		
	if(maxBeg.second < maxReq.second)
		return 0;
		
	if(minBeg.second == minReq.second && minBeg.first < minReq.first)
		return 0;
		
	if(minBeg.second > minReq.second)
		return 0;
		
	return 1;
}

void getPrefixKubki()
{
	for(auto it=req.begin(); it!=req.end(); it++)
	{
		//redukujemy
		if(beg.find((*it).first) != beg.end())
		{
			if(beg[(*it).first] >= (*it).second)
			{
				beg[(*it).first]-=(*it).second;
				continue;
			}
			else
			{
				(*it).second -= beg[(*it).first];
				beg[(*it).first] = 0;
			}
		}
		
		//cout << "tempKubek: [" << tempKubek.first << ", " << tempKubek.second << "], curr: [" << (*it).first << ", " << (*it).second << "]\n"; 
		
		tempKubek.first = ((tempKubek.first*tempKubek.second) + ((*it).first*(*it).second))/(tempKubek.second + (*it).second);
		tempKubek.second = tempKubek.second + (*it).second;
		prefixKubki.push_back(tempKubek);
	}
}

void getPrefixAkt()
{
	tempKubek.first = tempKubek.second = 0;
	for(auto it=beg.begin(); it!=beg.end(); it++)
	{
		if((*it).second == 0)
			continue;
			
		tempKubek.first = ((tempKubek.first*tempKubek.second)+((*it).first*(*it).second))/(tempKubek.second+(*it).second);
		tempKubek.second += (*it).second;
		prefixAkt.push_back(tempKubek);
		//cout << "[" << tempKubek.first << ", " << tempKubek.second << "]\n";
	}
}

ll index1;
ll getFirstSmaller(ld num, ll p, ll q)
{
	//zwraca idnex pierwszego prefixu ze >= litraz od szukanego
	ll mid = (p+q+1)/2;
	
	if(prefixAkt[mid].second >= num)
		index1 = mid;
		
	if(p==q)
		return index1;
		
	if(prefixAkt[mid].second >= num)
		return getFirstSmaller(num, p, mid-1);
		
	return getFirstSmaller(num, mid, q);
}


ll getLastBigger(ld num, ll p, ll q)
{
	ll siz = prefixAkt.size()-1;
	ll mid=(p+q+1)/2;
	
	if(prefixAkt[siz].second - prefixAkt[mid-1].second >= num)
		index1 = mid;
		
	if(p==q)
		return index1;
		
	if(prefixAkt[siz].second - prefixAkt[mid-1].second >= num)
		return getLastBigger(num, mid, q);
		
	return getLastBigger(num, p, mid-1);
}

bool isOKKubek(ll ind)
{
	index1=-1;
	//cout << "OK-";
	ll i = getFirstSmaller(prefixKubki[ind].second, 0, prefixAkt.size()-1);
	//cout << "BS\n";
	
	pair<ld,ld> akt, prev;
	//temp.first=temp.second=prev.first=prev.second=0;
	
	if(i > 0)
	{
		prev.first = prefixAkt[i-1].first;
		prev.second = prefixAkt[i-1].second;
		
		akt.first = ((prefixAkt[i].first*prefixAkt[i].second)-(prefixAkt[i-1].first*prefixAkt[i-1].second))/(prefixAkt[i].second - prefixAkt[i-1].second);
		akt.second = prefixAkt[i].second - prefixAkt[i-1].second;
		
		ld res = ((prev.first*prev.second) + (akt.first*(prefixKubki[ind].second-prev.second)))/prefixKubki[ind].second;
		
		if(res > prefixKubki[ind].first)
			return 0;
	}
	else
		if(prefixAkt[i].first > prefixKubki[ind].first)
			return 0;
			
	//znajdujemy taki  ostatni index, że suffix ma >= litraz
	index1=0;
	i = getLastBigger(prefixKubki[ind].second, 1, prefixAkt.size()-1);
	
	if(i < (prefixAkt.size()-1))
	{
		pair<ld,ld> next;
		next.first = ((prefixAkt[prefixAkt.size()-1].first * prefixAkt[prefixAkt.size()-1].second) - (prefixAkt[i].first * prefixAkt[i].second))/(prefixAkt[prefixAkt.size()-1].second - prefixAkt[i].second);
		next.second = prefixAkt[prefixAkt.size()-1].second - prefixAkt[i].second;
		
		if(i > 0)
		{
			akt.first = ((prefixAkt[i].first*prefixAkt[i].second) - (prefixAkt[i-1].first*prefixAkt[i-1].second))/(prefixAkt[i].second-prefixAkt[i-1].second);
			akt.second = prefixAkt[i].second - prefixAkt[i-1].second;
			
			ld res = ((next.first*next.second) + (akt.first*(prefixKubki[ind].second - next.second)))/prefixKubki[ind].second;
			
			if(res < prefixKubki[ind].first)
			{
				
			}
				//return 0;
		}
		else
		{
			akt.first = prefixAkt[0].first, akt.second = prefixAkt[0].second;
			
			ld res = ((next.first*next.second) + (akt.first*(prefixKubki[ind].second - next.second)))/prefixKubki[ind].second;
			
			if(res < prefixKubki[ind].first)
			{
				
			}
				//return 0;
		}
	}
	else
	{
		if(prefixAkt[i].first < prefixKubki[ind].first)
		{
			
		}
			//return 0;
	}
	
	return 1;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		prepare();
		
		while(n--)
		{
			cin >> l >> a >> b;
			update();
		}
		
		if(isPossible())
		{
			getPrefixKubki();
			getPrefixAkt();
			if(prefixAkt.size()==0)
			{
				cout << "TAK\n";
				continue;
			}
			bool pos=1;
			//cout << "JESZCZE KURWA NIE\n";
			for(ll i=0; i<prefixKubki.size(); i++)
			{
				if(!isOKKubek(i))
					pos=0;
			}
			
			if(pos)
				cout << "TAK\n";
			else
				cout << "NIE\n";
		}
		else
			cout << "NIE\n";
	}
	return 0;
}