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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define MAX 65536

using namespace std;

int tree[2*MAX];

struct Car
{
	int indexPre;
	int indexPost;
	int height;
};

void init()
{
	for(int i = 0; i < 2*MAX; i++) tree[i] = 0;
}

void insert(int x, int y)
{
	x += (MAX-1);
	tree[x] = y;
	x/=2;
	while(x != 0)
	{
		tree[x] = max(tree[2*x], tree[2*x+1]);
		x/=2;
	}
}

int maxIn(int s, int e)
{
	s+=(MAX-1);
	e+=(MAX-1);
	int maks = max(tree[s], tree[e]); 
	while(s/2 != e/2)
	{
		if(!(s%2)) maks = max(maks, tree[s/2]);
		if(e%2) maks = max(maks, tree[e/2]);
		s/=2;
		e/=2;
	}

	return maks;
}

bool comparePre(Car a, Car b)
{
	return a.indexPre < b.indexPre;
}

bool comparePostRev(Car a, Car b)
{
	return b.indexPost < a.indexPost;
}

bool solve()
{
	int n, w;
	cin >> n >> w;
	
	vector<Car> cars;
	init();
	for(int i = 0; i < n; i++)
	{
		Car car;
		int x1,x2,y1,y2;
		cin >> x1 >> y1 >> x2 >> y2;
		car.indexPre = min(x1, x2);
		car.height = abs(y1-y2);
		cars.push_back(car);
	}

	for(int i = 0; i < n; i++)
	{
		int x1,x2,y1,y2;
		cin >> x1 >> y1 >> x2 >> y2;
		cars[i].indexPost=min(x1,x2);
	} 			 
	
	sort(cars.begin(), cars.end(), comparePre);
	for(int i = 0; i < n; i++) cars[i].indexPre=i+1;

	sort(cars.begin(), cars.end(), comparePostRev);
	
	for(int i = 0; i < n; i++)
	{
		if(maxIn(1, cars[i].indexPre) + cars[i].height > w ) return false;
		insert(cars[i].indexPre, cars[i].height);
	}
	
	return true;
}

int main()
{
	ios::sync_with_stdio(0);
	
	int z;

	cin >> z;

	for(int i = 0; i < z; i++)
	{
		if(solve()) cout << "TAK" << endl;
		else cout << "NIE" << endl;
	}

	return 0;
}