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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

#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,a)    FOR(i,0,a)
#define MP          make_pair
#define PB          push_back
#define ST          first
#define ND          second

using namespace std;

typedef vector<int>             VI;
typedef vector<VI>              VVI;
typedef pair<int, int>          PII;
typedef vector<PII>             VII;
typedef long long int           LL;
typedef unsigned long long int  ULL;

const int MAXN = 50000+5;
const int MAXT = 1<<18;

int n, width;

PII before[MAXN], after[MAXN];
int pos_before[MAXN];
int w[MAXN];

void read() {
    scanf("%d %d", &n, &width);
    int x1, x2, y1, y2;
    REP (i, n) {
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        before[i] = { min(x1, x2), i };
        w[i] = abs(y1-y2);
    }
    REP (i, n) {
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        after[i] = { min(x1, x2), i };
    }
}

int tree[MAXT];
int tree_size;

void make_tree() {
    tree_size = 1;
    while (tree_size < n) {
        tree_size *= 2;
    }
}

void init_tree() {
    REP (i, n) {
        tree[i + tree_size] = w[before[i].ND];
    }
    FORD (i, tree_size-1, 1) {
        tree[i] = max(tree[2*i], tree[2*i+1]);
    }
}

// [va, vb)
int query(int va, int vb) {
    if (va == vb) return 0;
    int res;
    va = va + tree_size;
    vb = vb + tree_size - 1;
    res = max(tree[va], tree[vb]);
    while (va / 2 != vb / 2) {
        if (va % 2 == 0) res = max(res, tree[va+1]);
        if (vb % 2 == 1) res = max(res, tree[vb-1]);
        va /= 2;
        vb /= 2;
    }
    return res;
}

void insert(int va, int val) {
    va = va + tree_size;
    tree[va] = val;
    va /= 2;
    while (va > 0) {
        tree[va] = max(tree[2*va], tree[2*va+1]);
        va /= 2;
    }
}

void solve() {
    sort(before, before + n);
    sort(after, after + n);

    REP (i, n) {
        pos_before[before[i].ND] = i;
    }

    init_tree();

    REP (i, n) {
//cerr << "iter " << i << " " << pos_before[after[i].ND] << " " << w[after[i].ND] << " " << query(0, pos_before[after[i].ND]) << "\n";
        if (query(0, pos_before[after[i].ND]) + w[after[i].ND] > width) {
            printf("NIE\n");
            return;
        }
        insert(pos_before[after[i].ND], 0);
    }
    printf("TAK\n");
}

int main() {
    int testCases;
    scanf("%d", &testCases);

    REP (i, testCases) {
        read();
        make_tree();
        solve();
    }

    return 0;
}