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

#define MAX_T 50000

struct rect {
	int x, y, w, h, id; /// (x,y) gornego lewego wierzcholka
	rect* D;

	void read (int _id){
		int x1, y1, x2, y2;
		scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);
		x = min (x1, x2);
		y = max (y1, y2);
		w = abs (x1 - x2);
		h = abs (y1 - y2);
		id = _id;
	}

	void write (){
		printf ("%d : %d %d %d %d\n", id, x, y, w, h);
	}
};

bool cmp_by_x (const rect &a,const rect &b){
	return (a.x < b.x);
}


int n, w;
rect S [MAX_T]; ///  source
rect D [MAX_T]; ///  destination

void mysort (rect* T){
	/// wpierw posortuj po x
	sort (T, T + n, cmp_by_x);

	/// modyfikacja bubblesorta
	bool swaped = true;
	for (int i = 0; i < n && swaped; i++){
		swaped = false;
		for (int j = 1; j < n; j++){
			if (T[j - 1].h > w - T[j].h)
				continue;
			/// zamienialne

			if (T[j - 1].h < T[j].h)
				continue;

			if (
				( (T[j - 1].h == T[j].h) && (T[j - 1].id > T[j].id) )
			 ||   (T[j - 1].h > T[j].h)
			){
				swap (T[j - 1], T[j]);
				swaped = true;
			}
		}
	}
}


bool is_S_equal_D_by_id (){
	for (int i = 0; i < n; i++)
		if (S[i].id != D[i].id)
			return false;
	return true;
}

bool solve ()
{
	mysort (S);
	mysort (D);
	return is_S_equal_D_by_id ();
}


int main ()
{
	int t;
	scanf ("%d", &t);
	while (t--)
	{
		scanf ("%d%d", &n, &w);
		for (int i = 0; i < n; i++)
			S[i].read (i);
		for (int i = 0; i < n; i++)
			D[i].read (i);

//for (int i = 0; i < n; i++) S[i].write ();
//for (int i = 0; i < n; i++) D[i].write ();

		printf ("%s\n", (solve () ? "TAK" : "NIE"));	
	}

	return 0;
}