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
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int MaxSize = 50001;

struct Car{
	Car(int num, int x1, int x2, int y1, int y2){
		
		this->num = num;
		this->x1 = min(x1,x2);
		this->x2 = max(x1,x2);
		this->y1 = min(y1,y2);
		this->y2 = max(y1,y2);
		this->w = this->y2-this->y1;
		//printf("\tconstructor number %d (%d)\n",num,this->w);
	}
	
	Car(){
		this->num = 0;
		this->x1 = 0;
		this->x2 = 0;
		this->y1 = 0;
		this->y2 = 0;
		this->w = 0;
	}
	
	friend bool operator<(Car c1, Car c2);
	
	int num;
	int x1;
	int x2;
	int y1;
	int y2;
	int w;
};


bool operator<(Car c1, Car c2){
	return c1.x2<=c2.x1;
}


/*bool compare(const Car &c1, const Car &c2){
	if(c1.x2<=c2.x1)
		return true;
}*/

Car before[MaxSize], after[MaxSize];

int main(void){
	int numTests, numCars, width, x1, x2, y1, y2;
	
	scanf("%d",&numTests);
	
	for(int t=0;t<numTests;++t){
		vector<int> dep[MaxSize];
		bool possible = true;
		
		scanf("%d %d",&numCars,&width);
		
		for(int c=0;c<numCars;++c){
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			before[c] = Car(c,x1,x2,y1,y2);
			//printf("\t=> %d %d\n",c,abs(y2-y1));
		}
		
		// sortowanie po x i y
		//sort(before.begin(),before.end());
		
		//printf("----------------\n");
		//for(int c=0;c<numCars;++c){
		//	printf("\t%d\n",before[c].w);
		//}
		//printf("----------------\n");
		
		/*for(int i=0;i<numCars;++i)
			dep.push_back(vector<int>());*/
		
		// zbudowanie grafu zależności kierunkowej c1 -> c2 jesli c2 na prawo od c1 i nie można ich zamienić miejscami
		for(int i=0;i<numCars-1;++i){			
			for(int j=i+1;j<numCars;++j){
			//	printf("\t(%d,%d)\n",i,j);
				if(before[i].w+before[j].w>width){
					if(before[i]<before[j])
						dep[i].push_back(j);
					else if(before[j]<before[i])
						dep[j].push_back(i);
				}
			}
		}		
		
		/*printf("----------------\n");
		for(int c=0;c<numCars;++c){
			printf("\t%d =>",c);
			for(unsigned i=0;i<dep[c].size();++c)
				printf(" %d",dep[c][i]);
			printf("\n");
		}
		printf("----------------\n");*/
		
		for(int c=0;c<numCars;++c){
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			after[c] = Car(c,x1,x2,y1,y2);
		}
		
		// dla każdego samochodu sprawdzić, czy wszystkie zależne z nim kierunkowo są na prawo od niego
		for(int i=0;i<numCars;++i){
			//printf("\tcar %d\n",i);
			for(unsigned j=0;j<dep[i].size();++j){
				//printf("\tchecking %d -> %d ",i,dep[i][j]);
				if(!(after[i]<after[dep[i][j]])){
					possible = false;
				//	printf("FAIL\n");
					break;
				}
				//printf("OK\n");
			}
		}
		
		if(possible)
			printf("TAK\n");
		else
			printf("NIE\n");
	}
	
	return 0;
}