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

const int MAX = 50000;

struct wejscie
{
	int pos, h, numer;
	void show()
	{
		printf("%d %d | %d\n", pos, h, numer);
	}
};
bool operator<(wejscie a, wejscie b)
{
	if (a.pos == b.pos)
		return (a.h < b.h);
	return (a.pos < b.pos);
}

int N, H;
wejscie first[MAX], second[MAX];

int main()
{
	int Z;
	cin >> Z;
	while (Z--)
	{
		cin >> N >> H;

		int x1, x2, y1, y2;
		wejscie akt;
		for (int i = 0; i < N; i++)
		{
			cin >> x1 >> y1 >> x2 >> y2;
			akt.pos = min(x1, x2);
			akt.h = abs(y1 - y2);
			akt.numer = i;
			first[i] = akt;
		}
		for (int i = 0; i < N; i++)
		{
			cin >> x1 >> y1 >> x2 >> y2;
			akt.pos = min(x1, x2);
			akt.h = abs(y1 - y2);
			akt.numer = i;
			second[i] = akt;
		}
		sort(second, second + N);
		for (size_t i = 0; i < N; i++)
		{
			first[second[i].numer].numer = i+1;
			second[i].numer = i+1;
		}

		sort(first, first + N);
		/*cout << "FIRST\n";
		for (int i = 0; i < N; i++)
			first[i].show();
		cout << "SEC\n";
		for (int i = 0; i < N; i++)
			second[i].show();*/

		bool ok = true;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < i; j++)
			{
				if (first[j].numer > first[i].numer && first[i].h + first[j].h > H)
				{
					ok = false;
					break;
				}
			}
			if (!ok)
				break;
		}
		if (ok)
			cout << "TAK" << endl;
		else
			cout << "NIE" << endl;
	}
}