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

const int MAX = 50005;

struct car
{
	int h;
	int x;
	int id;
};

struct poz
{
	int x, y;
	
	void scan()
	{
		scanf("%d%d", &x, &y);
	}
};

bool operator<(car a, car b)
{
	return a.x<b.x;
}

car t[MAX];
car d[MAX];
int f[MAX];
int w;

bool merge(int p, int k)
{
	int n=k-p;
	if (n==1) return 1;
	int s=(p+k)/2;
	
	//dzielę na p<->s, s<->k
	if (!merge(p, s) || !merge(s, k)) return 0;
	int it1=p, it2=s, maxh=0;
	vector<car> v;
	
	while (it1<s && it2<k)
	{
		if (t[it1].h+maxh>w) return 0;
		
		if (t[it1].id<t[it2].id)
		{
			v.push_back(t[it1]);
			it1++;
		}
		else
		{
			maxh=max(maxh, t[it2].h);
			v.push_back(t[it2]);
			it2++;
		}
	}
	
	for (int i=it1; i<s; i++) 
	{
		if (t[i].h+maxh>w) return 0;
		v.push_back(t[i]);
	}
	for (int i=it2; i<k; i++) v.push_back(t[i]);
	for (int i=p; i<k; i++) t[i]=v[i-p];
	
	return 1;
}

int main()
{
	int test, n;
	poz a, b;
	car tmp;
	
	scanf("%d", &test);
	for (int iter=0; iter<test; iter++)
	{
		scanf("%d%d", &n, &w);
		
		for (int i=0; i<n; i++)
		{
			a.scan();
			b.scan();
			if (a.x>b.x) swap(a.x, b.x);
			if (a.y>b.y) swap(a.y, b.y);
			
			tmp.h=b.y-a.y;
			tmp.x=a.x;
			tmp.id=i;
			t[i]=tmp;
		}
		
		for (int i=0; i<n; i++)
		{
			a.scan();
			b.scan();
			if (a.x>b.x) swap(a.x, b.x);
			if (a.y>b.y) swap(a.y, b.y);
			
			tmp.h=b.y-a.y;
			tmp.x=a.x;
			tmp.id=i;
			d[i]=tmp;
			if (d[i].h!=t[i].h) printf("AAAAAAA\n");
		}
		
		sort(t, t+n);
		sort(d, d+n);
		
		//for (int i=0; i<n; i++) printf("%d %d %d\n", t[i].h, t[i].x, t[i].id); printf("\n");
		//for (int i=0; i<n; i++) printf("%d %d %d\n", d[i].h, d[i].x, d[i].id); printf("\n");
		
		for (int i=0; i<n; i++)
		{
			f[d[i].id]=i;
			d[i].id=i;
		}
		
		for (int i=0; i<n; i++)
			t[i].id=f[t[i].id];
		
		//for (int i=0; i<n; i++) printf("%d %d %d\n", t[i].h, t[i].x, t[i].id); printf("\n");
		//for (int i=0; i<n; i++) printf("%d %d %d\n", d[i].h, d[i].x, d[i].id); printf("\n");
		
		if (merge(0, n))
			printf("TAK\n");
		else
			printf("NIE\n");
	}
	
	return 0;
	//ADG
}