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
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<complex>
#include<map>
#include<iomanip>
#include<sstream>
#include<queue>
#include<set>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#include<stack>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define fup FOR
#define fdo FORD
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define siz SZ
#define CLR(x) memset((x), 0, sizeof(x))
#define PB push_back
#define MP make_pair
#define X first
#define Y second 
#define FI X
#define SE Y
#define SQR(a) ((a)*(a))
#define DEBUG 1
#define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cout <<endl; }}
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int>P;
typedef vector<int>VI;
const int INF=1E9+7;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }

const int MAX = (1<<16)+5;
int n, w;
struct A {int w; int x1; int x2; int p;}; A a[MAX];
int W[MAX][17];

bool comp1(const A& a, const A& b) {
	return a.x1<b.x1;
}


bool comp2(const A& a, const A& b) {
	return a.x2<b.x2;
}

int rgt(int w, int i, int j, int lev, int index) {
	if (W[index][lev]<=w) return -1;
	int a=index*(1<<lev), b=(index+1)*(1<<lev)-1;
	if (i>b || j<a) return -1;
	if (lev==0) return index;
	int r = rgt(w,i,j,lev-1, index*2+1);
	return (r!=-1)?r:rgt(w,i,j,lev-1, index*2);
}
int rightmost_greater_than(int w, int i) {
//	FORD(j,i,0) if (a[j].w>w) return j;
	return rgt(w,0,i,16,0);
}

int lmt(int w, int i, int j, int lev, int index) {
	if (W[index][lev]<=w) return -1;
	int a=index*(1<<lev), b=(index+1)*(1<<lev)-1;
	if (i>b || j<a) return -1;
	if (lev==0) return index;
	int r = lmt(w,i,j,lev-1, index*2);
	return (r!=-1)?r:lmt(w,i,j,lev-1, index*2+1);
}

int leftmost_greater_than(int w, int i) {
//	FOR(j,i,n+1) if (a[j].w>w) return j;
	return lmt(w,i,n+1,16,0);
}

void construct() {
	int j=1<<15;
	CLR(W);
	FOR(i,0,n+1) W[i][0]=a[i].w;

	FOR(i,1,16) {
		FOR(k,0,j-1)
			W[k][i]=max(W[k*2][i-1],W[k*2+1][i-1]);
		j/=2;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cout << setprecision(15) << fixed;

	int t; cin>>t;
	FOR(_,1,t) {
		int x1, x2, y1, y2;
		cin>>n>>w;
		FOR(i,1,n) {cin>>x1>>y1>>x2>>y2; a[i].w=y2-y1; a[i].x1=x1;}
		FOR(i,1,n) {cin>>x1>>y1>>x2>>y2; a[i].x2=x1;}
		sort(a+1, a+1+n, comp2);
		FOR(i,1,n) a[i].p=i;
		sort(a+1, a+1+n, comp1);
		a[0].x1=-1; a[0].x2=-1; a[0].p=0; a[0].w=w;
		a[n+1].x1=INF; a[n+1].x2=INF; a[n+1].p=n+1; a[n+1].w=w;
		construct();
		bool fail = false;

		FOR(i,1,n) {
			int l = rightmost_greater_than(w-a[i].w, i-1);
			int r = leftmost_greater_than(w-a[i].w, i+1);

			if (a[l].p>a[i].p || a[r].p<a[i].p) {
				fail = true;
				break;
			}
		}
		cout<<(fail?"NIE":"TAK")<<endl;
	}
	

	return 0;
}