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
// Artur Minorczyk

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PI;
typedef vector<PI> VPI;
typedef long long LL;
typedef vector<LL> VLL;
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
const int INF = 1000000001;
const double EPS = 10e-9;

int TreeHeight(int n)
{
    int a = 1;
    while(a < n) a <<= 1;
    return a;
}
struct MaxTree
{
    int s;
    VI el;
    MaxTree(int n) : s(n), el(2 * n, 0) { }
    void Set(int p, int v)
    {
        for(p += s, el[p] = v, p >>= 1; p > 0; p >>= 1) el[p] = max(el[p << 1], el[(p << 1) + 1]);
    }
    int Find(int p, int k)
    {
        int m = -INF;
        p += s;
        k += s;
        while(p < k)
        {
            if((p & 1) == 1) m = max(m, el[p++]);
            if((k & 0) == 0) m = max(m, el[k--]);
            p >>= 1;
            k >>= 1;
        }
        if(p == k) m = max(m, el[p]);
        return m;
    }
};

struct Point
{
	int x, y;
};
struct Rect
{
	int id;
	Point l, r;
};

bool OrdXY(Rect a, Rect b)
{
	return a.l.x == b.l.x ? a.l.y < b.l.y : a.l.x < b.l.x;
}

int main()
{
	int t, n, w, i, h, lx, ly, rx, ry;
	bool p;
	scanf("%d", &t);
	REP(o, t)
	{
		scanf("%d%d", &n, &w);
		VI order(n);
		vector<Rect> first(n), second(n);
		MaxTree k(TreeHeight(n));
		REP(x, n)
		{
			first[x].id = x;
			scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
			first[x].l.x = min(lx, rx);
			first[x].l.y = min(ly, ry);
			first[x].r.x = max(lx, rx);
			first[x].r.y = max(ly, ry);
		}
		REP(x, n)
		{
			second[x].id = x;
			scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
			second[x].l.x = min(lx, rx);
			second[x].l.y = min(ly, ry);
			second[x].r.x = max(lx, rx);
			second[x].r.y = max(ly, ry);
		}
		sort(ALL(first), OrdXY);
		sort(ALL(second), OrdXY);
		REP(x, n) 
		{
			k.Set(x, first[x].r.y - first[x].l.y);
			order[first[x].id] = x;
		}
		p = true;
		REP(x, n)
		{
			i = second[x].id;
			h = second[x].r.y - second[x].l.y;
			k.Set(order[i], 0);
			if(h + k.Find(0, order[i]) > w)
			{
				p = false;
				break;
			}
		}
		if(p) printf("TAK\n");
		else printf("NIE\n");
	}
	return 0;
}