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
#include <cstdio>
#include <iostream>
//#include "time.h"

#define getchar_custom getc_unlocked

//#define DEBUG

char c;

template <typename T>
inline T read_custom() {

	c = getchar_custom(stdin);

	while (c<'0' || c>'9')
	{
		c = getchar_custom(stdin);
	}

	T returnValue = 0;
	while (c >= '0' && c <= '9') {
		returnValue = (returnValue << 3) + (returnValue << 1) + c - 48;
		c = getchar_custom(stdin);
	}

	return returnValue;
}

int min_m(int a, int b) {
	return a < b ? a : b;
}

int max_m(int a, int  b) {
	return a > b ? a : b;
}

int main() {
	
	//clock_t t1, t2;
	 //t1 = clock();
	
	int casesNo = read_custom<int>();

	for (int i = 0; i < casesNo; i++) {

		int  minW =  -1;
		int  maxW = -1;
		int  minH = -1;
		int maxH = -1;

	    bool isEnough = true;

		int factories = read_custom<int>();

		for (int factory = 0; factory < factories; factory++) {

#ifdef DEBUG
            std::cout << "minW " << minW << " maxW " << maxW << " minH " << minH << " maxH " << maxH << std::endl;
#endif

			if (minW == -1) {
				minW = read_custom<int>();
				maxW = read_custom<int>();
				minH = read_custom<int>();
				maxH = read_custom<int>();
				continue;
			}

			int local_minW = read_custom<int>();
			int local_maxW = read_custom<int>();
			int local_minH = read_custom<int>();
			int local_maxH = read_custom<int>();

			if (minW <= local_minW && maxW >= local_maxW && minH <= local_minH && maxH >= local_maxH) {
                
                if (minW == local_minW && maxW == local_maxW && minH == local_minH && maxH == local_maxH) {
					isEnough = true;
				}
                
                // w srodku jest
				continue;
			}

			if (minW >= local_minW && maxW <= local_maxW && minH >= local_minH && maxH <= local_maxH) {
				// nowy kandydat
				minW = local_minW;
				maxW = local_maxW;
				minH = local_minH;
				maxH = local_maxH;

				isEnough = true;
				continue;
			}

			minW = min_m(minW, local_minW);
			maxW = max_m(maxW, local_maxW);
			minH = min_m(minH, local_minH);
			maxH = max_m(maxH, local_maxH);
			isEnough = false;

#ifdef DEBUG
            std::cout << "minW " << minW << " maxW " << maxW << " minH " << minH << " maxH " << maxH << std::endl;
#endif

		}

		if (isEnough) {
			std::cout << "TAK";
		} else {
			std::cout << "NIE";
		}

		std::cout << std::endl;
	}
	
	//t2 = clock();

 //float diff((float)t2 - (float)t1);
 //std::cout << diff / CLOCKS_PER_SEC << std::endl;
}