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
#include <bits/stdc++.h>

using namespace std;

int tt, n;
long long a, b, l, wym;
//	p, k - początkkowa i końcowa vol (second) w kubku z tem (first)
pair <int, int> p[100010], k[100010], p2[100010];
//	x - wsk w p
int x;
long long ene, mas;

int main()
{
	scanf("%d", &tt);
	for(int ii=0; ii<tt; ++ii)
	{
		scanf("%d", &n);
		wym=0;
		for(int i=1; i<=n; ++i)
		{
			scanf("%lld%lld%lld", &l, &a, &b);
			p[i]={a, l};
			k[i]={b, l};
			wym+=l*(a-b);
		}
		if(wym!=0)
		{
			printf("NIE\n");
			continue;
		}
		bool ch=false;
		sort(p+1, p+1+n);
		sort(k+1, k+1+n);
		for(int i=1; i<=n; ++i)
		{
			p2[i]=p[i];
		}
		ene=0;
		mas=0;
		x=1;
		for(int i=1; i<=n; ++i)
		{
			mas+=k[i].second;
			ene+=k[i].first*(long long)k[i].second;
			while(mas>0)
			{
				if(p[x].second<=mas)
				{
					mas-=p[x].second;
					ene-=p[x].first*(long long)p[x].second;
					++x;
				}
				else
				{
					ene-=mas*p[x].first;
					p[x].second-=mas;
					mas=0;
				}
			}
			if(ene<0)
			{
				ch=true;
				break;
			}
		}
		
		for(int i=1; i<=n; ++i)
		{
			p[i]=p2[i];
		}
		reverse(p+1, p+1+n);
		reverse(k+1, k+1+n);
		ene=0;
		mas=0;
		x=1;
		for(int i=1; i<=n; ++i)
		{
			mas+=k[i].second;
			ene+=k[i].first*(long long)k[i].second;
			while(mas>0)
			{
				if(p[x].second<=mas)
				{
					mas-=p[x].second;
					ene-=p[x].first*(long long)p[x].second;
					++x;
				}
				else
				{
					ene-=mas*p[x].first;
					p[x].second-=mas;
					mas=0;
				}
			}
			if(ene>0)
			{
				ch=true;
				break;
			}
		}
		if(ch)
		{
			printf("NIE\n");
		}
		else
		{
			printf("TAK\n");
		}
	}
	return 0;
}