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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

const int BASE = 1<<17;
const int N = 50001;

struct tri {
	int x, h, in;
	tri(int x, int h, int in) : x(x), h(h), in(in) {
	}
};

bool operator<(tri a, tri b) {
	return a.x < b.x;
}

int tree[BASE*2];
int pos[N], height[N], renum[N];
int t, n, w, x1, x2, y11, y22;
vector<tri> v;

int get_max(int x) {
	int res = tree[BASE + x];
	int in = BASE + x;
	while (in) {
		if (in % 2 == 0) {
			res = max(res, tree[in + 1]);
		}
		in /= 2;
	}
	return res;
}

void add(int x, int h) {
	int in = BASE + x;
	while (in) {
		tree[in] = max(tree[in], h);
		in /= 2;
	}
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &w);
		v.clear();
		for (int i=0; i<n; i++) {
			scanf("%d %d %d %d", &x1, &y11, &x2, &y22);
			v.push_back(tri(min(x1, x2), abs(y11 - y22), i));
		}
		sort(v.begin(), v.end());
		/*
		for (int i=0; i<(int)v.size(); i++) {
			printf("%d %d %d\n", v[i].x, v[i].h, v[i].in);
		}
	// */
		for (int i=0; i<(int)v.size(); i++) {
			renum[v[i].in] = i;
			height[i] = v[i].h;
		}
		v.clear();
		for (int i=0; i<n; i++) {
			scanf("%d %d %d %d", &x1, &y11, &x2, &y22);
			v.push_back(tri(min(x1, x2), abs(y11- y22), renum[i]));
		}
		sort(v.begin(), v.end());
		/*
		for (int i=0; i<(int)v.size(); i++) {
			printf("%d %d %d\n", v[i].x, v[i].h, v[i].in);
		}
	// */
		for (int i=0; i<n; i++) {
			pos[v[i].in] = i;
		}
		bool res = true;
		for (int i=0; i<n; i++) {
	//		printf("%d %d\n", get_max(pos[i]), height[i]);
			if (get_max(pos[i]) + height[i] > w) {
				res = false;
				break;
			}
			add(pos[i], height[i]);
		}
		printf(res ? "TAK\n": "NIE\n");
		for (int i=0; i<BASE*2; i++) {
			tree[i] = 0;
		}
	}
	return 0;
}