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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdio>
#include <algorithm>

static const int MAX_N = 50005;
static const bool verbose = false;

struct car {
	int cords[8];
	int cannot_swap;
} cars[MAX_N];

struct event {
	int w;
	int height;
	bool beg;//true = begin, false = end
	int car;
} events[MAX_N*2];

bool cmp(const event & a, const event & b)
{
	if(a.w == b.w)
		return a.beg < b.beg;//najpierw konczace
	return a.w < b.w;
}

int k;
int value[MAX_N];
int values[MAX_N];
int valueCars[MAX_N];


inline int magia(int x){return x - (x & (x-1));}
inline int up(int x){return x + magia(x);}
inline int left(int x){return x - magia(x);}

int getValue(int x) {
	x++;
	int max = 0;
	while(x < MAX_N) {
		if(values[x] > max) {
			max = values[x];
		}
		x = up(x);
	}
	return max;
}
void setValue(int x, int v) {
	int posOrig = x;
	x++;
	while(x > 0) {
		if (values[x] <= v) {
			values[x] = v;
		}
		x = left(x);
	}
}

int findGreather(int v) {
	if (verbose) {
		printf("=============Find %d\n", v);
	
		printf(" Values: ");
		for(int i = 0; i < k; ++i)
			printf("%2d ", getValue(i));
		printf("\n");
	}
	int beg = -1;
	int end = k;
	while ( (end - beg) > 1) {
		int centre = (end + beg) / 2;
		//printf("Test %d   %d  %d\n", centre, k, end - beg);
		if (getValue(centre) > v) {
			beg = centre;
		} else {
			end = centre;
		}
	}
	if (end == 0 && value[end] <= v)
		return -1;
	
	return valueCars[(end + beg) / 2];
}

int insert(int car, int v) {
	setValue(k, v);
	value[k] = v;
	valueCars[k] = car;
	++k;
}

bool processCase()
{
	for (int i = 0; i < MAX_N; ++i){
		values[i] = 0;
		value[i] = 0;
	}

	int n,w;
	scanf("%d%d", &n, &w);
	
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < 4; ++j)
			scanf("%d", cars[i].cords + j);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < 4; ++j)
			scanf("%d", cars[i].cords + j + 4);
		
	for (int i = 0; i < n; ++i) {
		events[2 * i].w = std::min(cars[i].cords[0], cars[i].cords[2]);
		events[2 * i].beg = true;
		events[2 * i].car = i;
		events[2 * i].height = abs(cars[i].cords[3] - cars[i].cords[1]);
		events[2 * i + 1].w = std::max(cars[i].cords[0], cars[i].cords[2]);
		events[2 * i + 1].beg = false;
		events[2 * i + 1].car = i;
		events[2 * i + 1].height = abs(cars[i].cords[3] - cars[i].cords[1]);
	}
	
	std::sort(events, events + 2 * n, cmp);
	k = 0;
	for (int i = 0; i < n * 2; ++i) {
		if (verbose)
			printf("Event pozycja:%d  begin:%d  height:%d  car:%d\n",
			       events[i].w, events[i].beg, events[i].height, events[i].car+1);
		if (events[i].beg) {
			cars[events[i].car].cannot_swap = findGreather(w - events[i].height);
		} else {
			insert(events[i].car, events[i].height);
		}
	}
	
	for (int i = 0; i < n; ++i) {
		if (verbose)
		printf("%d nie moze %d\n", i, cars[i].cannot_swap);
		int oponent = cars[i].cannot_swap; 
		if (oponent >= 0){
			int left = std::min(cars[i].cords[4+0], cars[i].cords[4+2]);
			int right = std::max(cars[oponent].cords[4+0], cars[oponent].cords[4+2]);
			if (verbose)
				printf("	moja lewa %d jego prawa %d \n", left, right);
			if (right > left)
				return false;
		}
	}
	return true;
}

int main()
{
	int t;
	scanf("%d\n", &t);
	while(t--)
		printf(processCase() ? "TAK\n" : "NIE\n");
	return 0;
}