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

typedef std::pair<std::pair<int, int>, std::pair<int, int>> mypair;

class MaxTree
{
public:
	MaxTree(int _size)
	{
		size = 1;
		do {
			size <<= 1;
		} while ((_size >>= 1) != 0);
		tree.resize(size * 2, 0);
	}

	void set(int w, int v)
	{
		for (w += size, tree[w] = v, w >>= 1; w > 0; w >>= 1) {
			tree[w] = std::max(tree[w << 1], tree[(w << 1) + 1]);
		}
	}

	int maximum(int b, int e) 
	{
		int result = 0;
		b += size; e += size;
		while (b < e) {
			if ((b & 1) == 1) { 
				result = std::max(result, tree[b++]);
			}
			if ((e & 1) == 0) { 
				result = std::max(result, tree[e--]);
			}
			b >>= 1;
			e >>= 1;
		}
		if (b == e) {
			result = std::max(result, tree[b]);
		}
		return result;
	}

private:
	std::vector<int> tree;
	int size;
};



int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, w;
		scanf("%d%d", &n, &w);
		std::vector<std::pair<mypair, int>> cars_before(n);
		std::vector<std::pair<mypair, int>> cars_after(n);
		for (int i = 0; i < n; ++i) {
			int x1, x2, y1, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			cars_before[i].first.first.first = std::min(x1, x2);
			cars_before[i].first.second.first = std::max(x1, x2);
			cars_before[i].first.first.second = std::min(y1, y2);
			cars_before[i].first.second.second = std::max(y1, y2);
			cars_before[i].second = i;
		}
		for (int i = 0; i < n; ++i) {
			int x1, x2, y1, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			cars_after[i].first.first.first = std::min(x1, x2);
			cars_after[i].first.second.first = std::max(x1, x2);
			cars_after[i].first.first.second = std::min(y1, y2);
			cars_after[i].first.second.second = std::max(y1, y2);
			cars_after[i].second = i;
		}
		std::sort(cars_before.begin(), cars_before.end());
		std::sort(cars_after.begin(), cars_after.end());
		std::vector<int> where(n);
		MaxTree tree(n);
		for (int i = 0; i < n; ++i) {
			where[cars_before[i].second] = i;
			tree.set(i, cars_before[i].first.second.second - cars_before[i].first.first.second);
		}
		bool result = true;
		for (int i = 0; i < n - 1; ++i) {
			if (where[cars_after[i].second] != 0 && (w - tree.maximum(0, where[cars_after[i].second] - 1)) < (cars_after[i].first.second.second - cars_after[i].first.first.second)) {
				result = false;
				break;
			}
			tree.set(where[cars_after[i].second], 0);
		}
		printf(result ? "TAK\n" : "NIE\n");
	}
	return 0;
}