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
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using ll=long long;
#define pb push_back
#define fst first
#define snd second
using vll=vector<ll>;
using Pii=pair<int,int>;
using Pll=pair<ll,ll>;
using vpii=vector<Pii>;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define sim template <class c
#define mor > muu & operator<<(
#define ris return *this
#define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)),muu&>::type operator<<(c g) {
sim> struct rge {c b, e;};
sim> rge<c> range(c i, c j) {return rge<c>{i, j};}
sim> auto dud(c*r)->decltype(cerr << *r);
sim> char dud(...);
struct muu {
	#ifdef LOCAL
	stringstream a;
	~muu() {cerr << a.str() << endl;}
	R22(<) a << boolalpha << g; ris;}
	R22(==) ris << range(begin(g), end(g));}
	sim mor rge<c> u) {
		a << "[";
		for (c i = u.b; i != u.e; ++i)
			*this << ", " + 2 * (i == u.b) << *i;
		ris << "]";
	}
	sim, class m mor pair<m,c> r) {ris << "(" << r.first << ", " << r.second << ")";}
	#else
	sim mor const c &){ris;}
	#endif
	muu & operator()(){ris;}
};
#define imie(r) "[" #r ": " << (r) << "] "
#define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": " )
typedef long long ll;
typedef pair<ll, ll> Pll;
int n;
ll licz_energie(vector<Pll> temp)
{
	ll res = 0;
	for(Pll p : temp)
		res += p.first * p.second;
	return res;
}
void solve()
{
	scanf("%d", &n);
	vector<Pll> jest;
	vector<Pll> ma_byc;
	
	for(int i = 0; i < n; ++i)
	{
		int l, a, b;
		scanf("%d%d%d", &l, &a, &b);
		jest.push_back({a, l});
		ma_byc.push_back({b, l});
	}
	sort(jest.begin(), jest.end());
	sort(ma_byc.begin(), ma_byc.end());
	jest.push_back({1e9, 0});
	if(licz_energie(jest) != licz_energie(ma_byc))
	{
		puts("NIE");
		return;
	}
	int ind = 0;
	ll energia = 0, litry = 0;
	for(int i = 0; i < n; )
	{
		while(ind < n && energia + jest[ind].first * jest[ind].second <= ma_byc[i].first * (litry + jest[ind].second))
		{
			litry += jest[ind].second;
			energia += jest[ind].first * jest[ind].second;
			++ind;
		}
		int j = i;
		ll tot_energ = 0;
		ll tot_lit = 0;
		for(; j < n; ++j)
		{
			tot_energ += ma_byc[j].first * ma_byc[j].second;
			tot_lit += ma_byc[j].second;
			ll lit_na_koncu = litry + jest[ind].second - tot_lit;
			if(lit_na_koncu < 0)
				break;
			ll en_na_koncu = energia + jest[ind].first * jest[ind].second - tot_energ;
			if(en_na_koncu > jest[ind].first * lit_na_koncu)
				break;
		}
		if(j == i)
		{
			puts("NIE");
			return;
		}
		while(i < j)
		{
			energia -= ma_byc[i].first * ma_byc[i].second;
			litry -= ma_byc[i].second;
			++i;
		}
	}
	puts("TAK");
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
		solve();
}