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
#include <stdio.h>
#include <algorithm>

struct Car
{
	int startX, startY;
	int destX, destY;
	int width, height;
	
	int startPos;
};

Car* cars;

bool sortStart(const int& a, const int& b)
{
	//int aEdge = cars[a].startX + cars[a].width;
	//int bEdge = cars[b].startX + cars[b].width;
	return (cars[a].startX < cars[b].startX);
}

bool sortDest(const int& a, const int& b)
{
	return (cars[a].destX < cars[b].destX);
}


struct Node
{
	int load;
	int sub;
	
	Node() : load(0), sub(0)
	{
	}
};

const int MAX = 65536;

void addWeight(Node* tree, int w, int from, int to)
{
	int l = MAX + from;
	int r = MAX + to;
	
	//printf("ADD: w: %d  (%d %d)\n", w, from, to);
	
	tree[l].load += w;
	tree[l].sub += w;
	if (l != r)
	{
		tree[r].load += w;
		tree[r].sub += w;
	}
	
	while (l >= 1)
	{
		if (l < r - 1)
		{
			if (l % 2 == 0)
			{
				tree[l + 1].load += w;
				tree[l + 1].sub += w;
			}
			
			if (r % 2 == 1)
			{
				tree[r - 1].load += w;
				tree[r - 1].sub += w;
			}
		}
		
		if (r < MAX)
		{
			tree[l].sub = (tree[2 * l].sub > tree[2 * l + 1].sub ? tree[2 * l].sub : tree[2 * l + 1].sub) + tree[l].load;
			tree[r].sub = (tree[2 * r].sub > tree[2 * r + 1].sub ? tree[2 * r].sub : tree[2 * r + 1].sub) + tree[r].load;
		}
		
		l /= 2;
		r /= 2;
	}
}

int query(Node* tree, int from, int to)
{
	if (to < from)
		return 0;
	int l  = MAX + from;
	int r = MAX + to;
	int length = 1;

	int llen = 0;
	int rlen = 0;
	int res = 0;

	while (l >= 1)
	{
		llen += tree[l].load;
		rlen += tree[r].load;
		int max = llen > rlen ? llen : rlen;
		res = max > res ? max : res;

		if (l < r - 1)
		{
			if (l % 2 == 0)
			{
				res = res > tree[l + 1].sub ? res : tree[l + 1].sub;
			}

			if (r % 2 == 1)
			{
				res = res > tree[r - 1].sub ? res : tree[r - 1].sub;
			}
		}

		l /= 2;
		r /= 2;
	}
	
	//printf("query: %d %d = %d\n", from, to, res);
	return res;
}

int main()
{
	int tests;
	scanf("%d", &tests);
	for (int t = 0; t < tests; t++)
	{
		int n, w;
		scanf("%d %d", &n, &w);
		
		cars = new Car[n];
		int* startOrder = new int[n];
		int* destOrder = new int[n];
		
		for (int i = 0; i < n; ++i)
		{
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			
			startOrder[i] = i;
			cars[i].startX = x1 < x2 ? x1 : x2;
			cars[i].startY = y1 < y2 ? y1 : y2;
			cars[i].width = x1 < x2 ? x2 - x1 : x1 - x2;
			cars[i].height = y1 < y2 ? y2 - y1 : y1 - y2;
		}
		
		for (int i = 0; i < n; ++i)
		{
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			
			destOrder[i] = i;
			cars[i].destX = x1 < x2 ? x1 : x2;
			cars[i].destY = y1 < y2 ? y1 : y2;
		}
		
		//MAGIC
		std::sort(startOrder, startOrder + n, sortStart);
		std::sort(destOrder, destOrder + n, sortDest);
		
		Node tree[2 * MAX];
		for (int i = 0; i < n; ++i)
		{
			addWeight(tree, cars[startOrder[i]].height, i, i);
			cars[startOrder[i]].startPos = i;
		}
		
		bool failed = false;
		for (int i = 0; i < n; ++i)
		{
			int car = destOrder[i];
			if (cars[car].startPos >= i)
			{
				if (query(tree, 0, cars[car].startPos - 1) + cars[car].height > w)
				{
					//printf("FAIL: %d: car: %d\n", i, car);
					failed = true;
					break;
				}
			}
			
			addWeight(tree, -cars[car].height, cars[car].startPos, cars[car].startPos);
			
		}
		
		printf("%s\n", failed ? "NIE" : "TAK");
		
		/*for (int i = 0; i < n; ++i)
		{
			printf("%d - from: (%d, %d) to: (%d %d), w: %d, h: %d\n", i, 
			cars[i].startX, cars[i].startY,
			cars[i].destX, cars[i].destY, 
			cars[i].width, cars[i].height);
		}
		
		printf("start: ");
		for (int i = 0; i < n; ++i)
			printf("[%d]%2d ", i, startOrder[i]);
		printf("\n");
		
		printf("dest:  ");
		for (int i = 0; i < n; ++i)
			printf("[%d]%2d ", i, destOrder[i]);
		printf("\n");
		
		printf("hei:   ");
		for (int i = 0; i < n; ++i)
			printf("[%d]%2d ", i, tree[MAX + i].load);
		printf("\n");*/
		
		
		delete[] startOrder;
		delete[] destOrder;
		delete[] cars;
	}
	
	return 0;
}