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 <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <typeinfo>
#include <vector>

#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;
#define rep(i,b) for(auto i=(0);i<(b);++i)
#define fo(i,a,b) for(auto i=(a);i<=(b);++i)
#define ford(i,a,b) for(auto i=(a);i>=(b);--i)
#define fore(a,b) for(auto &a:(b))
#define v vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define clr(x,a) memset(x,a,sizeof(x))

template <typename T, size_t N> struct ma : array<T,N> { T& operator[](size_t n) {
#ifdef DEBUG
           assert(n < N);
#endif
return (*static_cast<array<T,N>*>(this))[n]; } } ; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef ostringstream oss; typedef istringstream iss;
template<class T> string to_str(const T &a) { oss os; os << a; return os.str(); }
template<> string to_str<ld>(const ld& a) { oss os; os.precision(10); os.setf(ios::fixed); os << a; return os.str(); }
template<class T> T from_str(const string &s) { iss is; T val; is >> val; return val; }

int cond = (ll)0;
#define db(x...) { if (cond > 0) { cond--; rep (xxx, 1) cerr << __LINE__ << " " << #x << " " << x << endl; cerr.flush(); } }

typedef ma<int, 6> dat;
typedef array<int, 5> dat5;

const int sz = 1<<16;
int max_prz[2*sz];

void setmax(int i, int val) {
    db(i<<" "<<val);
    int p = sz + i;
    max_prz[p] = val;
    p /= 2;
    while (p >= 1) {
        max_prz[p] = max(max_prz[p*2], max_prz[p*2+1]);
        p /= 2;
    }
}

int checkmax(int i) {
    int p = i + sz;
    int res = 0;
    while (p >= 1)
    {
        if ((p ^1) >= 1 && (p ^ 1) < p) res = max(res, max_prz[p^1]);
        p /= 2;
    }
    return res;
}

void solve() { 
    clr(max_prz, 0);
    int n, h;
    scanf("%d%d", &n, &h);
    v<dat> a(n);
    v<dat> b(n);
    rep (i, n) rep (j, 4) scanf("%d", &a[i][j]);
    rep (i, n) rep (j, 4) scanf("%d", &b[i][j]);

    rep (i, n) rep (j, 2) if (a[i][j] > a[i][j+2]) swap(a[i][j], a[i][j+2]);
    rep (i, n) rep (j, 2) if (b[i][j] > b[i][j+2]) swap(b[i][j], b[i][j+2]);

    rep (i, n) a[i][4] = i;
    rep (i, n) b[i][4] = i;
    sort(all(a));
    sort(all(b));
    v<array<int,2>> pos(n);
    rep (i, n) pos[a[i][4]][0] = i;
    rep (i, n) pos[b[i][4]][1] = i;

    rep (i, n) a[pos[i][0]][5] = pos[i][1];
    rep (i, n) b[pos[i][1]][5] = pos[i][0];

    rep (i, n) setmax(i, a[i][3] - a[i][1]);
    bool ok = true;
    rep (i, n) {
        int old_pos = b[i][5];

        int cm = checkmax(old_pos);
        if (cm > h - (b[i][3] - b[i][1]))
                ok = false;
        setmax(old_pos, 0);
    }
    if (ok) cout << "TAK" << endl;
    else cout << "NIE" << endl;
}

int main(int argc, char ** argv) {
    ios::sync_with_stdio(false);
    //  freopen("../1.in","r",stdin); 
    //  freopen("../2.in","r",stdin); 
    //  freopen("../3.in","r",stdin); 
    //  freopen("../A-small.in","r",stdin); freopen("../A-small.out","w",stdout);
    //  freopen("../A-small-attempt0.in","r",stdin);freopen("../A-small-attempt0.out","w",stdout);
    //  freopen("../A-small-attempt1.in","r",stdin);freopen("../A-small-attempt1.out","w",stdout);
    //  freopen("../A-small-attempt2.in","r",stdin);freopen("../A-small-attempt2.out","w",stdout);
    //  freopen("../A-large.in","r",stdin); freopen("../A-large.out","w",stdout)
    vs args;
    int nr_test = -1;
    int t = 1;

    fo (i, 1, argc - 1) args.pb(argv[i]);
    fore (it, args) if (it == "q") cond = 1000;
    fore (it, args) if (it == "all") t = 1000;
    fore (it, args) if (atoi(it.c_str()) > 0) nr_test = atoi(it.c_str());

    scanf("%d", &t);
    fo (i, 1, t) {
        if (nr_test == -1 || nr_test <= i)
            solve();
    }
	return 0;
}