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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

int n,w;
VI h;
VI p; // where each number goes

// temp
//VII order1;
VII order;
VI hh;
VI pos;

/*int getInt(){
    char c;
    do{
        c = getchar();
    } while (c < '0' || c > '9');

    int res = 0;
    while( c >= '0' && c <= '9'){
        res = 10*res+c;
        c = getchar();
    }
    return res;
}*/

void readData(){
    scanf("%d %d",&n,&w);
    //n = getInt();
    //w = getInt();
    int x1,y1,x2,y2;
    hh.resize(n);
    
    order.resize(n); 
    REP(i,n){
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        //x1 = getInt(); y1 = getInt(); x2 = getInt(); y2 = getInt();
        if (x1>x2) swap(x1,x2); if (y1>y2) swap(y1,y2);
        order[i] = MP(x1,i);
        hh[i] = y2-y1;
    }
    sort(ALL(order));
    //order1 = order;
    pos.resize(n);
    REP(i,n) pos[order[i].Y] = i;
    h.resize(n);
    REP(i,n) h[i] = hh[order[i].Y]; 

    REP(i,n){
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        //x1 = getInt(); y1 = getInt(); x2 = getInt(); y2 = getInt();
        if (x1>x2) swap(x1,x2); if (y1>y2) swap(y1,y2);
        order[i] = MP(x1,i); 
    }
    sort(ALL(order));
    
    p.resize(n);
    REP(i,n) p[pos[order[i].Y]] = i;
    //print(p);
}

class IntervalTree{
public:
    VI T;
    int M;

    IntervalTree(int n){
        int pow = 32-__builtin_clz(n);
        M = 1 << pow; // 0 is empty
        T.resize(2*M,0);
    }

    void clear(){ T = VI(2*M,0);}

    void maxUpdate(int ind, int val){
        int v = M+ind;
        T[v] = val;
        while(v != 1){
            v /= 2;
            T[v] = max(T[2 * v],T[2 * v + 1]);
        }
    }

    int maxQuery(int ind1, int ind2){
        int v1 = M + ind1, v2 = M + ind2;
        int res = (v1 == v2) ? T[v1] : max(T[v1],T[v2]);
        while (v1 / 2 != v2 / 2) {
            if (v1 % 2 == 0) res = max(res,T[v1 + 1]);
            if (v2 % 2 == 1) res = max(res,T[v2 - 1]);
            v1 /= 2; v2 /= 2;
        }
        return res;
    }
};

int solve_brute(){
    VI sol(n,0);
    REP(i,n){
        int obstacle = *(max_element(sol.begin()+p[i],sol.end()));
        if (obstacle + h[i] > w)
            return 0;
        sol[p[i]] = h[i];
    }
    return 1;
}

int solve(){
    IntervalTree T(n);
    REP(i,n){
        int obstacle = T.maxQuery(p[i],n-1); 
        if (obstacle + h[i] > w){
            //cout << "CAR " << order1[i].Y << " WITH H=" << h[i] << " CANNOT GO PAST H=" << obstacle << endl;
            return 0;
        }
        T.maxUpdate(p[i],h[i]);
    }
    return 1;
}

int main(int argc, char *argv[]){
    int T;
    scanf("%d",&T);
    //T = getInt();
    REP(i,T){
        readData();
        if (solve())
            printf("TAK\n");
        else
            printf("NIE\n");
    }
    return 0;
}