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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <iterator>
#include <tr1/unordered_map>


using namespace std;

typedef vector<int> VI;
typedef long long LL;

#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(x,n) __typeof(n) x = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(c) ((int)(c).size())
#define FOREACH(i,c) for(VAR(i, (c).begin()); i!=(c).end(); ++i)
#define PB push_back
#define PF push_front
#define ST first
#define ND second
#define MP make_pair
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) >= 0 ? (a) : -(a))

struct car {
    int x,h,idx;
};

bool comp(const car& c1, const car& c2){
    return c1.x < c2.x;
}

#define N 65538

car start[N];
car koniec[N];

int interval_tree[2*N];
int id[N];

int main(){
    int t,n,n2,x1,x2,y1,y2,height,ok,va,vb,m;
    scanf("%d", &t);
    REP(i,t){
        scanf("%d %d", &n, &height);
        n2 = 1;
        while(n2<n)
            n2*=2;
        REP(i,n){
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            start[i].x = MIN(x1,x2);
            start[i].h = ABS(y1-y2);
            start[i].idx = i;
        }
        REP(i,n){
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            koniec[i].x = MIN(x1,x2);
            koniec[i].h = ABS(y1-y2);
            koniec[i].idx = i;
        }
        sort(start,start+n,comp);
        sort(koniec,koniec+n,comp);
        FOR(i,n2,2*n2-1)
            interval_tree[i] = 0;
        REP(i,n){
            interval_tree[i+n2] = start[i].h;
            id[start[i].idx] = i;
        }
        FORD(i,n2-1,1){
            interval_tree[i] = MAX(interval_tree[2*i], interval_tree[2*i+1]);
        }
        ok = 1;
        REP(i,n){
            va = n2;
            vb = n2+id[koniec[i].idx]-1;
            if(vb>=n2){
                m = MAX(interval_tree[va],interval_tree[vb]);
                while (va / 2 != vb / 2) {
                    if (va % 2 == 0)
                        m = MAX(m,interval_tree[va + 1]);
                    if (vb % 2 == 1)
                        m = MAX(m,interval_tree[vb - 1]);
                    va /= 2; vb /= 2;
                }
                if(m+koniec[i].h > height){
                    ok = 0;
                    break;
                }
            }
            vb = n2+id[koniec[i].idx];
            interval_tree[vb] = 0;
            vb /= 2;
            while(vb!=0){
                interval_tree[vb] = MAX(interval_tree[2*vb],interval_tree[2*vb+1]);
                vb /= 2;
            }
        }
        if(ok)
            printf("TAK\n");
        else
            printf("NIE\n");
    }
    return 0;
}