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
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

struct Car {
	int id;
	int left, height; 
	bool operator < (const Car& that) const
	{
		return left < that.left;
	}
};

struct ITMax {
	int* data;
	int n;
	ITMax(int _n)
	{
		n = _n;
		data = new int[2*n];
		for(int i=0;i<2*n;i++) data[i] = 0;
	}
	void set(int i, int v)
	{
		int p = n+i;
		data[p] = v;
		while(p/=2)
		{
			data[p] = max(data[p*2], data[p*2+1]);
		}
	}
	int getMax(int l, int r)
	{
		return gget(1, 0, n, l, r+1);
	}
private:
	int gget(int p, int boundL, int boundR, int requestL, int requestR)
	{
		if(requestL<boundL) requestL = boundL;
		if(requestR>boundR) requestR = boundR;
		if(requestL>=requestR) return 0;
		
		if(boundL==requestL && boundR==requestR)
			return data[p];
		else
		{
			int mid = boundR-(boundR-boundL)/2;
			return max( gget(p*2, boundL, mid, requestL, requestR)
			, gget(p*2+1, mid, boundR, requestL, requestR) );
		}
	}
};

int main()
{
	int t;
	scanf("%d", &t);
	while(t-->0)
	{
		int n, w;
		scanf("%d %d", &n, &w);
		
		vector<Car> cars1, cars2;
		
		{
			Car c;
			int x1, x2, y1, y2;
			for(int i=0;i<n;i++)
			{
				scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
				c.left = min(x1, x2);
				c.height = abs(y2-y1);
				c.id = i;
				cars1.push_back(c);
			}
			for(int i=0;i<n;i++)
			{
				scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
				c.left = min(x1, x2);
				c.height = abs(y2-y1);
				c.id = i;
				cars2.push_back(c);
			}
		}
		sort(cars1.begin(), cars1.end());
		sort(cars2.begin(), cars2.end());
		
		
		bool ok = true;
		
		map<int, int> s1, s2;
		ITMax it1(1<<16), it2(1<<16);
		for(int i=0;i<n && ok;i++)
		{
			Car& c1 = cars1[i];
			Car& c2 = cars2[i];
			
			if(!s1.count(c1.id) && !s2.count(c1.id))
			{
				s1[c1.id] = i;
				it1.set(i, c1.height);
			}
			if(!s1.count(c2.id) && !s2.count(c2.id))
			{
				s2[c2.id] = i;
				it2.set(i, c2.height);
			}
			
			if(s2.count(c1.id))
			{
				it2.set(s2[c1.id], 0);
				
				int ma = it1.getMax(0, i-1);
				ma = max(ma, it2.getMax(0, s2[c1.id]-1));
				
				s2.erase(c1.id);
				
				ma += c1.height;
				if(ma>w) ok = false;
			}
			if(s1.count(c2.id))
			{
				it1.set(s1[c2.id], 0);
				
				int ma = it2.getMax(0, i-1);
				ma = max(ma, it1.getMax(0, s1[c2.id]-1));
				
				s1.erase(c2.id);
				
				ma += c2.height;
				if(ma>w) ok = false;
			}
		}
		
		printf("%s\n", ok ? "TAK" : "NIE");
	}
}