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

struct car
{
	int spoz, tpoz, sx, sy, tx, ty, h;
	void setStart(int x1, int x2, int y1, int y2)
	{
		sx = min(x1, x2);
		sy = min(y1, y2);
		h = abs(y1 - y2);
	}
	void setTarget(int x1, int x2, int y1, int y2)
	{
		tx = min(x1, x2);
		ty = min(y1, y2);
	}
}Car[50001];
int S[50001], T[50001], H[131100];

bool checkrem(int id, int th, int mxh)
{
	int pos1 = (1 << (th - 1)), pos2 = Car[id].spoz + (1 << (th - 1)) - 1;
	if (Car[id].spoz == 0)
	{
		H[pos1] = 0;
		pos1 >>= 1;
		while (pos1 > 0)
		{
			H[pos1] = max(H[(pos1 << 1)], H[(pos1 << 1) + 1]);
			pos1 >>= 1;
		}
		return true;
	}
	int mx = max(H[pos1], H[pos2]);
	while (pos2 >> 1 != pos1 >> 1)
	{
		if (pos1 % 2 == 0)
			mx = max(H[pos1 + 1], mx);
		if (pos2 % 2 == 1)
			mx = max(H[pos2 - 1], mx);
		pos1 >>= 1, pos2 >>= 1;
	}
	pos1 = Car[id].spoz + (1 << (th - 1));
	H[pos1] = 0;
	pos1 >>= 1;
	while (pos1 > 0)
	{
		H[pos1] = max(H[(pos1 << 1)], H[(pos1 << 1) + 1]);
		pos1 >>= 1;
	}
	return Car[id].h + mx <= mxh;
}

int main()
{
	int t;
	cin >> t;
	for (int t1 = 0; t1 < t; t1++)
	{
		int n, h;
		cin >> n >> h;
		for (int i = 0; i < n; i++)
		{
			int x1, x2, y1, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			Car[i].setStart(x1, x2, y1, y2);
			S[i] = i;
		}
		for (int i = 0; i < n; i++)
		{
			int x1, x2, y1, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			Car[i].setTarget(x1, x2, y1, y2);
			T[i] = i;
		}
		sort(S, S + n, [](int a, int b){if (Car[a].sx == Car[b].sx) return Car[a].sy < Car[b].sy; return Car[a].sx < Car[b].sx; });
		sort(T, T + n, [](int a, int b){if (Car[a].tx == Car[b].tx) return Car[a].ty < Car[b].ty; return Car[a].tx < Car[b].tx; });
		for (int i = 0; i < n; i++)	Car[S[i]].spoz = i, Car[T[i]].tpoz = i;
		int th = [](int n){int ret = 1; while (n)ret++, n >>= 1; return ret; }(n),
			firstel = (1 << (th - 1));
		for (int i = 0; i < n; i++)
			H[firstel + i] = Car[S[i]].h;
		for (int i = firstel + n; i < (firstel << 1); i++)
			H[i] = 0;
		for (int i = firstel - 1; i >= 0; i--)
			H[i] = max(H[(i << 1)], H[(i << 1) + 1]);
		bool failed = false;
		for (int i = 0; i < n; i++)
		{
			failed = false == checkrem(T[i], th, h);
			if (failed)
			{
				cout << "NIE" << endl;
				break;
			}
		}
		if (failed)
			continue;
		cout << "TAK" << endl;
	}
	return 0;
}