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
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>

typedef long long int64;

using namespace std;

typedef struct {
	int node, from, to, qfrom, qto;
} query;

typedef struct {
	vector<int> data;
	vector<query> queue;
	int len, qb, qe;
} mxt;

inline int rson(int n) {
	return 1 + (n << 1);
}
inline int lson(int n) {
	return n << 1;
}
inline int par(int n) {
	return n >> 1;
}

inline void mxt_init(mxt& m, int len) {
	int n = 1;
	while (n < len) {
		n <<= 1;
	}
	m.data.resize(n << 1, 0);
	query q;
	m.queue.resize(100, q);
	m.len = n;
}

inline void mxt_debug(mxt& m) {
	int len = 1;
	while (len < m.len * 2) {
		for (int i = 0; i < len; ++i) {
			printf("%d ", m.data[len + i]);
		}
		printf("\n");
		len *= 2;
	}
}

inline void mxt_set(mxt& m, int pos, int val) {
	int cur = m.len + pos;
	m.data[cur] = val;
	cur = par(cur);
	while (cur) {
		m.data[cur] = max(m.data[lson(cur)], m.data[rson(cur)]);
		cur = par(cur);
	}
}

inline int mxt_max(mxt& m, int from, int to) {
	int cmax = 0;
	query q;
	q.node = 1;
	q.from = 0;
	q.to = m.len - 1;
	q.qfrom = from;
	q.qto = to;
	m.qe = 1;
	m.qb = 0;
	m.queue[0] = q;
	while (m.qe != m.qb) {
		q = m.queue[m.qb];
		++m.qb;
		if (q.to < q.from) {
			continue;
		}
		if (q.qto < q.from || q.qfrom > q.to) {
			continue;
		}
		if (q.qfrom <= q.from && q.qto >= q.to) {
			cmax = max(cmax, m.data[q.node]);
			continue;
		}
		int mid = q.from + (q.to - q.from) / 2;
		query q2;
		if (q.qfrom <= mid) {
			q2.node = lson(q.node);
			q2.from = q.from;
			q2.to = mid;
			q2.qfrom = q.qfrom;
			q2.qto = min(mid, q.qto);
			m.queue[m.qe] = q2;
			++m.qe;
		}
		if (q.qto >= mid + 1) {
			q2.node = rson(q.node);
			q2.from = mid + 1;
			q2.to = q.to;
			q2.qfrom = max(mid + 1, q.qfrom);
			q2.qto = q.qto;
			m.queue[m.qe] = q2;
			++m.qe;
		}
	}
	return cmax;
}

int main () {
	int t;
	scanf("%d", &t);
	for (int tt = 0; tt < t; ++tt) {
		int n, w;
		scanf("%d %d", &n, &w);
		vector<pair<int, int> > lefts_before, lefts_after;
		vector<int> heights;
		heights.reserve(n + 3);
		vector<int> position(n, 0);
		mxt m;
		mxt_init(m, n);
		for (int i = 0; i < n; ++i) {
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			lefts_before.push_back(make_pair(min(x1, x2), i));
			heights.push_back(abs(y1 - y2));
		}
		sort(lefts_before.begin(), lefts_before.end());
		for (int i = 0; i < n; ++i) {
			position[lefts_before[i].second] = i;
		}
		for (int i = 0; i < n; ++i) {
			mxt_set(m, i, heights[lefts_before[i].second]);
		}
		for (int i = 0; i < n; ++i) {
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			lefts_after.push_back(make_pair(min(x1, x2), i));
		}
		sort(lefts_after.begin(), lefts_after.end());
		bool ok = true;
		for (int i = 0; i < n; ++i) {
			int idx = lefts_after[i].second;
			int pos = position[idx];
			if (!pos || w - mxt_max(m, 0, pos - 1) >= heights[idx]) {
				mxt_set(m, pos, 0);
				continue;
			}
			ok = false;
			break;
		}
		printf(ok ? "TAK\n" : "NIE\n");
	}
	return 0;
}