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
#include<iostream>
#include<vector>
#include<algorithm>

#define VECUINT std::vector<unsigned int>
#define PUII std::pair<unsigned int, unsigned int>
#define VECPUII std::vector<PUII >

struct Potegowe{
	VECUINT tab;
	Potegowe(unsigned int n): tab(n+1,0) {}
	void insert(unsigned int x, unsigned int v){
		for(;x<tab.size()&&tab[x]<v;x+=(x&(-x)))
			tab[x]=v;
	}
	unsigned int query(unsigned int x){
		unsigned int r=0;
		for(;x>0;x-=(x&(-x)))
			r=std::max(r,tab[x]);
		
		return r;
	}
};

int main(){
	std::ios_base::sync_with_stdio(false);

	unsigned int t;
	std::cin>>t;
	
	while(t--){
		unsigned int n,w;
		std::cin>>n>>w;
		
		VECPUII A(n),B(n);
		VECUINT Wysokosc(n),Polozenie(n);
		unsigned int x1,x2,y1,y2;
		for(unsigned int i=0;i<n;++i){
			std::cin>>x1>>y1>>x2>>y2;
			Wysokosc[i]=abs(y2-y1);
			A[i]=PUII(std::min(x1,x2),i);
		}
		std::sort(A.begin(),A.end());
		for(unsigned int i=0;i<A.size();++i)
			Polozenie[A[i].second]=i;
			
		for(unsigned int i=0;i<n;++i){
			std::cin>>x1>>y1>>x2>>y2;
			B[i]=PUII(std::min(x1,x2),i);
		}
		std::sort(B.begin(),B.end());
		Potegowe S(n);
		
		bool sol=true;
		for(unsigned int i=0;i<B.size()&&sol;++i){
			if(Wysokosc[B[i].second]+S.query(n-Polozenie[B[i].second])>w)
				sol=false;
			S.insert(n-Polozenie[B[i].second],Wysokosc[B[i].second]);
		}
		
		std::cout<<(sol?"TAK":"NIE")<<"\n";
	}
	
	return 0;
}