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
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

struct Rect
{
    int x, y;
    int dx, dy;
    int w;
};

/*struct Rectcmp2
{
    bool operator()(Rect* r1, Rect* r2)
    {
        if (r1->dx == r2->dx) return r1->dy < r2->dy;
        else return r1->dx < r2->dx;
    }
};*/

struct Rectcmp
{
    bool operator()(Rect* r1, Rect* r2)
    {
        return (r1->x < r2->x);
    }
};

bool check(Rect* r1, Rect* r2)
{
    //cout << "c: " << r1->x << ' ' << r1->y << ", " << r1->dx << ' ' << r1->dy << "; " << r2->x << ' ' << r2->y << ", " << r2->dx << ' ' << r2->dy << endl;
    //if (r1->y <= r2->x) if (r1->dy > r2->dx) cout << "hit1 " << r1->dy << ' ' << r2->dx << endl;
    if (r1->y <= r2->x) return r1->dy > r2->dx;
    //if (r2->y <= r1->x) if (r2->dy > r1->dx) cout << "hit2 " << r2->dy << ' ' << r1->dx << endl;
    if (r2->y <= r1->x) return r2->dy > r1->dx;
    return true;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

    int t, n, w;
    int x1, x2, y1, y2;
    int i, j;
    Rect *r, *r2;
    Rect* unord[50000];
    multimap<int, Rect*> ord;

    cin >> t;
    while (t--)
    {
        cin >> n >> w;
        for (i = 0; i < n; ++i)
        {
            cin >> x1 >> y1 >> x2 >> y2;
            r = new Rect;
            r->x = min(x1, x2);
            r->y = max(x1, x2);
            r->w = abs(y1 - y2);
            unord[i] = r;
        }
        for (i = 0; i < n; ++i)
        {
            cin >> x1 >> y1 >> x2 >> y2;
            r = unord[i];
            r->dx = min(x1, x2);
            r->dy = max(x1, x2);
        }

        sort(unord, unord + n, Rectcmp());
        /*for (i = 0; i < n; ++i)
        {
            r = unord[i];
            cout << r->x << ' ' << r->y << ", " << r->dx << ' ' << r->dy << endl;
        }
        */
        for (i = n-1; i >= 0; --i)
        {
            r = unord[i];
            bool br = false;
            if (r->dx < r->x)
            {
                for (j = i-1; j >= 0; --j)
                {
                    r2 = unord[j];
                    if (r2->y <= r->dx) break;
                    if ((r->w + r2->w > w) && check(r, r2))
                    {
                        br = true;
                        break;
                    }
                }
            }
            else if (r->dx > r->x)
            {
                for (multimap<int, Rect*>::iterator it = ord.lower_bound(r->y); it != ord.end(); ++it)
                {
                    r2 = it->second;
                    if (r2->dx >= r->dy) break;
                    if ((r->w + r2->w > w) && check(r, r2))
                    {
                        br = true;
                        break;
                    }
                }
            }
            if (br)
            {
                //cout << "c: " << r->x << ' ' << r->y << ", " << r->dx << ' ' << r->dy << ", " << r->w << "; " << r2->x << ' ' << r2->y << ", " << r2->dx << ' ' << r2->dy << ", " << r->w << endl;
                break;
            }
            ord.insert(make_pair(r->dx, r));
        }
        cout << (i>=0 ? "NIE" : "TAK") << '\n';

        ord.clear();
        for (i = 0; i < n; ++i) delete unord[i];
    }
}