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
//Dawid Dworak, PAR, brzydka heura
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#define li long int
using namespace std;

bool fine;
li n;
li w;
li*heights;
/*int compare (const void * a, const void * b)
{
  li x=*(li*)a;
  li y=*(li*)b;
  if ( x<y ) {
        if(heights[x]+heights[y]>w)fine=0;
        return -1;
  }
  if ( x == y ){
    //if(heights[x]+heights[y]>w)fine=0;
    return 0;
  }
  return 1;
}*/
bool compare2 (li a, li b)
{
  if ( a<b ) {
        if(heights[a]+heights[b]>w)fine=0;
        return 0;
  }
  return 1;
}
void possible(){
    multimap<li,pair<li,li> >p; // k-x1,v- (h,i)
    multimap<li,pair<li,li> >k;
    multimap<li,pair<li,li> >::iterator it;
    li*t=new li[n];
    li x1,y1,x2,y2;
    for(li i=0; i<n;i++){
        cin >> x1 >> y1 >> x2 >> y2;
        p.insert(pair<li,pair<li,li> >(min(x1,x2),pair<li,li>(abs(y2-y1),i)));
    }
    for(li i=0; i<n;i++){
        cin >> x1 >> y1 >> x2 >> y2;
        k.insert(pair<li,pair<li,li> >(min(x1,x2),pair<li,li>(abs(y2-y1),i)));
    }
    li i=0;
    heights=new li[n];
    for(it=k.begin(); it!=k.end(); it++){
        t[it->second.second]=i;
        heights[i]=it->second.first;
        i++;
    }
    li*startpoint=new li[n];
    i=0;
    for(it=p.begin(); it!=p.end();it++){
        startpoint[i]=t[it->second.second];
        i++;
    }
    //qsort(startpoint,n,sizeof(li),compare);
    stable_sort(startpoint,startpoint+n,compare2);
    delete startpoint;
    delete t;
    delete heights;
}
int main(){
    li t;
    cin >> t;
    for(li i=0; i<t;i++){
        cin >> n >> w;
        fine=1;
        possible();
        if(fine)cout << "TAK\n";
        else cout << "NIE\n";
    }
}