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
#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(v, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second 
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", " << pair.Y << ")";}

const int N = 50000;
const int T = 100000;
int n, w, size;
PII x[N], y[N];
int h[N], rx[N];
int t[T*2];

void setValue(int p, int v) {
    p += size;
    while (p > 0) {
        maxi(t[p], v);
        p /= 2;
    }
}

int getValue(int p, int A, int B, int a, int b) {
    if (B <= a || b <= A) return 0;
    if (a <= A && B <= b) return t[p];
    return max(getValue(p*2, A, (A+B)/2, a, b),
            getValue(p*2+1, (A+B)/2, B, a, b));
}

int getValue(int p) {
    return getValue(1, 0, size, p, size);
}



void doit() {
    scanf("%d %d", &n, &w);
    size = 1; while (size < n) size *= 2;
    REP(i, size*2) t[i] = 0;

    int aa, bb, cc;
    REP(i, n) {
        x[i].Y = i;
        scanf("%d %d %d %d", &x[i].X, &aa, &bb, &cc);
        if (bb < x[i].X) x[i].X = bb;
    }
    REP(i, n) {
        y[i].Y = i;
        scanf("%d %d %d %d", &y[i].X, &aa, &bb, &cc);
        if (bb < y[i].X) y[i].X = bb;
        h[i] = abs(cc - aa);
    }

    sort(x, x+n);
    REP(i, n) rx[x[i].Y]=i;
    sort(y, y+n);
    REP(i, n) {
        int id = y[i].Y;
        int hh = getValue(rx[id]);

        if (hh + h[id] > w) {
            printf("NIE\n");
            return;
        }
        setValue(rx[id], h[id]);
    }
    printf("TAK\n");
}

int main(){
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    
    int tt;
    scanf("%d", &tt);
    while(tt--) doit();

    return 0;
}