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
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
using namespace std;
typedef pair<int,int> PI;
typedef long long LL;
typedef double D;
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define R(I,N) for(int I=0;I<N;I++)
#define F(I,A,B) for(int I=A;I<B;I++)
#define FD(I,N) for(int I=N-1;I>=0;I--)
#define make(A) scanf("%d",&A)
#define MAX 50001
int x[MAX],p[MAX],p2[MAX],g[MAX],sze[MAX];
int n,w,nn,dp[MAX*3];
bool q(int a,int b){
	return x[a] < x[b];
}
void pob(int *pp){
	R(i,n){
		int x1,y1,x2,y2;
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		if(x1>x2)swap(x1,x2);
		sze[i] = abs(y2 - y1);
		x[i] = x1;
		pp[i] = i;
	}
	sort(pp,pp+n,q);
}
int ma(int ak){
	ak += nn;
	int wyn = 0;
	do{
		if(ak&1)wyn = max(wyn,dp[ak-1]);
		ak/=2;
	}while(ak!=1);
	return wyn;
}
void akt(int ak){
	ak += nn;
	dp[ak] = 0;
	do{
		ak /= 2;
		dp[ak] = max(dp[ak*2],dp[ak*2+1]);
	}while(ak!=1);
}
bool test(){
	make(n);make(w);pob(p);pob(p2);
	R(i,n)g[p[i]] = i;
	nn=1;while(nn<=n)nn*=2;
	R(i,n)dp[i+nn] = sze[p[i]];
	FD(i,nn)dp[i] = max(dp[i*2], dp[i*2+1]);
	R(i,n){
		int nr = p2[i];
		int wyn = 0;
		if(sze[nr] + ma(g[nr]) > w)return 0;
		akt(g[nr]);
	}
	return 1;
}
main(){
	int _;make(_);while(_--)puts(test()?"TAK":"NIE");
}