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
188
189
190
191
192
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;

public class par {
	// by asokoly
	private static int skipWhitespace(final InputStream in) throws IOException {
		int val = -1;
		while ((val = in.read()) != -1) {
			if (!Character.isWhitespace((char) val))
				break;
		}

		return val;
	}

	private static int readInt(final InputStream in) {
		final StringBuilder b = new StringBuilder();
		try {
			int val = skipWhitespace(in);
			b.append((char) val);
			while ((val = in.read()) != -1) {
				if (Character.isWhitespace((char) val))
					break;
				b.append((char) val);
			}
			return Integer.parseInt(b.toString());
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}

	// by asokoly */
	
	static class data {
		int x1, x2, y1, y2;
		int h,w;
		
		public data(int x1, int y1, int x2, int y2) {
			super();
			this.x1 = x1;
			this.x2 = x2;
			this.y1 = y1;
			this.y2 = y2;
			
		    h = y2 - y1;
			w = x2 - x1;				
		}


		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "("+x1 + "," + y1 + "; " + x2 + ","+y2 + ") w:"+w+" h:"+h;
		}
	}
	

	public static void run(final InputStream in, PrintStream out) {
		int t = readInt(in);
		for (int i = 0; i < t; i++) {
			boolean res = true;
			int n = readInt(in);
			int hM = readInt(in);
			
			data ts[] = new data[n];
			data to[] = new data[n];
			int lm = 0;
			data tm[] = new data[n];
			
			
			for (int j = 0; j < n; j++) {
				int x1 = readInt(in);
				int y1 = readInt(in);
				int x2 = readInt(in);
				int y2 = readInt(in);
				data d = new par.data(x1, y1, x2, y2); 
				ts[j] = d;
				to[j] = d;
			}
			
			Arrays.sort(ts, new Comparator<data>() {

				@Override
				public int compare(data d1, data d2) {
					// TODO Auto-generated method stub
					int k = d1.x1 - d2.x1;
					if (k == 0)
					{
						return d2.h - d1.h;
					}
					return k;
				}
				
			});
			
//			System.out.println("TS");
			int lx = -1;
			for(int j=0; j<n; j++)
			{
				if (lx != ts[j].x1)
				{
					lx = ts[j].x1;
					tm[lm++] = ts[j];
				}
//				System.out.println(ts[j]);
			}

//			System.out.println("TM");
//			for(int j=0; j<lm; j++)
//			{
//				System.out.println(tm[j]);
//			}

			
			for (int j = 0; j < n; j++) {
				int x1 = readInt(in);
				int y1 = readInt(in);
				int x2 = readInt(in);
				int y2 = readInt(in);
				data d = to[j];
				data k = new data(x1,y1,x2,y2);
				int sp = Arrays.binarySearch(tm, d, new Comparator<data>(){

					@Override
					public int compare(data o1, data o2) {
						return o1.x1 - o2.x1;
					}
					
				});
				
				int sk = Arrays.binarySearch(tm, k, new Comparator<data>(){

					@Override
					public int compare(data o1, data o2) {
						return o1.x1 - o2.x1;
					}
					
				});
				
				if(sp<sk)
				{
					//System.out.println("LEWO");
					for (int a = sp; a<sk; a++)
					{
						//System.out.println(tm[a]);
						if (hM - tm[a].h < k.h)
						{
							res = false;
							break;
						}
					}
				}
				else
				if(sp>sk)
				{
					//System.out.println("PRAWO");
					for (int a = sk; a>sp; a--)
					{
						//System.out.println(tm[a]);
						if (hM - tm[a].h < k.h)
						{
							res = false;
							break;
						}
					}
				}
				
				
			}
			
			
			/*
1
3 3
4 0 6 1
2 1 4 3
0 0 2 2

			  
			 */
			out.println(res ? "TAK" : "NIE");
//			
		}
	}

	public static void main(String[] args) {
		run(System.in, System.out);
	}
}