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
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <algorithm>
//#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
//#include <cmath>
#include <set>
#include <map>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b);i>=(e);--i)

#define PB push_back
#define ALL(V) (V).begin(),(V).end()
#define SIZE(V) ((int)(V).size())

#define MP make_pair
#define ST first
#define ND second

#define DBG

#ifdef DBG
	#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
	#define debug(...)
#endif

int __stmp;
#define scanf __stmp=scanf


const int INF = 1000000001;
const int MAX = 50010;

class tree {
public:
	void init(int n) {
		for(size=1;size<n;size*=2);
		val.assign(2*size+3, 0);
	}
	void set(int ind, int v, bool lazy=false) {
		ind += size;
		val[ind] = v;
		if(!lazy)
			for(ind/=2;ind;ind/=2)
				val[ind] = max(val[2*ind], val[2*ind+1]);
	}
	void build() {
		for(int i=size-1;i>0;i--)
			val[i] = max(val[2*i], val[2*i+1]);
	}
	int query(int ind) {
		if(!ind) return 0;
		ind += size;
		int res = 0;
		while((ind-1) & (ind-2))
		{
			if(ind&1) {
				ind--;
				res = max(res, val[ind]);
			} else ind /= 2;
		}
		return max(res, val[ind-1]);
	}
	
private:
	int size;
	VI val;	
};

PII arr[MAX];
int pos[MAX], H[MAX];;
int n, k;
tree T;

int main(int argc, char *argv[]) {
	int z;
	scanf("%d", &z);
	while(z--)
	{
		scanf("%d %d", &n, &k);
		T.init(n);
		REP(i,n)
		{
			int x1, x2, y1, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			arr[i].ST = min(x1, x2);
			arr[i].ND = i;
			H[i] = abs(y1-y2);
		}
		sort(arr, arr+n);
		REP(i,n)
		{
			int id = arr[i].ND;
			T.set(i, H[id], true);
			pos[id] = i;
		}
		T.build();
		REP(i,n)
		{
			int x1, x2;
			scanf("%d %*d %d %*d", &x1, &x2);
			arr[i].ST = min(x1, x2);
			arr[i].ND = i;
		}
		sort(arr, arr+n);
		bool res = true;
		REP(i,n)
		{
			int id = arr[i].ND;
			int h = H[id];
			if(T.query(pos[id]) > k-h) {
				res = false;
				break;
			}
			T.set(pos[id], 0);
		}
		printf("%s\n", res ? "TAK" : "NIE");
	}
	return 0;
}