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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//Tadrion
#include <cstdio>
#include <vector>
#include <iostream>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define CLEAR(x) (memset(x,0,sizeof(x)))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define VAR(v, n) __typeof(n) v = (n)
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define DBG(v) cout<<#v<<" = "<<v<<endl;
#define IN(x,y) ((y).find(x)!=(y).end())
#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
int tt;
vector<PII> a[2];
int pos[2][200010];
int h[300010];
int n,w;
int xx1,yy1,xx2,yy2;
int xxx1,yyy1,xxx2,yyy2;

const int M = (1<<17);
int t[2*M+2];

void insert(int x, int k) {
    x += M;
    t[x] = k;
    x/=2;
    while(x > 0) {
        t[x] = MAX(t[2*x],t[2*x+1]);
        x/=2;
    }
}

int query(int a, int b) {
    a += M;
    b += M;
    int res = MAX(t[a],t[b]);
    while(a/2 != b/2) {
        if(a%2 == 0) res = MAX(res,t[a+1]);
        if(b%2 == 1) res = MAX(res,t[b-1]);
        a/=2; b/=2;
    }
    return res;
}


int main() {
    scanf("%d",&tt);
    FOR(ttt,1,tt) {
       scanf("%d %d",&n,&w);
       if(n <= 1000) {
       a[0].clear(); a[1].clear();
       FOR(i,0,n-1) {
         scanf("%d %d %d %d",&xxx1,&yyy1,&xxx2,&yyy2);
         xx1 = MIN(xxx1,xxx2);
         xx2 = MAX(xxx1,xxx2);
         yy1 = MIN(yyy1,yyy2);
         yy2 = MAX(yyy1,yyy2);

         int valx1 = 2*xx1, valx2 = 2*xx2;
         if(valx1 > valx2) valx1--; else valx2--;
         a[0].PB(MP(valx1,2*i));
         a[0].PB(MP(valx2,2*i+1));
         h[2*i] = MAX(yy2-yy1,yy1-yy2);
         h[2*i+1] = MAX(yy2-yy1,yy1-yy2);
       }
       FOR(i,0,n-1) {
         scanf("%d %d %d %d",&xxx1,&yyy1,&xxx2,&yyy2);
         xx1 = MIN(xxx1,xxx2);
         xx2 = MAX(xxx1,xxx2);
         yy1 = MIN(yyy1,yyy2);
         yy2 = MAX(yyy1,yyy2);

         int valx1 = 2*xx1, valx2 = 2*xx2;
         if(valx1 > valx2) valx1--; else valx2--;
         a[1].PB(MP(valx1,2*i));
         a[1].PB(MP(valx2,2*i+1));
       }
       sort(a[0].begin(),a[0].end()); sort(a[1].begin(),a[1].end());
       //FOR(i,0,2*n-1) printf("%d %d\n",a[0][i].ST,a[0][i].ND);
       //FOR(i,0,2*n-1) printf("%d %d\n",a[1][i].ST,a[1][i].ND);

       REP(i,SZ(a[0])) pos[0][a[0][i].ND] = i;
       REP(i,SZ(a[1])) pos[1][a[1][i].ND] = i;
       int ok = 1;
       FOR(i,0,2*n-1) FOR(j,i+1,2*n-1) {
            if(pos[0][i] < pos[0][j] && pos[1][i] < pos[1][j]);
            else if(pos[0][i] > pos[0][j] && pos[1][i] > pos[1][j]);
            else {
             //mijamy
             if(h[i] + h[j] > w) {
                   // printf("%d %d %d %d %d\n",i,j,h[i],h[j],w);
                    ok = 0;

             }
            }
       }
        printf("%s\n",ok ? "TAK" : "NIE");
       }
       else {
            a[0].clear(); a[1].clear();
            CLEAR(t);
       FOR(i,0,n-1) {
         scanf("%d %d %d %d",&xxx1,&yyy1,&xxx2,&yyy2);
         xx1 = MIN(xxx1,xxx2);
         xx2 = MAX(xxx1,xxx2);
         yy1 = MIN(yyy1,yyy2);
         yy2 = MAX(yyy1,yyy2);

         int valx1 = 2*xx1, valx2 = 2*xx2;
         if(valx1 > valx2) valx1--; else valx2--;
         a[0].PB(MP(valx1,2*i));
         a[0].PB(MP(valx2,2*i+1));
         h[2*i] = MAX(yy2-yy1,yy1-yy2);
         h[2*i+1] = MAX(yy2-yy1,yy1-yy2);
       }
       FOR(i,0,n-1) {
         scanf("%d %d %d %d",&xxx1,&yyy1,&xxx2,&yyy2);
         xx1 = MIN(xxx1,xxx2);
         xx2 = MAX(xxx1,xxx2);
         yy1 = MIN(yyy1,yyy2);
         yy2 = MAX(yyy1,yyy2);

         int valx1 = 2*xx1, valx2 = 2*xx2;
         if(valx1 > valx2) valx1--; else valx2--;
         a[1].PB(MP(valx1,2*i));
         a[1].PB(MP(valx2,2*i+1));
       }
       sort(a[0].begin(),a[0].end()); sort(a[1].begin(),a[1].end());
       REP(i,SZ(a[0])) pos[0][a[0][i].ND] = i;
       REP(i,SZ(a[1])) pos[1][a[1][i].ND] = i;

       REP(i,2*n-1) {
            insert(i,h[a[0][i].ND]);
       }
       int ok = 1;
       REP(i,2*n-1) {
            if(i > pos[0][a[1][i].ND]) {
                int r = query(i,pos[0][a[1][i].ND]);
                if(r + h[a[1][i].ND] > w) {ok = 0;
                    //printf("%d %d %d\n",r,h[a[1][i].ND],w);
                }
            }
            insert(pos[0][a[1][i].ND],0);
       }


        printf("%s\n",ok ? "TAK" : "NIE");



       }

    }
  return 0;
}