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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>     
using namespace std;
#define HAT 64 * 1024
#define MOD 39989  

int tests, n, h;
struct car {
	int h, newh, x, index, newhmatch, luck;
}acar;

vector<car> cars, cars2;
int tree[2 * HAT], tree2[2 * HAT];
int res[50001], res2[50001];

inline bool op(car a, car b) {
	return a.h < b.h;
}

inline bool op2(car a, car b) {
	return a.x < b.x;
}

void add(int val, int pos) {
	int v = HAT + pos;
	while (v) {
		tree[v] += val;
		v /= 2;
	}
}

void add2(int val, int pos) {
	int v = HAT + pos;
	while (v) {
		tree2[v] += val;
		v /= 2;
	}
}

int getSum(int pos) {
	int v = HAT + pos;
	int sum = tree[v];
	while (v) {
		if (v % 2 == 0) sum += tree[v + 1];
		v /= 2;
	}
	return sum;
}

int getSum2(int pos) {
	int v = HAT + pos;
	int sum = tree2[v];
	while (v) {
		if (v % 2 == 0) sum += tree2[v + 1];
		v /= 2;
	}
	return sum;
}

int main() {
	srand(time(NULL));
	scanf("%d", &tests);
	for (int test = 0; test < tests; test++) {
		memset(tree, 0, sizeof(tree));
		memset(tree2, 0, sizeof(tree2));
		cars.clear(); cars2.clear();

		scanf("%d %d", &n, &h);
		for (int i = 0; i < n; i++) {
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			acar.h = y2 - y1;
			acar.x = x1;
			acar.index = i;
			acar.luck = rand() % MOD;
			cars.push_back(acar);
		}

		for (int i = 0; i < n; i++) {
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			acar.h = y2 - y1;
			acar.x = x1;
			acar.index = i;
			acar.luck = cars[i].luck;
			cars2.push_back(acar);
		}

		sort(cars.begin(), cars.end(), op);

		for (int i = 0; i < cars.size(); i++) {
			cars[i].newh = i;
			cars2[cars[i].index].newh = i;
			int a = 0; int b = cars.size() - 1; int c;
			while (b - a > 1) {
				c = (a + b) / 2;
				if (cars[i].h + cars[c].h <= h) a = c;
				else b = c;
			}
			if (cars[i].h + cars[b].h > h) c = b;
			if (cars[i].h + cars[a].h > h) c = a;
			if (cars[i].h + cars[b].h <= h) c = b + 1;
			cars[i].newhmatch = c;
			cars2[cars[i].index].newhmatch = c;
		}

		sort(cars.begin(), cars.end(), op2);
		sort(cars2.begin(), cars2.end(), op2);


		for (int i = 0; i < cars.size(); i++) add(cars[i].luck, cars[i].newh);
		for (int i = 0; i < cars.size(); i++) {
			add(-cars[i].luck, cars[i].newh);
			res[cars[i].index] = getSum(cars[i].newhmatch);
		}

		for (int i = 0; i < cars.size(); i++) add2(cars2[i].luck, cars2[i].newh);
		for (int i = 0; i < cars.size(); i++) {
			add2(-cars2[i].luck, cars2[i].newh);
			res2[cars2[i].index] = getSum2(cars2[i].newhmatch);
		}

		bool differs = false;
		for (int i = 0; i < n; i++) {
			if (res[i] != res2[i]) differs = true;
		}

		if (differs) printf("NIE\n");
		else printf("TAK\n");
	}

	

	return 0;
}