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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>

#define INF 1000000001

using namespace std;

struct Car{
    int x1,y1;
    int x2,y2;
    int id;

    Car(){}
    Car(int x1, int y1, int x2, int y2){
        this->x1 = min(x1, x2);
        this->y1 = min(y1, y2);

        this->x2 = max(x1, x2);
        this->y2 = max(y1, y2);
    }

    bool operator<(const Car& that) const{
        return this->x1 < that.x1;
    }
};

struct MaxTree {
    int *elements, size;

    MaxTree(int size) {
        elements = new int[2 * (this->size = 1 << size)];
        for(int x=0; x<2*size; x++) elements[x] = 0;
    }

    ~MaxTree() {
        delete[]elements;
    }

    void Set(int index, int value) {
        for (index += size, elements[index] = value, index >>= 1; index > 0; index >>= 1)
            elements[index] = max(elements[index << 1], elements[(index << 1) + 1]);
    }

    int Find(int left, int right) {
        int m = -INF;
        left += size;
        right += size;
        while (left < right) {
            if ((left & 1) == 1) m = max(m, elements[left++]);
            if ((right & 1) == 0) m = max(m, elements[right--]);
            left >>= 1;
            right >>= 1;
        }
        if (left == right) m = max(m, elements[left]);
        return m;
    }
};

bool tryReorderingCars(Car* carIn, Car* carOut, int carCount, int parkHeight){
    sort(carIn, carIn+carCount);

    for(int i=0; i<carCount; i++){
        carOut[carIn[i].id].id = i;
    }

    sort(carOut, carOut+carCount);
    int treeSize = ceil(log2(carCount))+1;

    MaxTree* tree = new MaxTree(treeSize);

    for(int i=0; i<carCount; i++){
        tree->Set(i, carIn[i].y2 - carIn[i].y1);
    }

    for(int i=0; i<carCount; i++){
        if(carOut[i].id > 0){
            int mx = tree->Find(0, carOut[i].id-1);
            if(parkHeight - mx < carOut[i].y2 - carOut[i].y1){
                delete tree;
                return false;
            }
        }
        tree->Set(carOut[i].id, 0);
    }
    delete tree;
    return true;

}


void readTestCaseAndRun(){
    int carCount, parkHeight;

    scanf("%d%d", &carCount, &parkHeight);

    Car carIn[carCount], carOut[carCount];

    for(int i=0; i<carCount; i++){
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

        carIn[i] = Car(x1, y1, x2, y2);
        carIn[i].id = i;
    }

    for(int i=0; i<carCount; i++){
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

        carOut[i] = Car(x1, y1, x2, y2);
        carOut[i].id = i;
    }

    if(tryReorderingCars(carIn, carOut, carCount, parkHeight))
        printf("TAK\n");
    else
        printf("NIE\n");
}


int main(){
    int testCount;
    scanf("%d", &testCount);

    for(int i=0; i<testCount; i++) readTestCaseAndRun();

    return 0;
}