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
#include <iostream>

using namespace std;

int main() {
	int t, n;
	
	
	cin >> t;
	
	int res[t] ;
	
	for(int i=0; i<t; i++)
	{
		int ANS = 0;
		cin >> n;
		int W[n], w[n], H[n], h[n], BIG[n], SMALL[n];
		for(int j=0; j<n; j++)
		{
			cin >> w[j];
			cin >> W[j];
			cin >> h[j];
			cin >> H[j];
		}
		//end .read
		
		int W_Max = 0, H_Max = 0;
		for(int j=0; j<n; j++){
			if(W[j] >= W_Max && H[j] >= H_Max)
			{
				W_Max = W[j];
				H_Max = H[j];
			}
		}
		
		
		int __W_Max = 0, __H_Max = 0;
		for(int j=0; j<n; j++){
			if(W[n-j-1] >= __W_Max && H[n-j-1] >= __H_Max)
			{
				__W_Max = W[n-j-1];
				__H_Max = H[n-j-1];
			}
		}
		
		if((W_Max == __W_Max) && (H_Max == __H_Max))
		{
			ANS = 1;
		}
		else
		{
			ANS= 0;
		}
		
		int w_Min = w[0], h_Min = h[0];
		int __w_Min = w[n-1], __h_Min = h[n-1];
		
		
		
		if(ANS == 1){
			for(int j=0; j<n; j++){
				if(w[j] <= w_Min && h[j] <= h_Min)
				{
					w_Min = w[j];
					h_Min = h[j];
				}
			}
			
			
			for(int j=0; j<n; j++){
				if(w[n-j-1] <= __w_Min && h[n-j-1] <= __h_Min)
				{
					__w_Min = w[n-j-1];
					__h_Min = h[n-j-1];
				}
			}
			
			
			if((w_Min == __w_Min) && (h_Min == __h_Min))
			{
				ANS = 1;
			}
			else
			{
				ANS= 0;
			}	
				
			if(ANS == 1){
				for(int j=0; j<n; j++){
					if(W[j] == W_Max && H[j] == H_Max && w[j] == __w_Min && h[j] == __h_Min){
						ANS = 1;
						break;
					}
					else{
						ANS = 0;
					}
				}
			}
		}
		//cout << "ans " << ANS << " " <<endl;
		res[i] = ANS;
	
		
		
	}
	
	
		for(int i=0; i<t; i++){
	//	cout << "Przpadek" << i << " " <<OUT[i]<<endl;
		if(res[i] == 1){
			
    printf("%s", "TAK");
   printf("\n");
		}
		else{
			
    printf("%s", "NIE");
   printf("\n");
		}
	}
	
	
	
	
	
	return 0;
}