//Jakub Staroń
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <stdexcept>

#ifdef COMPILING_HOME
#define DEBUG_MODE
#define HAVE_NEW_STANDARD
#endif

#ifdef HAVE_NEW_STANDARD
#include <unordered_map>
#include <unordered_set>
#else
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace std::tr1;
#endif

#define hash_map      unordered_map
#define hash_multimap unordered_multimap
#define hash_set      unordered_set
#define hash_multiset unordered_multiset

using namespace std;

typedef char int8;
typedef unsigned char uint8;
typedef short int int16;
typedef unsigned short int uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

typedef std::pair<int32,int32> int32_pair;
typedef std::pair<uint32, uint32> uint32_pair;
typedef std::pair<int64,int64> int64_pair;
typedef std::pair<uint64,uint64> uint64_pair;

typedef std::vector<bool> bit_vector;

#ifdef HAVE_NEW_STANDARD
#define let(a,b) auto a = (b)
#else
#define let(a,b) __typeof (b) a = (b)
#endif

#ifdef DEBUG_MODE
#define pause system("pause")
#define clear_screen system("cls")
#define print(x) cerr << #x << " = " << x << endl
#define label_print(label, value) cerr << label << ' ' << value << endl
#define debug(x) x
#define line cerr << "Line " << __LINE__ << endl
#include <cassert>
#else
#define pause
#define clear_screen
#define print(x)
#define label_print(label, value)
#define debug(x)
#define assert(x)
#define line
#endif

#define rep(i,x) for(uint32 i = 0 ; i < (x) ; i++)
#define for_range(i,begin,end) for( let(i, (begin) ) ; i != (end) ; ++i )
#define foreach(i, c) for_range(i, c.begin(), c.end())
#define abs(a) ( (a) < 0 ? -(a) : (a) )
#define all(c) (c).begin(),(c).end()
#define sort_all(x) sort( all(x) )
#define divide(a,b) ( ( (b)%(a) ) == 0 )
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)

#define min(a,b) ( (a) < (b)? (a) : (b) )
#define max(a,b) ( (a) > (b)? (a) : (b) )
#define min3(a,b,c) (  min((a), min((b),(c))) )
#define max3(a,b,c) (  max((a), max((b),(c))) )
#define sig(x) ( (x) == 0 ? 0 : ( (x) < 0 ? -1 : 1 ) )

const double epsilon = 1e-5;

template<class T>
void unique(std::vector<T>& v)
{
	sort_all(v);
	v.resize( std::unique(all(v)) - v.begin() );
}

ostream& newline(ostream& str)
{
	str.put('\n');
	return str;
}

template<typename T1, typename T2>
istream& operator>>(istream& stream, std::pair<T1, T2>& pair)
{
	stream >> pair.first >> pair.second;
	return stream;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& stream, const std::pair<T1, T2>& pair)
{
#ifdef DEBUG_MODE
	stream << "(" << pair.first << ", " << pair.second << ")";
#else
	stream << pair.first << ' ' << pair.second;
#endif
	return stream;
}

template<class _T>
class StaticBinaryTree
{
public:
	typedef _T node_type;
	typedef StaticBinaryTree<node_type> basic_type;
	typedef uint32 index_type;

	uint32 real_size()
	{
		return data_size*2;
	}

	uint32 size()
	{
		return data_size;
	}

protected:
	StaticBinaryTree(uint32 n)
	{
		uint32 PowerOfTwo = 8; 
		while( PowerOfTwo < n)
			PowerOfTwo <<= 1;

		data_size = PowerOfTwo;
		tree.resize(data_size * 2);
	}

#ifdef USE_BIT_OPERATIONS

	bool is_left_son(index_type k)
	{
		return !(k & 1);
	}

	bool is_right_son(index_type k)
	{
		return (k & 1);
	}

	index_type left_sibling(index_type k)
	{
		return ( k & (~1) );
	}

	index_type right_sibling(index_type k)
	{
		return (k | 1);
	}	

	index_type parent(index_type i)
	{
		return i>>1;
	}

	index_type left_son(index_type i)
	{
		return i<<1;
	}

	index_type right_son(index_type i)
	{
		return ( (i<<1) | 1 );
	}
	
#else
	bool is_left_son(index_type k)
	{
		return (k % 2 == 0);
	}

	bool is_right_son(index_type k)
	{
		return (k % 2 == 1);
	}

	index_type left_sibling(index_type k)
	{
		return (k - k%2);
	}

	index_type right_sibling(index_type k)
	{
		return (k - k%2 + 1);
	}

	index_type parent(index_type i)
	{
		return i/2;
	}

	index_type left_son(index_type i)
	{
		return i*2;
	}

	index_type right_son(index_type i)
	{
		return i*2 + 1;
	}

#endif

	index_type index_of_node(uint32 k)
	{
		return k + data_size;
	}

	index_type root()
	{
		return 1;
	}

	bool is_leaf(index_type n)
	{
		return ( n >= data_size && n < data_size*2 );
	}

	void assert_range(uint32 first, uint32 last)
	{
		if(first > last)		
			throw std::logic_error("StaticBinaryTree: first > last!");		
		else if(last >= size())		
			throw std::logic_error("StaticBinaryTree: last >= size()!");		
	}

	void assert_index(uint32 index)
	{
		if(index >= size())		
			throw std::logic_error("StaticBinaryTree: index >= size()");		
	}

	vector<node_type> tree;
	
private:	
	uint32 data_size;
};


class PointIntervalTree: public StaticBinaryTree<int64>
{
public:
    typedef int64 integer_type;

	PointIntervalTree(uint32 n): basic_type(n) {}

	void set(uint32 index, integer_type value)
	{
	    index = index_of_node(index);
		tree[index] = value;		
		while(index != root())
		{
			index = parent(index);
			tree[index] = max(tree[ left_son(index) ], tree[ right_son(index) ]);
		}

	}

	integer_type query(uint32 first, uint32 last)
	{		
		first = index_of_node(first);
		last = index_of_node(last);

		integer_type result = tree[first];

		if(first != last)
			result = max(result, tree[last]);

		while( parent(first) != parent(last) )
		{			
			if( is_left_son(first) )
			{
				result = max(result, tree[ right_sibling(first) ]);
			}

			if( is_right_son(last) )
			{
				result = max(result, tree[ left_sibling(last) ]);
			}
						
			first = parent(first);
			last = parent(last);
		}

		return result;
	}
};

struct data_type
{
	int64 szerokosc;
	int64 poczatek;
	uint32 numer;
};

const int64 infinity = 1000000000000000000LL; 

class Application
{
public:
	Application()
	{
	}

	~Application()
	{
		delete tree;
	}

	void Run()
	{
		WczytajDane();
		Normalize();
		
		vector<data_type> start_data, stop_data;

		rep(i, N)
		{
			auto& pair = start[i];
			int64 szerokosc = pair.second.second - pair.first.second;
			start_data.pb(  data_type({szerokosc, pair.first.first, i}) );
		}

		rep(i, N)
		{
			auto& pair = stop[i];
			int64 szerokosc = pair.second.second - pair.first.second;
			stop_data.pb(  data_type({szerokosc, pair.first.first, i}) );
		}

		sort( all(start_data), 
			[](const data_type& a, const data_type& b)->bool {
				return a.poczatek < b.poczatek;
			}
			);

		sort( all(stop_data), 
			[](const data_type& a, const data_type& b)->bool {
				return a.poczatek < b.poczatek;
			}
			);

		vector<uint32> pozycje_start(N);
		rep(i, N)
			pozycje_start[ start_data[i].numer ] = i;

		rep(i, N)
		{
			tree->set(i, start_data[i].szerokosc);
			//print(start_data[i].szerokosc);
		}
			
		rep(i, N)
		{
			uint32 index = pozycje_start[ stop_data[i].numer ];

			//assert(start_data[index].szerokosc == stop_data[i].szerokosc);
			//assert(start_data[index].numer == stop_data[i].numer);

			int64 maximum;

			if(index > 0)
				maximum = tree->query(0, index - 1);
			else
				maximum = -infinity;

			//print(maximum);

			if(maximum + stop_data[i].szerokosc <= W)
			{
				tree->set(index, -infinity);
			}
			else
			{
				/*print(maximum);
				print(W);
				print(stop_data[i].szerokosc);
				print(index);
				print(i);*/
				cout << "NIE" << newline;
				return;
			}
		}

		cout << "TAK" << newline;

	}

	void Normalize()
	{
		for(auto& pair : start)
		{
			std::pair<int64_pair, int64_pair> copy = pair;
			pair.first.first = min(copy.first.first, copy.second.first);
			pair.first.second = min(copy.first.second, copy.second.second);
			pair.second.first = max(copy.first.first, copy.second.first);
			pair.second.second = max(copy.first.second, copy.second.second);
		}

		for(auto& pair : stop)
		{
			std::pair<int64_pair, int64_pair> copy = pair;
			pair.first.first = min(copy.first.first, copy.second.first);
			pair.first.second = min(copy.first.second, copy.second.second);
			pair.second.first = max(copy.first.first, copy.second.first);
			pair.second.second = max(copy.first.second, copy.second.second);
		}
	}

	void WczytajDane()
	{
		cin >> N >> W;
		tree = new PointIntervalTree(N);
		start.resize(N);
		stop.resize(N);

		rep(i, N)
			cin >> start[i];

		rep(i, N)
			cin >> stop[i];
	}

	uint32 N;
	int64 W;

	PointIntervalTree* tree;

	vector<pair<int64_pair, int64_pair> > start, stop;
};


int main()
{
	ios::sync_with_stdio(0);
	uint32 T;
	cin >> T;
	rep(i, T)
	{
		Application application;
		application.Run();
	}
	return 0;
}