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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAX_COUNT 50100
// #define DEBUG 1
// #define DEBUG2 1
using namespace std;
class D
{
	public:
		int id, b, e, d;
		D(){}

		void sb(int Id, int v)
		{
			id = Id;
			b = v;
			d = (b < e) ? e - b : b - e;			
		}
		void se(int Id, int v)
		{
			id = Id;
			e = v;
			d = (b < e) ? e - b : b - e;			
		}
		
		void operator=(const D& o)
		{
			id = o.id;
			b = o.b;
			e = o.e;
			d = o.d;
		}
		bool operator==(const D& b) const 
		{ 
			return this->id == b.id; 
		}
		bool operator!=(const D& b) const 
		{
			return !(*this == b);
		}
		bool operator< (const D& b) const
		{
			if(d > b.d) return true;
			if(d < b.d) return false;
			return id > b.id;
		}
		bool operator> (const D& b) const
		{
			return b < *this;
		}
		bool operator<=(const D& b) const
		{
			return !operator> (b);
		}
		bool operator>=(const D& b) const
		{
			return !operator< (b);
		}

		void dump() const
		{
			printf("\tD = { id: %d, b: %d, e: %d, d: %d }\n", id, b, e, d);
		}

};
class M
{
	public:
		int id, x0, y0, x1, y1, s, h;
		M(){}
		M(int Id, int X0, int Y0, int X1, int Y1, int w) : id(Id), x0((X0 < X1) ? X0 : X1), y0((Y0 < Y1) ? Y0 : Y1), x1((X0 < X1) ? X1 : X0), y1((Y0 < Y1) ? Y1 : Y0)
		{
			h = (y1 - y0);
			s = (w - h);
			#ifdef DEBUG
				puts("M::constructor");
				dump();
			#endif
		}

		void operator=(const M& b)
		{
			id = b.id;
			x0 = b.x0;
			y0 = b.y0;
			x1 = b.x1;
			y1 = b.y1;
			s = b.s;
			h = b.h;
		}
		bool operator==(const M& b) const 
		{ 
			return this->id == b.id; 
		}
		bool operator!=(const M& b) const 
		{
			return !(*this == b);
		}
		bool operator< (const M& b) const
		{
			#ifdef DEBUG2
				printf("M %d < %d: (%d %d) < (%d %d)\n", this->id, b.id, this->x0, this->y0, b.x0, b.y0);
			#endif
			
			if(x0 < b.x0) return true;
			if(x0 > b.x0) return false;
			if(y0 < b.y0) return true;
			if(y0 > b.y0) return false;
			return id < b.id;
		}
		bool operator> (const M& b) const
		{
			return b < *this;
		}
		bool operator<=(const M& b) const
		{
			return !operator> (b);
		}
		bool operator>=(const M& b) const
		{
			return !operator< (b);
		}

		void dump() const
		{
			printf("\tM = { id: %d, s: %d, h: %d, (%d, %d) : (%d, %d) }\n", id, s, h, x0, y0, x1, y1);
		}
};


typedef vector<M> TvM;
TvM B = TvM(), E = TvM();
typedef vector<D> TvD;
TvD T = TvD();
TvM::const_iterator IB, IE;
// TvM::iterator Iw;
TvD::const_iterator IT, ITL;

int t, w, i, n, r;
int a, b, c, d;
int main()
{
	scanf("%d", &t);
	while(t--)
	{
		#ifdef DEBUG2
			printf("t=%d\n", t);
		#endif
	
		scanf("%d %d", &n, &w);
		#ifdef DEBUG2
			printf("n=%d w=%d\n", n, w);
		#endif
		B.clear();
		E.clear();
		T.clear();
		B.reserve(n);
		E.reserve(n);
		T.reserve(n);
		T.resize(n);
		for(i=0; i<n; ++i)
		{
			scanf("%d %d %d %d", &a, &b, &c, &d);
			#ifdef DEBUG2
				printf("B %d: %d %d %d %d\n", i, a, b, c, d);
			#endif
			B.push_back(M(i, a, b, c, d, w));
		}
		for(i=0; i<n; ++i)
		{
			scanf("%d %d %d %d", &a, &b, &c, &d);
			#ifdef DEBUG2
				printf("E %d: %d %d %d %d\n", i, a, b, c, d);
			#endif
			E.push_back(M(i, a, b, c, d, w));
		}
		#ifdef DEBUG2
			for(IB = B.begin(); IB != B.end(); ++IB)
				printf("B %d: (%d %d), (%d %d), %d\n", IB->id, IB->x0, IB->y0, IB->x1, IB->y1, IB->s);
		#endif
		#ifdef DEBUG2
			printf("\n\nB sorting\n");
		#endif
		sort(B.begin(), B.end());
		#ifdef DEBUG2
			printf("B sorted\n\n");
		#endif
		#ifdef DEBUG2
			for(IB = B.begin(); IB != B.end(); ++IB)
				printf("B %d: (%d %d), (%d %d), %d\n", IB->id, IB->x0, IB->y0, IB->x1, IB->y1, IB->s);
		#endif
		#ifdef DEBUG2
			for(IB = E.begin(); IB != E.end(); ++IB)
				printf("E %d: (%d %d), (%d %d), %d\n", IB->id, IB->x0, IB->y0, IB->x1, IB->y1, IB->s);
		#endif
		#ifdef DEBUG2
			printf("\n\nE sorting\n");
		#endif
		
		sort(E.begin(), E.begin() + n);
		#ifdef DEBUG2
			printf("E sorted\n\n");
		#endif
		#ifdef DEBUG2
			for(IB = E.begin(); IB != E.end(); ++IB)
				printf("E %d: (%d %d), (%d %d), %d\n", IB->id, IB->x0, IB->y0, IB->x1, IB->y1, IB->s);
		#endif
		
		for(i=0, IB = B.begin(), IE = E.begin();i<n;++i, ++IB, ++IE)
		{
			#ifdef DEBUG
				printf("T %d: set IB.id = %d, IE.id = %d\n", i, IB->id, IE->id);
			#endif
			T[IB->id].sb(IB->id, i);
			T[IE->id].se(IE->id, i);
		}
		#ifdef DEBUG
			puts("T Dump");
			for(IT = T.begin(); IT != T.end(); ++IT)
				printf("T (%d -> %d) = %d\n", IT->b, IT->e, IT->d);
			puts("\n\nT Sorting");
		#endif
		sort(T.begin(), T.end());
		#ifdef DEBUG
			puts("T Sorted\n");
			for(IT = T.begin(); IT != T.end(); ++IT)
				printf("T (%d -> %d) = %d\n", IT->b, IT->e, IT->d);
		#endif
		
		r = 1;
		#ifdef DEBUG
			puts("Start check");
		#endif
		for(IT = T.begin(); IT != T.end(); ++IT)
		{
			#ifdef DEBUG
				printf("Check: %d: %d -> %d\n", IT->id, IT->b, IT->e);
			#endif
			if(IT->e != IT->b)
			{
				#ifdef DEBUG
					IT->dump();
				#endif
				IE = B.begin() + IT->e;
				IB = B.begin() + IT->b;
				#ifdef DEBUG
					IE->dump();
					IB->dump();
				#endif
				w = IB->h;
				if(IE->h > IB->s || w > IE->s)
				{
					#ifdef DEBUG
						printf("Check: fast: %d <=> %d : not possible\n", IT->b, IT->e);
					#endif
					r = 0;
					break;
				}
				for(++IB; IB < IE; ++IB)
				{
					#ifdef DEBUG
						printf("R: IB(id = %d, s = %d): %d\n", IB->id, IB->s, w);
					#endif
					if(IB->s < w)
					{
						r = 0;
						break;
					}
				}
			}
			if(!r) break;
		}
		puts(r ? "TAK" : "NIE");
	}
	
	return 0;
}