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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <cstdio>
#include <vector>
#include <cassert>
#include <algorithm>
using namespace std;

template<typename T>
class Memory
{
public:
    Memory(int size = 1000000) : top(0) {
        memory.resize(size);
    }
    
    T & get_new() {
        return memory[top++];
    }

private:
    int top;
    vector<T> memory;
};


inline bool overlap(int from1, int to1, int from2, int to2)
{
    return !((from1 < from2 && to1 <= from2) || (to1 > to2 && from1 >= to2));
}


template<int N>
class MaxTree
{
public:
    
    class Node {
    public:
        Node() : max(0), left(0), from(0), to(0), right(0) {}
        int max, from, to;
        Node *left, *right;
    };
    
    MaxTree() { root = init_node(0, N); }
    
    void set_max(int from, int to, int value)
    {
        set_max_rec(from, to, value, root);
    }
    
    void update_max(int where, int value)
    {
        update_max_rec(where, value, root);
    }
    
    int get_max(int from, int to)
    {
        return get_max_rec(from, to, root);
    }
    
    void test_overlap()
    {
        assert(overlap(1, 3, 1, 2));
        assert(overlap(1, 3, 2, 3));
        assert(overlap(1, 3, 0, 4));
        assert(overlap(0, 4, 1, 3));
        assert(!overlap(1, 3, 3, 4));
        assert(!overlap(3, 4, 1, 3));
        assert(!overlap(1, 3, 100, 200));
    }
    
private:
    
    Node *init_node(int from, int to)
    {
        Node & node = memory.get_new();
        node.from = from;
        node.to = to;
        if (from + 1 < to) {
            int mid = from + (to - from) / 2;
            node.left = init_node(from, mid);
            node.right = init_node(mid, to);
        }
        return &node;
    }
    
    void set_max_rec(int from, int to, int value, Node *node)
    {
        if (!node)
            return;
        
        if (!overlap(from, to, node->from, node->to))
            return;
        
        if (value > node->max)
            node->max = value;
        
        set_max_rec(from, to, value, node->left);
        set_max_rec(from, to, value, node->right);
    }
    
    void update_max_rec(int where, int value, Node *node)
    {
        assert(node != NULL);
        
        if (!overlap(where, where + 1, node->from, node->to))
            return;
        
        if (node->from == where && node->to == where + 1) {
            node->max = value;
        } else {
            update_max_rec(where, value, node->left);
            update_max_rec(where, value, node->right);
            node->max = max(node->left->max, node->right->max);
        }
    }
    
    int get_max_rec(int from, int to, Node *node)
    {
        if (!node)
            return 0;
        
        if (!overlap(from, to, node->from, node->to))
            return 0;
        
        if (node->from >= from && node->to <= to) {
            return node->max;
        } else {
            return max(get_max_rec(from, to, node->left), get_max_rec(from, to, node->right));
        }
    }
    
    Memory<Node> memory;
    Node *root;
};

void test_tree1()
{
    MaxTree<32> max_tree;
    max_tree.test_overlap();
    
    int from = 10, to = 20, the_max = 100;
    max_tree.set_max(from, to, the_max);
    
    for (int i = 0; i < 32; i++) {
        for (int j = i + 1; j < 32; j++) {
            if (overlap(i, j, from, to))
                assert(max_tree.get_max(i, j) == the_max);
            else
                assert(max_tree.get_max(i, j) == 0);
        }
    }
}

void test_tree2()
{
    for (int where = 0; where < 100; where++)
    {
        MaxTree<512> max_tree;
        
        int the_max = 100;
        max_tree.update_max(where, the_max);
        
        for (int i = 0; i < 512; i++) {
            for (int j = i + 1; j < 512; j++) {
                if (overlap(i, j, where, where + 1))
                    assert(max_tree.get_max(i, j) == the_max);
                else
                    assert(max_tree.get_max(i, j) == 0);
            }
        }
    }
}

struct Car
{
    Car() {}
    Car(int id, int start_x, int desired_x, int height)
        : id(id), start_x(start_x), desired_x(desired_x), height(height) {}
    int id, start_x, desired_x, height;
};

struct CmpCars_desired_x
{
    bool operator() (Car *c1, Car *c2) {
        return c1->desired_x < c2->desired_x;
    }
};

struct CmpCars_start_x
{
    bool operator() (Car *c1, Car *c2) {
        return c1->start_x < c2->start_x;
    }
};

bool alg()
{
    //vector<int> start_x, desired_x, height;
    MaxTree<100000> max_tree;
    
    vector<Car*> cars;
    
    int n, w;
    scanf("%d%d", &n, &w);
    
    Memory<Car> car_memory(n);
    
    for (int i = 0; i < n; i++) {
        int x1, y1, x2, y2;
        
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        
        if (x2 < x1)
            swap(x1, x2);

        int height = abs(y2 - y1);
        
        Car &new_car = car_memory.get_new();
        new_car = Car(i, x1, 0, height);
        cars.push_back(&new_car);
        
       // max_tree.update_max(x1, height);
        
        //max_tree.set_max(x1, x2, height.back());
    }
    
    for(int i = 0; i < n; i++) {
        int x1, y1, x2, y2;
        
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        
        if (x2 < x1)
            swap(x1, x2);
        
        cars[i]->desired_x = x1;
    }
    
    sort(cars.begin(), cars.end(), CmpCars_start_x());
    
    /*
    int zipped_x = -1;
    int prev_start_x = -1;
    for (int i = 0; i < cars.size(); i++) {
        if (cars[i]->start_x != prev_start_x) {
            prev_start_x = cars[i]->start_x;
            zipped_x++;
        }
        
        cars[i]->start_x = zipped_x;
    }*/
    
    for (int i = 0; i < cars.size(); i++) {
        cars[i]->start_x = i;
        max_tree.update_max(i, cars[i]->height);
    }
    
    
    
    sort(cars.begin(), cars.end(), CmpCars_desired_x());
    
    for (int i = 0; i < cars.size(); i++) {
        int max_to_cross = max_tree.get_max(0, cars[i]->start_x);
        if (max_to_cross + cars[i]->height > w)
            return false;
        max_tree.update_max(cars[i]->start_x, 0);
    }
    
    return true;
}


int main()
{
//    test_tree1();
//    test_tree2();
    
    int t;
    scanf("%d", &t);
    
    while (t--) {
        if (alg()) printf("TAK\n");
        else printf("NIE\n");
    }
}