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
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

struct Rect {
  long x1,x2;
  long rm;
  long j;
  long lg,rg;
  Rect(long x1, long x2, long rm,long j):x1(x1),x2(x2),rm(rm),j(j) {};  
};

bool compare_x1(const Rect& first, const Rect& second) {
  if (first.x1 < second.x1)
    return true;
  else
    return false;
}

bool compare_j(const Rect& first, const Rect& second) {
  if (first.j < second.j)
    return true;
  else
    return false;
}

int main() {
	long t;
	vector<Rect> all1;
	vector<Rect> all2;
	vector<long> gtmp;

	scanf("%ld\n", &t);

	for (long i=0;i<t;i++) {
		all1.clear();
		all2.clear();
		gtmp.clear();
		bool dasie = true;
		long n, w;
		long x1,y1,x2,y2;
		long x1t,y1t,x2t,y2t;
		long rw, rh, rm;
		
		scanf("%ld %ld\n", &n, &w);

		for (int j=0;j<n;j++) {
			scanf("%ld %ld %ld %ld\n",&x1t,&y1t,&x2t,&y2t);

			x1 = min(x1t,x2t);
			x2 = max(x1t,x2t);
			y1 = min(y1t,y2t);
			y2 = max(y1t,y2t);

			rw = abs(x2-x1);
			rh = abs(y2-y1);
			if ((long long)rw* (long long)rw + (long long)rh*(long long)rh <= (long long)w) {
				rm = min(rw, rh);
			} else {
				rm = rh;
			}
			all1.push_back(Rect(x1,x2,rm,j));
		}

		for (int j=0;j<n;j++) {
			scanf("%ld %ld %ld %ld\n",&x1,&y1,&x2,&y2);
			rw = abs(x2-x1);
			rh = abs(y2-y1);
			if ((long long)rw* (long long)rw + (long long)rh*(long long)rh <= (long long)w) {
				rm = min(rw, rh);
			} else {
				rm = rh;
			}
			all2.push_back(Rect(x1,x2,rm,j));
		}

		sort(all1.begin(),all1.end(),compare_x1);
		sort(all2.begin(),all2.end(),compare_x1);


		for(int j=0;j<n;j++) {
//			printf("wst %ld\n", all1[j].rm);
			vector<long>::iterator up1=upper_bound (gtmp.begin(), gtmp.end(), w-all1[j].rm);
			vector<long>::iterator up2=upper_bound (gtmp.begin(), gtmp.end(), all1[j].rm);
			
			all1[j].lg = gtmp.end() - up1;
//			printf("left greater: %ld\n", all1[j].lg);
			gtmp.insert(up2, all1[j].rm);
		}

		gtmp.clear();

		for(int j=n-1;j>=0;j--) {
//			printf("wst %ld\n", all1[j].rm);
			vector<long>::iterator up1=upper_bound (gtmp.begin(), gtmp.end(), w-all1[j].rm);
			vector<long>::iterator up2=upper_bound (gtmp.begin(), gtmp.end(), all1[j].rm);
			
			all1[j].rg = gtmp.end() - up1;
//			printf("left greater: %ld\n", all1[j].lg);
			gtmp.insert(up2, all1[j].rm);
		}
		gtmp.clear();

		for(int j=0;j<n;j++) {
//			printf("wst %ld\n", all1[j].rm);
			vector<long>::iterator up1=upper_bound (gtmp.begin(), gtmp.end(), w-all2[j].rm);
			vector<long>::iterator up2=upper_bound (gtmp.begin(), gtmp.end(), all2[j].rm);
			
			all2[j].lg = gtmp.end() - up1;
//			printf("left greater: %ld\n", all1[j].lg);
			gtmp.insert(up2, all2[j].rm);
		}

		gtmp.clear();

		for(int j=n-1;j>=0;j--) {
//			printf("wst %ld\n", all1[j].rm);
			vector<long>::iterator up1=upper_bound (gtmp.begin(), gtmp.end(), w-all2[j].rm);
			vector<long>::iterator up2=upper_bound (gtmp.begin(), gtmp.end(), all2[j].rm);
			
			all2[j].rg = gtmp.end() - up1;
//			printf("left greater: %ld\n", all1[j].lg);
			gtmp.insert(up2, all2[j].rm);
		}

		sort(all1.begin(),all1.end(),compare_j);
		sort(all2.begin(),all2.end(),compare_j);

		for(long j=0;j<n; j++) {
			if (all1[j].lg != all2[j].lg || all1[j].rg != all2[j].rg) {
				dasie = false;
				break;
			//	break;
			}
//			printf("%ld %ld %ld %ld %ld\n", all1[j].lg, all1[j].rg, all2[j].lg, all2[j].rg, j);

				//printf("%ld %ld %ld %ld %ld %ld\n", all1[j].x1, all1[j].y1, all1[j].x2, all1[j].y2, all1[j].rm, all1[j].j);
		}


		if(dasie) {
			printf("TAK\n");
		} else {
			printf("NIE\n");
		}
	}

	return 0;
}