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
//Zuzanna Ossowska
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;

//LONG LONGIIIIIIIIIIIIIIIII

pair <long long,long long> t1[100000];
pair <long long,long long> t2[100000];
pair <long long,long long> t3[100000];
pair <long long,long long> t4[100000];
long long t,n,a,b,c,l1,l2,suma1,suma2,p;
bool w;

int main()
{
	scanf("%lld",&t);
	for (long long zz=0; zz<t; zz++)
	{
		w=0;
		scanf("%lld",&n);
		for (long long i=0; i<n; i++)
		{
			scanf("%lld%lld%lld",&a,&b,&c);
			t1[i]={b,a};
			t2[i]={c,a};
			t3[i]={b,a};
			t4[i]={c,a};
		}
		sort (t1,t1+n);
		sort (t2,t2+n);
		sort (t3,t3+n);
		sort (t4,t4+n);
		l1=0;
		suma1=0;
		l2=0;
		suma2=0;
		p=0;
		for (long long i=0; i<n; i++)
		{
			//cout << t2[i].ff << " " << t2[i].ss << endl;
			suma2+=t2[i].ff*t2[i].ss;
			l2+=t2[i].ss;
			while (l1<l2 && p<n)
			{
				if (l2-l1<t1[p].ss)
				{
					t1[p].ss-=(l2-l1);
					suma1+=(l2-l1)*t1[p].ff;
					l1=l2;
				}
				else
				{
					l1+=t1[p].ss;
					suma1+=t1[p].ss*t1[p].ff;
					p++;
				}
			}
			//cout << suma1 << " " << suma2 << endl;
			if (suma1>suma2)
			{
				w=1;
				break;
			}
		}
		l1=0;
		l2=0;
		suma1=0;
		suma2=0;
		p=n-1;
		for (long long i=n-1; i>=0; i--)
		{
			//cout << t2[i].ff << " " << t2[i].ss << endl;
			suma2+=t4[i].ff*t4[i].ss;
			l2+=t4[i].ss;
			//cout << suma2 << " " << l2 << endl;
			while (l1<l2 && p>=0)
			{
				if (l2-l1<t3[p].ss)
				{
					t3[p].ss-=(l2-l1);
					suma1+=(l2-l1)*t3[p].ff;
					l1=l2;
				}
				else
				{
					l1+=t3[p].ss;
					suma1+=t3[p].ss*t3[p].ff;
					p--;
				}
			}
			//cout << suma1 << " " << suma2 << endl;
			if (suma1<suma2)
			{
				w=1;
				break;
			}
		}
		if (suma1!=suma2) w=1;
		if (w==1) printf("NIE\n");
		else printf("TAK\n");
	}
}