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
#include <stdio.h>
#include <algorithm>
#define MAX 50011
using namespace std;
//----------------------------------

typedef struct CAR
{
 int Ax, Ay, Bx, By;
 int hi, v, x;
} CAR;
//----------------------------------

CAR mazda6[MAX];
//----------------------------------

int min(int a, int b) { if (a<b) return a; return b;  }
int abees(int a) { if(a<0) return -a; return a; }
char OrdH(const CAR &a, const CAR &b) { return ((a.hi==b.hi)? a.x<b.x : a.hi>b.hi); }
//----------------------------------------------

char check(int x1, int v1, int x2, int v2)
{
   if (x2>x1) { if (x2+v2<x1+v1) return 1; }
   else
    if (x1>x2) { if (x2+v2>x1+v1) return 1; }
return 0;
}
//----------------------------------

void readInt(int *n)
{
  register char c=0;
  while (c < 48) c=getc_unlocked(stdin);
  (*n) = 0;     
  while (c>32) { (*n)=(*n)*10 + (c-48); c=getc_unlocked(stdin); }
}
//------------------------------------

int main(void)
{
  int i, j, k, n, t, w, x1, y1, x2, y2;
  char F;
        
  readInt(&t);
  while(t--)  
  {
    readInt(&n); readInt(&w);
	F = 1;
    
    for(i=0; i<n; i++) 
    {
     readInt(&mazda6[i].Ax); readInt(&mazda6[i].Ay); readInt(&mazda6[i].Bx); readInt(&mazda6[i].By);
	 mazda6[i].hi = abees(mazda6[i].By - mazda6[i].Ay); 
	 mazda6[i].x = min(mazda6[i].Ax, mazda6[i].Bx);
    }

    for(i=0; i<n; i++) 
    {
     readInt(&x1); readInt(&y1); readInt(&x2); readInt(&y2);
	 mazda6[i].v = min(x1, x2) - min(mazda6[i].Ax, mazda6[i].Bx);
    }
    
   sort(mazda6, mazda6+n, OrdH);    
   
   for(i=0; i<n; i++)
   {
     for(j=i+1; j<n; j++)
     {
       if(mazda6[i].hi+mazda6[j].hi>w)
       {
         if (check(mazda6[i].x, mazda6[i].v, mazda6[j].x, mazda6[j].v)) { F=0; break; }
       }
       else break;
     }     
     if (!F) break;
   }
   
   if(!F) puts("NIE");
   else puts("TAK");
  }

return 0;
}
//----------------------------------