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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
#define REP(i,n) FOR(i,1,(n)+1)
typedef vector<int> vi;
#define pb push_back
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
#define sz size()
#define VAR(n,v) typeof(v) n=(v)
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)
#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

struct car {
	int dy;
	int x, y, i;
	int x2, y2, i2;
	bool init;
	car(int x, int y, int dy): x(x), y(y), dy(dy), init(1) {}
	bool operator<(const car &c) const {
		if(init) {
			if(x2 != c.x2) return x2 < c.x2;
			else return y2 < c.y2;			
		} else {
			if(x != c.x) return x < c.x;
			else return y < c.y;
		}
	}
	void set_goal(int x, int y) {
		x2 = x;
		y2 = y;
	}
};

int main() {
	GET(t);
	while(t--) {
		GET(n);
		GET(d);
		vector<car> cars;
		FOR(i, 0, n) {
			GET(x1);
			GET(y1);
			GET(x2);
			GET(y2);
			if(x2<x1) {
				swap(x1, x2);
				swap(y1, y2);
			}
			cars.pb(car(x1, y1, abs(y1-y2)));
		}
		FOR(i, 0, n) {
			GET(x1);
			GET(y1);
			GET(x2);
			GET(y2);
			if(x2<x1) {
				swap(x1, x2);
				swap(y1, y2);
			}
			cars[i].set_goal(x1, y1);
		}
		sort(ALL(cars));
		FOR(i,0,n) {
			cars[i].i2 = i;
			cars[i].init = 0;
		}
		sort(ALL(cars));
		FOR(i,0,n) {
			cars[i].i = i;
			dprintf("%d -> %d\n", cars[i].i, cars[i].i2);
		}
		
		bool done[50000];
		FOR(i,0,n) done[i] = 0;

		bool ok = true;
		FOR(i,0,n) {
			FOR(j,i+1,n) {
				car &ci = cars[i];
				car &cj = cars[j];
				if(ci.i2 > cj.i2) {
					if(ci.dy + cj.dy <= d) {
						swap(ci, cj);
					} else {
						ok = false;
						break;
					}
				}
			}
			if(!ok) break;
		}
		//bool ok = true;
		//FOR(i,0,n-1) {
			//if(cars[i+1].i2 < cars[i].i2) {
				//ok = false;
				//break;
			//}
		//}
		if(ok) printf("TAK\n");
		else printf("NIE\n");
	}
	return 0;
}