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
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxN 50100
#define SIZE ((1<<17)+5)
#define length (1<<16)
using namespace std;

struct car {
    int h, place, nr; /// wyskosc, prawy koniec, lewy koniec
    car(const int &x,const int &y,const int &xx,const int &yy, const int &i) {
        h = y-yy;
        if(h<0) h = -1*h;
        nr = i;
        place = min(x, xx);
    }
};

vector<car> in, out;
vector<int> posit;

struct tree {
    int maxx[SIZE], czapa, ans;

    void new_() {
        czapa = length;
        for(int i=0; i<SIZE; ++i)
            maxx[i]=0;
    }

    void insert_(int msc, int liczba) {
        msc += czapa;
        maxx[msc] = liczba;
        msc/=2;
        while(msc>0){
            maxx[msc] = max(maxx[2*msc], maxx[2*msc +1]);
            msc/=2;
        }
    }

    void solve(int w, int p, int q, int a, int b) {
        if(p == a and q == b) {
            ans = max(ans, maxx[w]);
            return ;
        }
        int m = (p+q)/2;
        if(b <= m) solve(2*w, p, m, a, b);
        else if(a >= m ) solve(2*w+1, m, q, a, b);
            else {
                solve(2*w, p, m, a, m);
                solve(2*w+1, m, q, m, b);
            }
    }

    int query_(int a, int b) {
        ans = 0;
        if(a == b) return 0;
        solve(1, 0, czapa, a, b);
        return ans;
    }
};

tree itree;

bool cmp(car a, car b) {
    if(a.place < b.place) return 1;
    return 0;
}

void solve_test() {
    int n, x, y, xx, yy, W;
    scanf("%d%d", &n, &W);
    posit.resize(n);
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &x, &y, &xx, &yy);
        car wagen = car(x, y, xx, yy, i);
        in.push_back(wagen);
    }
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &x, &y, &xx, &yy);
        car wagen = car(x, y, xx, yy, i);
        out.push_back(wagen);
    }
    sort(in.begin(), in.end(), cmp);
    sort(out.begin(), out.end(), cmp);
    for(int i=0; i<n; ++i)
        posit[out[i].nr] = i;
    for(int i=0; i<n; ++i) {
        int maxx = itree.query_(posit[in[i].nr] +1, n);
        if(maxx + in[i].h > W) {
            printf("NIE\n");
            return ;
        }
        itree.insert_(posit[in[i].nr] , in[i].h);
    }
    printf("TAK\n");
    return ;
}

int main() {
    int testy;
    scanf("%d", &testy);
    while(testy--) {
        itree.new_();
        int rozm = in.size();
        for(int i=0; i<rozm; ++i) {
            out.pop_back();
            in.pop_back();
        }
        solve_test();
    }
    return 0;
}