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

using namespace std;

struct car
{
	long long x1;
	long long x2;
	long long y1;
	long long y2;
	int poz;
};

long long z,n,w,a,b,c,d;
car t[50005];
car ttt[50005];
bool tt[50005];

bool check (int x)
{
	for (int i=0;i<n;i++)
	{
	if (ttt[x].poz==t[i].poz)  { tt[i]=true;  return true; }
	else if ( ( abs(ttt[x].y1-ttt[x].y2) > w- abs(t[i].y1-t[i].y2)) && tt[i]==false  ) { return false;}
	}
}

bool cmp (car a,car b)
{
	return a.x1<b.x1;
	
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin >>z;
	for (int j=0;j<z;j++)
	{
		cin >>n>>w;
		for (int i=0;i<50002;i++) tt[i]=false;
		for (int i=0;i<n;i++)
		{
			cin >>a>>b>>c>>d;
			t[i].poz=i+1;
			if (a<c)	
			{
				t[i].x1=a;
				t[i].y1=b;
				t[i].x2=c;
				t[i].y2=d;	
			}
			else
			{
				t[i].x1=c;
                        	t[i].y1=d;
                        	t[i].x2=a;
                        	t[i].y2=b;
			}
		}
		
		sort (t, t+n,cmp );
		
		for (int i=0;i<n;i++)
                {
                        cin >>a>>b>>c>>d;
                        ttt[i].poz=i+1;
                        if (a<c)
                        {
                                ttt[i].x1=a;
                                ttt[i].y1=b;
                                ttt[i].x2=c;
                                ttt[i].y2=d;
                        }
                        else
                        {
                                ttt[i].x1=c;
                                ttt[i].y1=d;
                                ttt[i].x2=a;
                                ttt[i].y2=b;
                        }
                }
		
	
		sort (ttt,ttt+n,cmp);
		

		for (int i=0;i<n;i++)
		{
			if(check(i) == false )
			{
			cout <<"NIE"<<endl;
			i=n;
			}
			else
			{
			if (i==n-1) cout <<"TAK"<<endl;
			}	
		}
		

	}
	return 0;
}