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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <unordered_map>
using namespace std;
#define VAR(n,v) typeof(v) n=(v)
#define REP(i,n) for(int i=1; i<=(n); ++i)
#define FOR(i,a,b) for(VAR(i,a); i!=(b); ++i)
#define FORE(it,t) FOR(it,t.begin(),t.end())
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 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 i,x,y,h;
	car(int i=0) : i(i) {}
	car& read() {
		GET(x1); GET(y1); GET(x2); GET(y2);
		x = min(x1, x2);
		y = min(y1, y2);
		h = abs(y1 - y2);
		return *this;
	}
	bool operator<(const car& c) const {
		return x < c.x;
	}
};

int n, w;

vector<car> before, after;
vi afterpos, beforepos;

int g(int i) {
	return ((i ^ (i-1)) +1) / 2;
}

bool comp(const car& x, const car& y) {
	return beforepos[x.i] > beforepos[y.i];
}

struct dpot {
	int n;
	vector<vector<car>> t;

	dpot(int n) : n(n), t(n+1) {}

	void put(int i, car& c) {
		while(i <= n) {
			t[i].pb(c);
			i += g(i);
		}
	}

	void erase(int i) {
		while(i <= n) {
			t[i].pop_back();
			i += g(i);
		}
	}

	void done() {
		for(auto& cars: t) {
			sort(ALL(cars), comp);
			int mx = 0;
			for(auto& car: cars)
				mx = car.h = max(mx, car.h);
		}
	}

	int query(int i) {
		if(i == 0) return 0;
		int result = 0;
		while(i > 0) {
			if(t[i].sz)
				result = max(result, t[i].rbegin()->h);
			i -= g(i);
		}
		return result;
	}
};



void read_cars(vector<car>& cars) {
	cars.clear();
	REP(i,n) cars.pb(car(i).read());
	sort(ALL(cars));
	
	/*for(auto& c: cars)
		printf("(%d,%d,%d,%d) ", c.i, c.x, c.y, c.h);
	printf("\n");*/
}


bool solve() {
	afterpos.clear(); afterpos.resize(n+1);
	beforepos.clear(); beforepos.resize(n+1);
	FOR(i,0,n) {
		afterpos[after[i].i] = i;
		beforepos[before[i].i] = i;
	}

	dpot d(n);
	FOR(i,0,n) d.put(i+1, after[i]);
	d.done();
	//REP(i,n)
	//	dprintf("query(%d) -> %d\n", i, d.query(i));
	for(auto& c: before) {
		int pos = afterpos[c.i];
		dprintf("afterpos %d\n", pos);
		int space = d.query(pos+1-1);
		dprintf("space: %d (%d, %d)\n", space, c.h, w);
		if(space+c.h>w) {
			return false;
		}
		d.erase(pos+1);
	}
	return true;
}

int main() {
	GET(t);
	while(t--) {
		SC(n); SC(w);
		read_cars(before);
		read_cars(after);
		puts(solve()? "TAK":"NIE");
	}
}