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
#include <stdio.h>

typedef struct {
	int number;
	int height;
	int x;
} t_car;

typedef struct {
	int number;
	int height;
} t_pos;


int Min(int a, int b) {
	return (a<b ? a : b);
}

int Dif(int a, int b) {
	return (a<b ? b-a : a-b);
}

int Sig(int a, int b) {
	return(a<b ? 1 : -1);
}

t_car src[50000];
int  srcp[50000];
t_car dst[50000];
int  dstp[50000];
t_pos all[50000];

int _read(int *n) {
	register char c = 0;
	register int res;
	while (c != EOF && c < '0')
		c=getc_unlocked(stdin);
	(*n) = 0;
	res = 0;
	while (c >= '0' && c <= '9' ) {
		(*n)=(*n)*10 + (c-'0');
		c=getc_unlocked(stdin);
		res |= 1;
	}
	return(res);
}

int cmp (const void *a, const void *b) {
	if(((t_car*)a)->x <  ((t_car*)b)->x)
		return -1;
	if(((t_car*)a)->x >  ((t_car*)b)->x)
		return 1;
	return 0;
}

int cmph (const void *a, const void *b) {
	if(((t_pos*)a)->height >  ((t_pos*)b)->height)
		return -1;
	if(((t_pos*)a)->height <  ((t_pos*)b)->height)
		return 1;
	return 0;
}

int main() {
	int t, n, w;
	int x1, x2, y1, y2;
	int i, j, res;
	t_car *msrc, *mdst;
	_read(&t);
	while(t--) {
		_read(&n);
		_read(&w);
		for(i=0; i<n; i++) {
			_read(&x1);
			_read(&y1);
			_read(&x2);
			_read(&y2);
			(src+i)->number = i;
			(src+i)->height = Dif(y1, y2);
			(src+i)->x      = Min(x1, x2);
		}
		for(i=0; i<n; i++) {
			_read(&x1);
			_read(&y1);
			_read(&x2);
			_read(&y2);
			(dst+i)->number = i;
			(dst+i)->height = Dif(y1, y2);
			(dst+i)->x      = Min(x1, x2);
		}
		qsort(src, n, sizeof(t_car), cmp);
		qsort(dst, n, sizeof(t_car), cmp);

		msrc = src;
		mdst = dst;
		res = 1;
		while(n) {
			if(msrc->number == mdst->number) {
				msrc++;
				mdst++;
				n--;
			} else if(n>1 && (msrc+1)->number == mdst->number && msrc->number == (mdst-1)->number ) {
				if(msrc->height+(msrc+1)->height > w) {
					res = 0;
					break;
				} else {
					msrc+=2;
					mdst+=2;
					n-=2;
				}
			} else {
				break;
			}
		}
		if(res) {
			i = n-1;
			while(n) {
				if((msrc+i)->number == (mdst+i)->number) {
					i--;
					n--;
				} else if(n>1 && (msrc+i)->number == (mdst+i-1)->number && (msrc+i-1)->number == (mdst+i)->number) {
					if((msrc+i)->number+(msrc+i-1)->number > w) {
						res = 0;
						break;
					} else {
						i-=2;
						n-=2;
					}
				} else {
					break;
				}
			}
		}
		for(i=0; res && i<n; i++) {
			(all+i)->number = (msrc+i)->number;
			(all+i)->height = (msrc+i)->height;
			srcp[(msrc+i)->number] = i;
			dstp[(mdst+i)->number] = i;
		}
		qsort(all, n, sizeof(t_pos), cmph);
		for(i=0; res && i<n-1; i++) {
			for(j=i+1; res && (all+i)->height+(all+j)->height>w && j<n; j++) {
				if(Sig((msrc+srcp[(all+i)->number])->x, (msrc+srcp[(all+j)->number])->x) != Sig((mdst+dstp[(all+i)->number])->x, (mdst+dstp[(all+j)->number])->x)) {
					res = 0;
				}
			}
		}
		printf("%s\n", res ? "TAK" : "NIE");
	}
	return 0;
}