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
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#else
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#define FOR(x,y,z) for (int x=(y); x<=(z); ++x)
#define FORD(x,y,z) for(int x=(y); x>=(z); --x)
#define REP(x,y) for (int x=0; x<(y); ++x)
#if defined(__GNUC__) && __cplusplus < 201103L
#define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y)
#else
#define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y)
#endif
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x).size())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
const int INF = 1000000001;

inline int Pow2(int exp) { return 1 << exp; }

template<typename T>
inline T Pow2(int exp) { return (T)1 << exp; }

int Log2(ULL x, bool ceil)
{
	unsigned long r;
#ifdef __GNUC__
	r = 63 - __builtin_clzll(x);
#else
	_BitScanReverse64(&r, x);
#endif
	return r + (ceil && x > Pow2<ULL>(r));
}

struct Tree {
	int D;
	VI d;

	Tree(int n);
	void Update(int ix, int value);
	int Get(int ix1, int ix2);
};

Tree::Tree(int n) : D(Pow2(Log2(n, true))), d(D*2) { }

void Tree::Update(int ix, int value)
{
	ix += D;
	d[ix] = max(d[ix], value);
	while (ix > 1) {
		ix /= 2;
		d[ix] = max(d[ix*2], d[ix*2+1]);
	}
}

int Tree::Get(int ix1, int ix2)
{
	ix1 += D;
	ix2 += D;
	int res = d[ix1];
	if (ix1 != ix2) res = max(res, d[ix2]);
	while (ix1/2 != ix2/2) {
		if (ix1%2 == 0) res = max(res, d[ix1+1]);
		if (ix2%2 == 1) res = max(res, d[ix2-1]);
		ix1 /= 2;
		ix2 /= 2;
	}
	return res;
}

struct V {
	int nr;
	int sx1,sx2,dx1,dx2,h;
};

struct Problem {
	int n,H,W;
	vector<V> v;
	Problem(int n, int H) : n(n), H(H), v(n) { }
	bool Go();
	bool Sweep1();
	bool Sweep2();
};

bool Problem::Go()
{
	return Sweep1() && Sweep2();
}

struct BySx1 {
	bool operator()(const V* v1, const V* v2) const
	{
		if (v1->sx1 != v2->sx1) return v1->sx1 < v2->sx1;
		return v1->nr < v2->nr;
	}
};

struct BySx2Dec {
	bool operator()(const V* v1, const V* v2) const
	{
		if (v1->sx2 != v2->sx2) return v1->sx2 > v2->sx2;
		return v1->nr < v2->nr;
	}
};

bool Problem::Sweep1()
{
	vector<V*> evs(n);
	REP(i,n) evs[i] = &v[i];
	sort(ALL(evs), BySx1());
	Tree t(W);
	REP(i,n) {
		V* v = evs[i];
		if (t.Get(v->dx2, W-1) + v->h > H) return false;
		t.Update(v->dx1, v->h);
	}
	return true;
}

bool Problem::Sweep2()
{
	vector<V*> evs(n);
	REP(i,n) evs[i] = &v[i];
	sort(ALL(evs), BySx2Dec());
	Tree t(W);
	REP(i,n) {
		V* v = evs[i];
		if (t.Get(0, v->dx1) + v->h > H) return false;
		t.Update(v->dx1, v->h);
	}
	return true;
}

int main(int argc, char** argv)
{
	int tc;
	scanf("%d", &tc);
	REP(tccc,tc) {
		int n,H;
		scanf("%d%d", &n, &H);
		Problem p(n, H);
		VI xs;
		xs.reserve(4*n);
		REP(i,n) {
			int x1,y1,x2,y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			p.v[i].sx1 = min(x1,x2);
			p.v[i].sx2 = max(x1,x2);
			p.v[i].h = max(y1,y2) - min(y1,y2);
			xs.push_back(x1);
			xs.push_back(x2);
		}
		REP(i,n) {
			int x1,y1,x2,y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			p.v[i].dx1 = min(x1,x2);
			p.v[i].dx2 = max(x1,x2);
			xs.push_back(x1);
			xs.push_back(x2);
		}
		sort(ALL(xs));
		xs.erase(unique(ALL(xs)), xs.end());
		REP(i,n) {
			int* r[4] = { &p.v[i].sx1, &p.v[i].sx2, &p.v[i].dx1, &p.v[i].dx2 };
			REP(j,4) *r[j] = lower_bound(ALL(xs), *r[j]) - xs.begin();
		}
		p.W = SIZE(xs);
		if (p.Go()) printf ("TAK\n");
		else printf("NIE\n");
	}

#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}