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
150
151
152
153
154
155
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int,int> PI;
#define MP make_pair
#define PB push_back
#define SD second
#define FT first

int k,n,m,q;

const int N = 100003;
const int INF = 20000000;

int w;

int h[N];
PI pos[N];
PI oczek[N];
int zyw[N];

int pospos[N];

int values[N];
const int BLOCKSIZE = 200;
class HalfBlockMax {
	public:
		int* data;
		int n;
		
		vector<int>blocks; // [0,200), [200,400) ...
		
		inline int block_num(int i) {
			return i / BLOCKSIZE;
		}
		
		HalfBlockMax(int* d, int na) {
			data=d;
			n = na;
			blocks.clear();
			for(int i=0;i<(n+BLOCKSIZE-1)/BLOCKSIZE;i++) {
				blocks.PB(0);
				for(int i2=i*BLOCKSIZE;i2<(i+1)*BLOCKSIZE&&i2<n;i2++) {
					blocks.back() = max(blocks.back(),data[i2]);
				}
			}
		}
		
		void updateDrop(int x) {
			int myBlock = block_num(x);
			int recompute = blocks[myBlock] == data[x];
			data[x] = 0;
			if(recompute) {
				blocks[myBlock] = 0;
				for(int i=0;i<BLOCKSIZE && i+myBlock*BLOCKSIZE<n;i++) {
					blocks[myBlock] = max(blocks[myBlock],data[i+myBlock*BLOCKSIZE]);
				}
			}
		}
		
		int getMax(int inclusive) {
			if(inclusive < 0) return 0;
			int maks = 0;
			// Blocks
			for(int i=0;i<block_num(inclusive);i++) {
				maks = max(maks,blocks[i]);
			}
			int rest = inclusive % BLOCKSIZE;
			
			for(int i=0;i<=rest;i++) {
				maks = max(maks,data[i+block_num(inclusive)*BLOCKSIZE]);
			}
			return maks;
		}
		
} ;

int main() {
	int d;scanf("%d",&d);
	while(d--) {
		scanf("%d%d",&n,&w);
		for(int i=0;i<n;i++) {
			zyw[i] = 1;
			int x,x2,y1,y2;
			scanf("%d%d%d%d",&x,&y1,&x2,&y2);
			h[i] = max(y2,y1)-min(y2,y1);
			pos[i] = MP(min(x,x2),i);
		}
		for(int i=0;i<n;i++) {
			int x,x2,y1,y2;
			scanf("%d%d%d%d",&x,&y1,&x2,&y2);
			oczek[i] = MP(min(x,x2),i);
		}
		sort(pos,pos+n);
		for(int i=0;i<n;i++) pospos[pos[i].SD] = i;
		sort(oczek,oczek+n);
		
		/*for(int i=0;i<n;i++) {
			printf("h=%d id=%d\n",h[pos[i].SD],pos[i].SD);
		}
		printf("oczek\n");
				for(int i=0;i<n;i++) {
			printf("h=%d id=%d",h[oczek[i].SD],oczek[i].SD);
		}*/
		for(int i=0;i<n;i++) {
			values[i]=h[pos[i].SD];
		}
		
		HalfBlockMax supersuper(values,n);
		
		bool res = false;
		for(int i=0;i<n;i++) {
			// max pos[i],...,pos[iakt]
			int szukany = oczek[i].SD;
			int pozycjaPos = pospos[szukany];
			int maks = 0;
			maks = supersuper.getMax(pozycjaPos-1);
			/*
			int maks2 = 0;
			for(int i2=0;i2<pospos[szukany];i2++) if(zyw[pos[i2].SD]) maks2 = max(maks2,h[pos[i2].SD]);
			
			if(maks != maks2) {
				printf("DUPA!!\n");
				printf("brute = %d, szyb = %d, s=%d,k=%d\n",maks2, maks,0,pozycjaPos);
			}
			//maks = maks2;
			*/
			int sukces = 0;
			if(maks + h[szukany] <=w) {
				sukces = 1;
			}
			
			
			if(!sukces) 
			{
				break;
			}
			else {
				//printf("przenies z %d na %d, ",akt,i);
				supersuper.updateDrop(pozycjaPos);
				zyw[szukany]=0;
			}
			if(i == n-1)
				res = true;
		}
		
		printf("%s\n", res ? "TAK" : "NIE");
	}

	return 0;
}