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

using namespace std;
const int nn = 65555;
const int nn2 = 32768;
int tab[nn];
int t[nn];
int n;
void clear() {
	for(int i = 0; i < nn; ++i) {
		tab[i] = 0;
	}
}
void set(int j, int v) {
	int in = j + nn2;
	tab[in] = v;
	while (in != 0) {
		in /= 2;
		
		tab[in] = max(tab[in*2 + 1], tab[in*2] );
	}
}
int get(int i, int j) {
	int m1 = nn2 + i;
	int m2 = nn2 + j;
	int actm1 = tab[m1];
	int actm2 = tab[m2];
	while (m1 != m2) {
		int p1 = m1/2;
		int p2 = m2/2;
		if (p1 == m1 * 2) {
			actm1 = tab[p1];
		}
		if (p2 == m2 * 2 + 1) {
			actm2 = tab[p2];
		}
		m1 = p1;
		m2 = p2;
	}
	return max(actm1, actm2);
}
int main() {
	int tt; scanf("%d", &tt);
	for(int i = 0; i < tt; ++i) {
		int w; scanf("%d %d", &n, &w);
		clear();
		vector<pair<pair<int, int>, int> > vp;
		for(int j = 0; j < n; ++j) {
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			vp.push_back(make_pair(make_pair(x1, y2-y1), j + 1));
		}
		vector<pair<pair<int, int>, int> > vp2;
		for(int j = 0; j < n; ++j) {
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			vp2.push_back(make_pair(make_pair(x1, y2-y1), j + 1));
		}
		sort(vp.begin(), vp.end());
		sort(vp2.begin(), vp2.end());
		for(int i = 0; i < n; ++i) {
			set(i + 1, vp2[i].first.second);
			t[vp2[i].second] = i + 1;
		}
	//	for(int i = 0; i < n; ++i) {
		//	printf("%d %d\n", vp[i].second, vp2[i].second);
	//	}
	//	printf("\n");
		int b = 1;
		bool ok = true;
		for(int i = 0; i < n; ++i) {
			int ind = t[vp[i].second];
			int m = get(b, ind-1);
//			printf("%d %d %d\n", vp[i].second, ind,  m);
			if (m + vp2[i].first.second > w) {
				ok = false;
				break;
			}
			set(ind, 0);
		}
		if (ok) {
			printf("TAK\n");
		}
		else {
			printf("NIE\n");
		}
	}

}