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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct lustro
{
	int i, nr;
	lustro (int a, int b): i(a), nr(b){}
};
bool porsmall(lustro a, lustro b){if (a.i!=b.i) return a.i<b.i; return a.nr<b.nr;}
bool porbig(lustro a, lustro b){if (a.i!=b.i) return a.i>b.i; return a.nr<b.nr;}


vector<lustro> wsmall, wbig, hsmall, hbig;

int main()
{
	int ilez;
	cin>>ilez;
	for(int aa=0; aa<ilez; aa++)
	{
		int ile;
		cin>>ile;
		for(int n=0; n<ile; n++)
		{
			int tmp;
			cin>>tmp;
			wsmall.push_back(lustro(tmp, n));
			cin>>tmp;
			wbig.push_back(lustro(tmp, n));
			cin>>tmp;
			hsmall.push_back(lustro(tmp, n));
			cin>>tmp;
			hbig.push_back(lustro(tmp, n));
		}
		sort(wsmall.begin(), wsmall.end(), porsmall);
		sort(wbig.begin(), wbig.end(), porbig);
		sort(hsmall.begin(), hsmall.end(), porsmall);
		sort(hbig.begin(), hbig.end(), porbig);
		
		while(wsmall.back().i!=wsmall[0].i) wsmall.pop_back();
		while(wbig.back().i!=wbig[0].i) wbig.pop_back();
		while(hsmall.back().i!=hsmall[0].i) hsmall.pop_back();
		while(hbig.back().i!=hbig[0].i) hbig.pop_back();
		
//		cout<<"wsmall"<<endl; for(int n=0; n<wsmall.size(); n++) { cout<<wsmall[n].nr<<" ";} cout<<endl;
//		cout<<"wbig"<<endl; for(int n=0; n<wbig.size(); n++) { cout<<wbig[n].nr<<" ";} cout<<endl;
//		cout<<"hsmall"<<endl; for(int n=0; n<hsmall.size(); n++) { cout<<hsmall[n].nr<<" ";} cout<<endl;
//		cout<<"hbig"<<endl; for(int n=0; n<hbig.size(); n++) { cout<<hbig[n].nr<<" ";} cout<<endl;
		
		int maxi=max(max(wsmall.back().nr, wbig.back().nr) ,max(hsmall.back().nr, hbig.back().nr));
		while(wsmall.size()>0 && wbig.size()>0 && hsmall.size()>0 && hbig.size()>0 && (wsmall.back().nr!=maxi || wbig.back().nr!=maxi || hsmall.back().nr!=maxi || hbig.back().nr!=maxi))
		{
			if(wsmall.back().nr==maxi) wsmall.pop_back();
			if(wbig.back().nr==maxi) wbig.pop_back();
			if(hsmall.back().nr==maxi) hsmall.pop_back();
			if(hbig.back().nr==maxi) hbig.pop_back();
			maxi=max(max(wsmall[wsmall.size()-1].nr, wbig[wbig.size()-1].nr) ,max(hsmall[hsmall.size()-1].nr, hbig[hbig.size()-1].nr));
		}
		if(wsmall.size()>0 && wbig.size()>0 && hsmall.size()>0 && hbig.size()>0)
		{
			cout<<"TAK"<<endl;
		}
		else
		{
			cout<<"NIE"<<endl;
		}
		wsmall.clear(); wbig.clear(); hsmall.clear(); hbig.clear();
	}
}