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

#define MAX_CARS 50000
bool endFlag = false;

void sortpair(int& c1, int& c2){
	if (c1 > c2){
		int tmp = c1;
		c1 = c2;
		c2 = tmp;
	}
}

class car{
public:
	int px1,py1,px2,py2; // poczatkowe
	int x1,y1,x2,y2;     // koncowe
	static bool endFlag;
	bool operator<(const car& right)const{
		if (px1 != right.px1){
			return px1 < right.px1;
		}
		return py1 < right.py1;
	}
	bool operator>(const car& right)const{
		if (x1 != right.x1){
			return x1>right.x1;
		}
		return y1>right.y1;
	}
	int getW(){
		return y2 - y1;
	}
	void normalize(){
		sortpair(x1, x2);
		sortpair(px1, px2);
		sortpair(y1, y2);
		sortpair(py1, py2);
	}

};

car cars[MAX_CARS];

bool insertionSort(car* A, int n, int w){
	for (int i = 1; i<n; i++){
		car key = A[i];
		int j;
		for (j = i - 1; j >= 0 && A[j]>key; j--){
			if (A[j].getW() + key.getW() > w){
				return false;
			}
			if (A[j].x1 < key.x2 && A[j].y1 < key.y2 && A[j].y2 > key.y1){
				return false;
			}
			A[j + 1] = A[j];
		}
		A[j + 1] = key;
	}
	return true;
}

int main(){
	int t,n,w;	
	cin.sync_with_stdio(false);
	cin >> t;
	for(int ti=0;ti<t;ti++){

		cin >> n >> w;
		for(int i=0;i<n;i++){
			cin >> cars[i].px1 >> cars[i].py1 >> cars[i].px2 >> cars[i].py2;
		}
		for(int i=0;i<n;i++){
			cin >> cars[i].x1 >> cars[i].y1 >> cars[i].x2 >> cars[i].y2;
			cars[i].normalize();
		}
		sort(cars,cars+n);
		if (insertionSort(cars, n, w)){
			cout << "TAK" << endl;
		}
		else{
			cout << "NIE" << endl;
		}
	}
	return 0;
}