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
#include <cstdio>
#include <algorithm>
using namespace std;
const int C = 1<<16;
int x[C];
bool cmp(int a, int b) { return x[a]<x[b]; }
int tree[2*C];
void set(int a, int x) {
    a += C;
    tree[a] = x;
    while (a/=2) tree[a] = min(tree[2*a], tree[2*a+1]);
}
int get_min(int a, int b) {
    a += C; b += C;
    int res = min(tree[a],tree[b]);
    while (a/2 != b/2) {
        if (a%2==0) res = min(res, tree[a+1]);
        if (b%2) res = min(res, tree[b-1]);
        a/=2; b/=2;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        for (int i=0; i<2*C; i++) tree[i] = 2e9;
        int n,w;
        scanf("%d%d", &n,&w);
        int y1,y2,a,h[n],t[n],start_pos[n];
        for (int i=0; i<n; i++) {
            scanf("%d%d%d%d", &x[i],&y1,&a,&y2);
            if (a < x[i]) x[i] = a;
            h[i] = abs(y1-y2);
            t[i] = i;
        }
        sort(t,t+n,cmp);
        for (int i=0; i<n; i++) {
            set(i,w-h[t[i]]);
            start_pos[t[i]] = i;
            scanf("%d%d%d%d", &x[i],&y1,&a,&y2);
        }
        sort(t,t+n,cmp);
        bool ans = true;
        for (int i=0; i<n; i++) {
            if (start_pos[t[i]] && get_min(0,start_pos[t[i]]-1) < h[t[i]]) {
                ans = false;
                break;
            }
            set(start_pos[t[i]], 2e9);
        }
        printf(ans?"TAK\n":"NIE\n");
    }
    return 0;
}