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
#include<algorithm>
//#include<stdio.h>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ull long long int
using namespace std;
//int debug=0;
//#define dcout debug && cout
class C{
public:
	int h;
	int n;
	int i;
	int an;
};
ostream& operator<<(ostream& os, const C r)
{
	os<<"["<<r.n<<"->"<<r.an<<","<<r.h<<",i:"<<r.i<<"]";
	return os;
}
struct CMP{
	bool operator()(C a,C b){
		return a.n<b.n;
	}
};
struct CMPa{
	bool operator()(C a,C b){
		return a.an<b.an;
	}
};
int main(){
	ios_base::sync_with_stdio(0);
	int n;
	cin>>n;
	while(n--){
		int k,h;
		cin>>k>>h;
		C t[k];
		FOR(i,k){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			t[i].h=abs(b-d);
			t[i].n=min(a,c);
			t[i].i=i;
		}
		FOR(i,k){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			t[i].an=min(a,c);
		}
		CMPa cmpa;
		CMP cmp;
		sort(t,t+k,cmpa);
		FOR(i,k)t[i].i=i;
		sort(t,t+k,cmp);
		
		int r=1<<17;
		int**tree=new int*[17];
		*tree=new int[r];
		FOR(i,r)(*tree)[i]=0;
		FORI(i,1,17)tree[i]=tree[i-1]+(1<<(i-1));
		
		int fail=0;
		
		FOR(i,k){
			//dcout<<t[i]<<endl;
			
			//search max
			int from=t[i].i;
			int lvl=16;
			int mx=tree[lvl][from];
			while(lvl){
				if((from&1)==0){
					mx=max(mx,tree[lvl][from|1]);
				}
				lvl--;
				from>>=1;
			}
			if(t[i].h+mx>h){
				//dcout<<"jest:"<<i<<" ma byc "<<t[i]<<endl;
				//dcout<<"found mx:"<<mx<<endl;
				cout<<"NIE"<<endl;
				fail=1;
				break;
			}
			
			//insert
			int level=16;
			int to=t[i].i;
			tree[level][to]=t[i].h;
			//dcout<<"insert "<<t[i].h<<" to "<<to<<endl;
			while(level--){
				to/=2;
				tree[level][to]=max(tree[level+1][to*2],tree[level+1][to*2+1]);
			}
			//FOR(j,k+1)dcout<<tree[16][j]<<" ";dcout<<endl;
			//FOR(j,k/2+1)dcout<<tree[15][j]<<" ";dcout<<endl;
			//FOR(j,k/4+1)dcout<<tree[14][j]<<" ";dcout<<endl;
			//FOR(j,k/8+1)dcout<<tree[13][j]<<" ";dcout<<endl;
			//dcout<<endl;
		}
		if(!fail)
			cout<<"TAK"<<endl;
	}
	return 0;
}