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
#include <iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int i,j,k,t,b,b2,n,y2,y3,w;
struct z{int x; int h;int nr;};
struct z c[50000],c2[50000];
bool comp(const z& x,const z& y)
{
    return x.x < y.x;
}
bool x;
int main()
{
    cin>>t;
    for(i=0;i<t;i++,x=0){
        cin>>n>>w;
        for(j=0;j<n;j++){
            cin>>b2>>y2>>b>>y3;
            c[j].x=min(b,b2);
            c[j].h=abs(y2-y3);
            c[j].nr=j;
        }
        sort(c,c+n,comp);
        for(j=0;j<n;j++){
            cin>>b2>>y2>>b>>y3;
            c2[j].nr=j;
            c2[j].h=abs(y2-y3);
            c2[j].x=min(b,b2);
        }
        sort(c2,c2+n,comp);
        for(j=0;j<n;j++){
            for(k=0;k<n;k++){
                if(c2[j].nr==c[k].nr){
                    c[k].h=0;
                    k=n;
                }
                else if(c2[j].h+c[k].h>w){
                    k=j=n;
                    x=1;
                }

            }
        }
        if(x)
            cout<<"NIE"<<endl;
        else
            cout<<"TAK"<<endl;
    }
    return 0;
}