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
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;

public class lus {
	public static void main(String[] args) throws IOException {
		SystemIO io = new SystemIO();
		new lus().resolve(io);
		io.out.flush();
	}
	
	void resolve(IO io) throws IOException {
		int cnt = io.readInt();
		for (int i = 0; i < cnt; i++) {
			int workshopCnt = io.readInt();
			Rect[] r = new Rect[workshopCnt];
			for (int j = 0; j < workshopCnt; j++) {
				r[j] = new Rect(io.readLong(), io.readLong(), io.readLong(), io.readLong());
			}
			dbg("---");
			Arrays.sort(r);
			boolean ok = true;
			Rect b = r[0];
			dbg(r[0]);
			for (int j = 1; j < workshopCnt; j++) {
				dbg(r[j]);
				if (!b.majorizes(r[j])) {
					ok = false;
					break;
				}
			}
			io.writeString(ok ? "TAK\n" : "NIE\n");
		} 
	}
	
	class Rect implements Comparable<Rect>{
		long x1, x2, y1, y2, w, h;
		Rect(long x1, long x2, long y1, long y2) {
			this.x1 = x1;
			this.x2 = x2;
			this.y1 = y1;
			this.y2 = y2;
			w = x2 - x1;
			h = y2 - y1;
		}
		@Override
		public int compareTo(Rect r) {
			long dw = w - r.w;
			if (dw < 0) return 1;
			if (dw > 0) return -1;
			long dh = h - r.h;
			if (dh < 0) return 1;
			if (dh > 0) return -1;
			long dx1 = x1 - r.x1;
			if (dx1 < 0) return -1;
			if (dx1 > 0) return 1;
			long dy1 = y1 - r.y1;
			if (dy1 < 0) return -1;
			if (dy1 > 0) return 1;
			long dx2 = x2 - r.x2;
			if (dx2 < 0) return -1;
			if (dx2 > 0) return 1;
			long dy2 = y2 - r.y2;
			if (dy2 < 0) return -1;
			if (dy2 > 0) return 1;
			return 0;
		}
		public boolean majorizes(Rect r) {
			if ((w == 0 || h == 0) && (r.w > 0 && r.h > 0)) return false;
			if ((r.w == 0 || r.h == 0) && (w > 0 && h > 0)) return true;
			if ((w == 0 || h == 0) && (r.w == 0 || r.h == 0)) return true;
			if (x1 > r.x1 || y1 > r.y1) return false;
			if (x2 < r.x2 || y2 < r.y2) return false;
	        return true;
		}
		@Override
		public String toString() {
			return String.format("[x1:%d,y1:%d,x2:%d,y2:%d,w:%d,h:%d]", x1, y1, x2, y2, w, h);
		}
	}
	
	/* ------------------------------- In / Out ------------------------------------------- */
	
	void dbg(Object o) {
		if (false)
			System.err.println(o);
	}
	
    interface IO {
		int readInt() throws IOException;
		long readLong() throws IOException;
		String readLine() throws IOException;
		void writeString(String str) throws IOException;
    }

    static class SystemIO implements IO {
		StringBuilder b = new StringBuilder();
		BufferedInputStream in = new BufferedInputStream(System.in);
		PrintStream out = new PrintStream(new BufferedOutputStream(System.out));
	
		int skipWhitespace() throws IOException {
		    int r = -1;
		    do {
			r = in.read();
			if (r < 0)
			    break;
		    } while (Character.isWhitespace(r));
		    return r;
		}
	
		public void readDigit() throws IOException {
		    b.setLength(0);
		    int r = skipWhitespace();
		    while (Character.isDigit(r)) {
				b.append((char) r);
				r = in.read();
		    }
		}
	
		@Override
		public int readInt() throws IOException {
		    readDigit();
		    return Integer.parseInt(b.toString());
		}
	
		@Override
		public long readLong() throws IOException {
		    readDigit();
		    return Long.parseLong(b.toString());
		}
	
		@Override
		public String readLine() throws IOException {
		    b.setLength(0);
		    int r = skipWhitespace();
		    while (r != 10) {
			if (r == -1)
			    break;
			b.append((char) r);
			r = in.read();
		    }
		    return b.toString();
		}
	
		@Override
		public void writeString(String str) throws IOException {
		    out.write(str.getBytes());
		}
    }
}