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
279
280
281
282
283
284
// ======== https://gist.github.com/utkarshl/4417204 ========

template<class node>
class segtree{/*{{{*/
	template<bool b>class param{};
	inline void spltdwn(int idx, param<true>){ splt(idx); }
	inline void splt(int idx){/*{{{*/
		idx >>= 1;
		if (idx>0)splt(idx);
		tree[idx].split(tree[idx << 1], tree[(idx << 1) | 1]);
	}/*}}}*/
	inline void spltdwn(int, param<false>){};
	inline void split(node& a, node& b, node& c, param<true>){ return a.split(b, c); }
	inline void split(node&, node&, node&, param<false>){}
	template<typename t, void (t::*)(t&, t&)> class T{};
	template<typename t> static char test(T<t, &t::split>*){ return 0; }
	template<typename t> static long double test(...){ return 0; }
	int u, v;
	node query(int root, int left_range, int right_range){/*{{{*/
		if (u <= left_range && right_range <= v)
			return tree[root];
		int mid = (left_range + right_range) >> 1,
			l = root << 1,
			r = l | 1;
		if (has_split)split(tree[root], tree[l], tree[r], param<has_split>());
		node res;
		if (u >= mid)res = query(r, mid, right_range);
		else if (v <= mid)res = query(l, left_range, mid);
		else{
			node n1 = query(l, left_range, mid),
				n2 = query(r, mid, right_range);
			res.merge(n1, n2);
		}
		if (has_split) tree[root].merge(tree[l], tree[r]);
		return res;
	}/*}}}*/
	template<void(*fn)(node&)>
	void local_update(int root, int left_range, int right_range){/*{{{*/
		if (u <= left_range && right_range <= v){
			return fn(tree[root]);
		}
		int mid = (left_range + right_range) >> 1,
			l = root << 1,
			r = l | 1;
		if (has_split)split(tree[root], tree[l], tree[r], param<has_split>());
		if (v>mid)local_update<fn>(r, mid, right_range);
		if (u<mid)local_update<fn>(l, left_range, mid);
		tree[root].merge(tree[l], tree[r]);
	}/*}}}*/
	void mrgup(int idx){/*{{{*/
		idx >>= 1;
		while (idx>0)
			tree[idx].merge(tree[idx << 1], tree[(idx << 1) | 1]),
			idx >>= 1;
	}/*}}}*/
public:
	static bool const has_split = (sizeof(test<node>(0)) == sizeof(char));
	int N;
	int leftmost_leaf, rightmost_leaf;
	node* tree;
	node identity;
	segtree(){ tree = 0; }
	~segtree(){
		if (tree) delete[] tree;
	}
	void init(int n, const node a[], const node& identity){/*{{{*/
		if (tree) delete[] tree;
		this->identity = identity;
		N = 0;
		while ((1 << N)<n)N++;
		leftmost_leaf = 1 << N;
		rightmost_leaf = leftmost_leaf << 1;
		tree = new node[rightmost_leaf];
		for (int i = 0; i<n; i++)
			tree[i + leftmost_leaf] = a[i];
		for (int i = n + leftmost_leaf; i<rightmost_leaf; i++)
			tree[i] = identity;
		for (int i = leftmost_leaf - 1; i; i--)
			tree[i].merge(tree[i << 1], tree[(i << 1) | 1]);
	}/*}}}*/
	node query(int u, int v){//[u,v]/*{{{*/
		this->u = u + leftmost_leaf;
		this->v = v + leftmost_leaf + 1;
		return query(1, leftmost_leaf, rightmost_leaf);
	}/*}}}*/
	node query(int u){//faster version of query(u,u)/*{{{*/
		//indexing starts from 0
		u += leftmost_leaf;
		spltdwn(u, param<has_split>());
		return tree[u];
	}/*}}}*/
	template<void(*fn)(node&)>
	void update(int u, int v){/*{{{*/
		//0-indexed
		this->u = u + leftmost_leaf;
		this->v = v + leftmost_leaf + 1;
		return local_update<fn>(1, leftmost_leaf, rightmost_leaf);
	}/*}}}*/
	template<void(*fn)(node&)>
	void update(int u){//faster version of update(u,u)/*{{{*/
		//indexing starts from 0
		u += leftmost_leaf;
		spltdwn(u, param<has_split>());
		fn(tree[u]);
		mrgup(u);
	}/*}}}*/
	void split_down(int leaf_idx){/*{{{*/
		spltdwn(leaf_idx + leftmost_leaf, param<has_split>());
	}/*}}}*/
	void merge_up(int leaf_idx){/*{{{*/
		mrgup(leaf_idx + leftmost_leaf);
	}/*}}}*/
	bool is_leaf(int tree_idx){ return tree_idx >= leftmost_leaf; }
	int binary_search(node k){/*{{{*/
		//search the last place i, such that merge( everyting to the left of i(including i) ) compares less than k
		int root = 1;
		node n = identity;
		//identity satisfies merge(identity,y) = merge(y,identity) = y for all y.
		assert(!(k<identity));
		while (!is_leaf(root)){
			int left_child = root << 1,
				right_child = left_child | 1;
			if (has_split)
				split(tree[root], tree[left_child], tree[right_child], param<has_split>());
			node m;
			m.merge(n, tree[left_child]);
			if (m<k){//go to right side
				n = m;
				root = right_child;
			}
			else root = left_child;
		}
		node m;
		m.merge(n, tree[root]);
		mrgup(root);
		if (m<k)return root - leftmost_leaf;
		else return root - 1 - leftmost_leaf;
	}/*}}}*/
};/*}}}*/

// ======== par.cpp ========

#include <iostream>
#include <cctype>
#include <vector>
#include <algorithm>

typedef unsigned int uint;

uint get_uint()
{
	int c;
	while (isspace(c = std::cin.get()));

	uint value = (c - '0');

	while (isdigit(c = std::cin.get()))
	{
		value = (10 * value) + (c - '0');
	}

	return value;
}

struct data
{
	uint id;

	uint x;
	uint w;

	data(uint _id, uint _x, uint _w) : id(_id), x(_x), w(_w) {}
};

struct node
{
	uint w;

	void merge(node &l, node &r) { w = std::min(l.w, r.w); }
} identity;


inline bool operator<(const data &a, const data &b)
{
	return a.x < b.x;
}

void del(node &n)
{
	n.w = identity.w;
}

bool par()
{
	uint n = get_uint();
	uint w = get_uint();

	std::vector<data> data_in;
	std::vector<data> data_out;

	data_in.reserve(n + 2);
	data_out.reserve(n + 2);

	for (uint i = 0; i < n; ++i)
	{
		uint x1 = get_uint();
		uint y1 = get_uint();

		uint x2 = get_uint();
		uint y2 = get_uint();

		data_in.push_back(data(i, x1, y2 - y1));
	}

	for (uint i = 0; i < n; ++i)
	{
		uint x1 = get_uint();
		uint y1 = get_uint();

		uint x2 = get_uint();
		uint y2 = get_uint();

		data_out.push_back(data(i, x1, y2 - y1));
	}

	std::sort(data_in.begin(), data_in.end());
	std::sort(data_out.begin(), data_out.end());

	std::vector<node> data_w;

	data_w.reserve(n + 2);

	for (uint i = 0; i < n; ++i)
	{
		data_w.push_back({ w - data_in[i].w });
	}

	segtree<node> tree;

	identity.w = w;

	tree.init(n, data_w.data(), identity);

	std::vector<uint> pos_in(n + 2);

	for (uint i = 0; i < n; ++i)
	{
		pos_in[data_in[i].id] = i;
	}

	for (uint i = 0; i < n; ++i)
	{
		uint j = pos_in[data_out[i].id];

		if (j > 0)
		{
			uint min_w = tree.query(0, j - 1).w;

			if (min_w < data_in[j].w)
			{
				return false;
			}
		}

		tree.update<del>(j);
	}

	return true;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);

	uint t = get_uint();

	for (uint i = 0; i < t; ++i)
	{
		std::cout << (par() ? "TAK\n" : "NIE\n");
	}

	return 0;
}