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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>

struct Car
{
    int x, tox, height;

    bool operator< (const Car& lhs) const
    {
        return x < lhs.x;
    }
};

struct ToxOrder
{
    ToxOrder(const std::vector<Car>& cars) : cars_(cars)
    {
    }
    bool operator() (int lhs, int rhs) const
    {
        return cars_[lhs].tox < cars_[rhs].tox;
    }
    const std::vector<Car>& cars_;
};


struct MaxTree
{
    MaxTree(const std::vector<Car>& cars)
    {
        p_ = 1;
        while (p_ < cars.size())
            p_ *= 2;
        heights_.resize(2 * p_);
        for (size_t i=0; i<cars.size(); ++i)
            heights_[p_ + i] = cars[i].height;
        for (size_t i=p_-1; i>0; --i)
            heights_[i] = std::max(heights_[2*i], heights_[2*i+1]);
    }
    void erase(int x)
    {
        heights_[x += p_] = 0;
        while (x /= 2)
            heights_[x] = std::max(heights_[2*x], heights_[2*x+1]);
    }
    int highest(int x) const
    {
        int ret = 0;
        int p = p_;
        int i = 1;
        for (;;)
        {
            if (x+1 >= p) {
                ret = std::max(ret, heights_[i]);
                break;
            }
            p /= 2;
            if (x < p)
                i = 2*i;
            else {
                ret = std::max(ret, heights_[2*i]);
                i = 2*i + 1;
                x -= p;
            }
        }
        return ret;
    }
private:
    std::vector<int> heights_;
    int p_;
};

void solve()
{
    int n, height;
    scanf("%d%d",&n, &height);

    std::vector<Car> cars(n);
    for (int i=0; i<n; ++i)
    {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d",&x1, &y1, &x2, &y2);
        cars[i].x = std::min(x1, x2);
        cars[i].height = std::abs(y2 - y1);
    }
    for (int i=0; i<n; ++i)
    {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d",&x1, &y1, &x2, &y2);
        cars[i].tox = std::min(x1, x2);
    }

    std::sort(cars.begin(), cars.end());

    std::vector<int> tox_cars(n);
    for (int i=0; i<n; ++i)
        tox_cars[i] = i;
    std::sort(tox_cars.begin(), tox_cars.end(), ToxOrder(cars));

    MaxTree tree(cars);
    for (int i=0; i<n; ++i)
    {
        int position = tox_cars[i];
        int mh = position ? tree.highest(position-1) : 0;
        if (mh + cars[position].height > height)
        {
            std::cout << "NIE" << std::endl;
            return;
        }
        tree.erase(position);
    }
    std::cout << "TAK" << std::endl;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
        solve();

    return 0;
}