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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

class Car{
public:
    int x;
    int h;
    int indeks;
};

vector <int> drzewo;

bool sx(Car l , Car p){
    if(l.x < p.x)
        return true;
    else
        return false;
}

int dwa(int n){
    int pdwa = 2;
    while(pdwa <n)
        pdwa*=2;

    return pdwa;
}

int hmax(int i){
    int maxi = 0;
    while(i>0){
        if(i%2==0)
            maxi = max(maxi , drzewo[i-1]);

        i = (i-1)/2;
    }
    return maxi;
}

void del(int i){
    drzewo[i] = 0;
    while(i>0){
        i = (i-1)/2;
        drzewo[i] = max(drzewo[2*i+1] , drzewo[2*i+2]);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    for(int k=0 ; k<t ;k++){
        drzewo.clear();
        int n;
        cin >> n;
        int w;
        cin >> w;
        vector <Car> start(n);
        int h1,h2,x1,x2;
        for(int i=0;i<n;i++){
            cin >> x1 >> h1 >> x2 >> h2;
            start[i].x = min(x1 , x2);
            start[i].h = abs(h2-h1);
            start[i].indeks=i;
        }
        vector <Car> finish(n);
        for(int i=0;i<n;i++){
            cin >> x1 >> h1 >> x2 >> h2;
            finish[i].x = min(x1 , x2);
            finish[i].h = abs(h2-h1);
            finish[i].indeks=i;
        }
        sort(start.begin() , start.end() , sx);
        sort(finish.begin() , finish.end() , sx);

        vector <int> positions(n);
        for(int i=0 ; i<n ; i++)
            positions[start[i].indeks]=i;

        int pdwa = dwa(n);
        drzewo.resize(pdwa*2-1);

        for(int i = 0 ; i<n ; i++)
            drzewo[i+pdwa-1]=start[i].h;

        for(int i = drzewo.size()-1 ; i>0 ; i-=2)
            drzewo[(i-1)/2] = max(drzewo[i] , drzewo[i-1]);


        for(int i=0 ; i<n ;i++){
            if(positions[finish[i].indeks] == 0){
                del(positions[finish[i].indeks]+pdwa-1);
                if(i == n-1)
                    cout << "TAK" << endl;

                continue;
            }

            if(finish[i].h + hmax(positions[finish[i].indeks]+pdwa-1) > w){
                cout << "NIE " << endl;
                break;
            }

            del(positions[finish[i].indeks]+pdwa-1);

            if(i == n-1)
                cout << "TAK" << endl;
        }
    }
}