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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int nmax = 1 << 17;
const int inf = 1e9+10;

typedef pair<int, int> pii;

ostream& operator<<(ostream& out, const pii& p) {
	out << '(' << p.first << ',' << p.second << ')';
	return out;
}

struct rect {
	int w, h;
	pii b, a;
};

rect cars[nmax];

int n, h, pow2;

vector<int> before, target;
int beforepos[nmax];

int t[2*nmax];

void calcPow2() {
	pow2 = 1;
	while (pow2 < n) pow2 *= 2;
}

// bool cmpCar(const int lhs, const int rhs, const pii& p) {}
bool cmpCarBefore(const int lhs, const int rhs) { return cars[lhs].b < cars[rhs].b; }
bool cmpCarAfter(const int lhs, const int rhs) { return cars[lhs].a < cars[rhs].a; }

void printCars() {
	for (int i = 0; i < n; i++) {
		cerr << i << " ";
		cerr << cars[i].w << " " << cars[i].h << " ";
		cerr << cars[i].b << " -> " << cars[i].a << " : " << beforepos[i];
		cerr << endl;
	}

	cerr << pow2 << endl;

	cerr << "S: ";
	for (auto x : before) cerr << x << " ";
	cerr << endl;
	cerr << "T: ";
	for (auto x : target) cerr << x << " ";
	cerr << endl;
}

void printT() {
	for (int i = 1; i < 2*pow2; i++) {
		cerr << t[i] << " ";
		if (i == 1 || i == 3 || i == 7 || i == 15) cerr << endl;
	}
}

void initT() {
	for (int i = 0; i < n; i++) { int cid = before[i]; t[pow2+i] = h - cars[cid].h; }
	for (int i = n; i < pow2; i++) { t[pow2+i] = inf; }
	for (int i = pow2-1; i > 0; i--) {
		t[i] = min(t[2*i], t[2*i+1]);
	}
}

void updateT(int idx, int val) {
	t[pow2+idx] = val;
	int i = (pow2+idx) / 2;
	while (i > 0) { t[i] = min(t[2*i], t[2*i+1]); i /= 2; }
}

int getValT(int l, int r, int b, int e, int i) {
	// cerr << "getValT " << l << " " << r << " " << b << " " << e << " " << i << endl;
	if (b > e) return inf;
	if ((l == b) && (r == e)) return t[i];
	int m = (l + r) / 2;

	int res = min(getValT(l, m, b, min(m, e), 2*i), getValT(m+1, r, max(m+1, b), e, 2*i+1));
	return res;
}

void readRect(int& x1, int& y1, int& x2, int &y2) {
	int a1, b1, a2, b2; cin >> a1 >> b1 >> a2 >> b2;
	if (a1 < a2) { x1 = a1; x2 = a2; } else { x1 = a2; x2 = a1; }
	if (b1 < b2) { y1 = b1; y2 = b2; } else { y1 = b2; y2 = b1; }
}

int main() {
	ios_base::sync_with_stdio(false);
	int t_; cin >> t_;
	while (t_--) {
		before.clear();
		cin >> n >> h;
		for (int i = 0; i < n; i++) {
			before.push_back(i);
			// cin >> cars[i].b.first >> cars[i].b.second;
			int ex, ey; // cin >> ex >> ey;
			readRect(cars[i].b.first, cars[i].b.second, ex, ey);
			cars[i].w = ex - cars[i].b.first; cars[i].h = ey - cars[i].b.second;
		}
		for (int i = 0; i < n; i++) {
			target.push_back(i);
			// cin >> cars[i].a.first >> cars[i].a.second;
			int ex, ey; // cin >> ex >> ey;
			readRect(cars[i].a.first, cars[i].a.second, ex, ey);
		}

		if (n == 1) { cout << "TAK\n"; continue; }

		calcPow2();
		sort(before.begin(), before.end(), cmpCarBefore);
		sort(target.begin(), target.end(), cmpCarAfter);
		for (int i = 0; i < n; i++) { beforepos[before[i]] = i; }

		// printCars();
		initT();

		bool ans = true;
		for (auto cid : target) {
			// printT();
			int pos = beforepos[cid];
			// cerr << "c=" << cid << " " << pos << endl;
			if (pos > 0) {
				int window = getValT(0, pow2-1, 0, pos-1, 1);
				// cerr << "min(0," << (pos-1) << ")=" << window << endl;

				if (window < cars[cid].h) { ans = false; break; }
			}
			updateT(pos, inf);
		}

		if (ans) { cout << "TAK\n"; } else { cout << "NIE\n"; }
	}
	return 0;
}