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

using namespace std;

struct Car{
	int nr;
	int begin;
	int height;
};

bool sorter(Car x, Car y){
	return x.begin<y.begin;
}

int n,h;
vector <Car> fCars;
vector <Car> sCars; 
Car el;
int posCar[50001];
int rposCar[50001];
int tempTab[50001];
bool res;

void merge(int beg, int mid, int end){
	int i=beg;
	int j=mid+1;
	int k=0;
	while(i<=mid && j<=end){
		if(rposCar[i]<rposCar[j]){
			tempTab[k++]=rposCar[i++];
		}else{
			//printf("--\n");
			if(fCars[rposCar[i]-1].height+fCars[rposCar[j]-1].height > h){
				res=false;
			}
			tempTab[k++]=rposCar[j++];
		}
	}
	while(i<=mid){
		tempTab[k++]=rposCar[i++];
	}
	while(j<=end){
		tempTab[k++]=rposCar[j++];
	}
	k--;
	while(k>=0){
		rposCar[beg+k]=tempTab[k--];
	}
}

void mergeSort(int beg, int end){
	if(beg<end){
		int mid=(beg+end)/2;
		mergeSort(beg,mid);
		mergeSort(mid+1,end);
		merge(beg,mid,end);
	}
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&h);
		int a,b,c,d;
		res=true;
		fCars.clear();
		sCars.clear();
		for(int i=0;i<n;++i){
			scanf("%d%d%d%d",&a,&b,&c,&d);
			el.nr=i+1;
			el.begin=a;
			el.height=d-b;
			fCars.push_back(el);
		}
		for(int i=0;i<n;++i){
			scanf("%d%d%d%d",&a,&b,&c,&d);
			el.nr=i+1;
			el.begin=a;
			el.height=abs(d-b);
			sCars.push_back(el);
		}
		
		sort(fCars.begin(),fCars.end(),sorter);
		sort(sCars.begin(),sCars.end(),sorter);
		
		for(int i=0;i<fCars.size();++i){
			posCar[fCars[i].nr]=i+1;
		}
		for(int i=0;i<sCars.size();++i){
			rposCar[i]=posCar[sCars[i].nr];
		}
		mergeSort(0,n-1);
		if(res){
			printf("TAK\n");
		}else{
			printf("NIE\n");
		}
	}
	return 0;
}