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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.io.IOException;
import java.io.InputStream;


public class lus {
 
	public static void main(String[] args) throws IOException {
		Timer timer = new Timer();
		
		final InputStream inputStream = System.in;
		final SimpleNumber simpleNumber = new SimplePositiveInteger();
		final SimpleStringParser simpleStringParser = new SimpleStringParser(100000, inputStream);
		
		int n = simpleStringParser.parseLastInt(simpleNumber, true);
		for(int i = 0;i < n;i++) {
			calculteAnswer(simpleNumber, simpleStringParser, i < n - 1);
		}
		
		timer.printTimeInMillis();
	}

	private static void calculteAnswer(SimpleNumber simpleNumber, SimpleStringParser simpleStringParser, boolean isNotLast) throws IOException {
		int n = simpleStringParser.parseLastInt(simpleNumber, true);
		
		int minW = simpleStringParser.parseInt(simpleNumber);
		int maxW = simpleStringParser.parseInt(simpleNumber);
		int minH = simpleStringParser.parseInt(simpleNumber);
		int maxH = simpleStringParser.parseLastInt(simpleNumber, true);
		final int maxI = n - 1;
		
		String message = "TAK";
		for(int i = 1;i < n;i++) {
			int w1 = simpleStringParser.parseInt(simpleNumber);
			int w2 = simpleStringParser.parseInt(simpleNumber);
			int h1 = simpleStringParser.parseInt(simpleNumber);
			int h2 = simpleStringParser.parseLastInt(simpleNumber, isNotLast || i < maxI);
			
			if(minW > w1 || maxW < w2 || minH > h1 || maxH < h2) {
				minW = Math.min(minW,  w1);
				maxW = Math.max(maxW,  w2);
				minH = Math.min(minH,  h1);
				maxH = Math.max(maxH,  h2);
				message = "NIE";
			} 
			
			if(minW == w1 && maxW == w2 && minH == h1 && maxH == h2) {
				message = "TAK";
			}
		}
		
		System.out.println(message);
	}
	
	static class SimpleStringParser {
		int index = 0;
		final byte[] array;
		int bytesReaded;
		final InputStream inputStream;
		
		public SimpleStringParser(final int arraySize, final InputStream inputStream) throws IOException {
			array = new byte[arraySize];
			this.inputStream = inputStream;
			bytesReaded = inputStream.read(array);
		}
		
		protected final void nextChunkOfData() throws IOException {
			bytesReaded = inputStream.read(array);
			index = 0;
		}
		
		protected final boolean nextChunkOfDataRequired() {
			return bytesReaded <= index;
		}
		
		public int parseInt(final SimpleNumber simpleNumber) throws IOException {
			final int integer = simpleNumber.parseInt(this);
			++index;
			return integer;
		}
		
		public int parseLastInt(final SimpleNumber simpleNumber, final boolean hasMore) throws IOException {
			return simpleNumber.parseLastInt(this, hasMore);
		}
	}
	
	static abstract class SimpleNumber {
		protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
		protected static final char BEGIN_OF_LINE_SEPARATOR = LINE_SEPARATOR.charAt(0);
		protected static final int ZERO = '0';
		protected static final int SPACE = ' ';
		
		public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();}
		public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();}
	}
	
	static class SimplePositiveInteger extends SimpleNumber {
		@Override
		public int parseInt(final SimpleStringParser simpleStringParser) throws IOException {
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			if(simpleStringParser.array[simpleStringParser.index] == SPACE) {
				++simpleStringParser.index;
			}
			int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO;
			int value;
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) {
				integer *= 10;
				integer += value - ZERO;
				++simpleStringParser.index;
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
				}
			}
			return integer;
		}
		
		@Override
		public int parseLastInt(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException {
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO;
			int value;
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			while((value = simpleStringParser.array[simpleStringParser.index]) != BEGIN_OF_LINE_SEPARATOR) {
				integer *= 10;
				integer += value - ZERO;
				++simpleStringParser.index;
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
				}
			}
			if(hasMore) {
				simpleStringParser.index += LINE_SEPARATOR.length();
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
					for(int i = 0;i < LINE_SEPARATOR.length();++i) {
						if(simpleStringParser.array[i] == LINE_SEPARATOR.charAt(i)) {
							++simpleStringParser.index;
						}
					}
				}
			}
			return integer;
		}
	}
	
	static public class Timer {

		private long time;
		
		public Timer() {
			startNextMeasure();
		}
		
		public void startNextMeasure() {
			time = System.nanoTime();
		}
		
		public void printTimeInMillis() {
			System.err.println(getTitmeInNanos() / 1000000);
		}
		
		public void printTimeInNanos() {
			System.err.println(getTitmeInNanos());
		}
		
		public void printTimeInMicros() {
			System.err.println(getTitmeInNanos() / 1000);
		}
		
		public void printTimeInSeconds() {
			System.err.println(getTitmeInNanos() / 1000000000);
		}
		
		private long getTitmeInNanos() {
			return (System.nanoTime() - time);
		}
	}
	
}