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
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <cctype>
#include <cmath>
#include <iostream>
#include <set>
#include <map>
#include <sstream>
#include <typeinfo>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<string> vs;
 
#define SIZE(x) ((int)(x.size()))
#define LET(var, val) typeof(val) var = (val)
#define FOR(k, a, b) for(typeof(a) k = (a); k < (b); ++k)
#define FORR(k, a, b) for(typeof(a) k = (a); k >= (b); --k)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(LET(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define PF push_front
 
const int INF = 2000000001;
const double EPS = 10e-9;
const double PI = acos(-1.0);
 
ostringstream out;

struct Rectangle
{
	int x1;
	int x2;
	int y1;
	int y2;
	int number;
};

bool cmp(Rectangle a, Rectangle b)
{
	return (a.x1 < b.x1);
}

vi barrier[50000];
Rectangle car[50000];
Rectangle final[50000];

bool was_it[50000];

int main()
{
	ios_base::sync_with_stdio(0);
	int t;
	cin >> t;
	while (t--)
	{
		int n, w;
		cin >> n >> w;
		REP(i, n)
		{
			barrier[i].clear();
			was_it[i] = false;
			cin >> car[i].x1 >> car[i].y1 >> car[i].x2 >> car[i].y2;
			car[i].number = i;
		}
		REP(i, n)
		{
			int width = abs(car[i].y1 - car[i].y2);
			FOR(j, i + 1, n)
			{
				if (width + abs(car[j].y1 - car[j].y2) > w) 
				{
					if (car[i].x1 < car[j].x1)
						barrier[i].PB(j);
					else
						barrier[j].PB(i);
				}
			}
		}
		REP(i, n)
		{
			cin >> final[i].x1 >> final[i].y1 >> final[i].x2 >> final[i].y2;
			final[i].number = i;
		}
		sort(final, final + n, cmp);
		bool ok = true;
		REP(i, n)
		{
			int c = final[i].number;
			was_it[c] = true;
			FOREACH(j, barrier[c])
			{
				if (was_it[*j] == true)
				{
					ok = false;
					break;
				}
			}
			if (ok == false)
				break;
		}
		cout << (ok ? "TAK\n" : "NIE\n");

	}
	cout << out.str();
	return 0;
}