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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>

class Offer {
public:
    int w1, w2, h1, h2;
    Offer(): w1(), w2(), h1(), h2() {}
    Offer(int w1, int w2, int h1, int h2): w1(w1), w2(w2), h1(h1), h2(h2) {}

    bool isMajorizing(Offer &o) {
        return w1 <= o.w1 && w2 >= o.w2 && h1 <= o.h1 && h2 >= o.h2;
    }

    bool isMajorized(Offer &o) {
        return o.isMajorizing(*this);
    }

    int getThickness() {
        return (w2 - w1) * (h2 - h1);
    }


};


class Coord {
public:
    Coord(): coord(), offerIds() {}
    int coord;
    std::vector<int> offerIds;

    void updateOuter(int newCoord, int newOfferId) {
        if (newCoord == coord) {
            offerIds.push_back(newOfferId);
        } else if (newCoord > coord) {
            coord = newCoord;
            offerIds.clear();
            offerIds.push_back(newOfferId);
        }
    }

    void updateInner(int newCoord, int newOfferId) {
        if (newCoord == coord) {
            offerIds.push_back(newOfferId);
        } else if (newCoord < coord) {
            coord = newCoord;
            offerIds.clear();
            offerIds.push_back(newOfferId);
        }
    }

};

void runIteration2(){
    std::vector<Offer> offers;
    int n, w1, w2, h1, h2;

    scanf("%d", &n);

    offers.reserve(n);

    for (int i = 0; i < n; ++i) {
        scanf("%d", &w1);
        scanf("%d", &w2);
        scanf("%d", &h1);
        scanf("%d", &h2);
        offers.push_back(Offer(w1, w2, h1, h2));
    }

}

void dumpVector(std::vector<int> &v) {
    fprintf(stderr, "[");
    for (std::vector<int>::iterator i = v.begin(); i != v.end(); i++) {
        fprintf(stderr, "%d, ", *i);
    }
    fprintf(stderr, "]\n");
}

void updateIntersectionSlow(std::vector<int> &intersection, std::vector<int> &a) {
    std::vector<int> result(intersection.size());
    std::vector<int>::iterator it;

    it = std::set_intersection(intersection.begin(), intersection.end(),
                               a.begin(), a.end(), result.begin());

    result.resize(it - result.begin());
    result.swap(intersection);
}

// for sorted unique vectors
void updateIntersection(std::vector<int> &intersection, std::vector<int> &a) {
    std::vector<int> result(intersection.size());
    std::vector<int>::iterator it;

    int resultCnt = 0, x, y;

    for (std::vector<int>::iterator it1 = intersection.begin(), it2 = a.begin(),
            itr = result.begin();
            it1 != intersection.end() && it2 != a.end(); ) {
        x = *it1;
        y = *it2;

        if (x == y) {
            *itr = x;
            itr++;
            it1++;
            it2++;
            resultCnt++;

        } else if (x < y) {
            it1++;
        } else /* if (x > y) */{
            it2++;
        }
    }
    result.resize(resultCnt);
    result.swap(intersection);
}

void runIteration(){
    int n, w1, w2, h1, h2;

    Coord cw1;
    Coord cw2;
    Coord ch1;
    Coord ch2;

    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        scanf("%d", &w1);
        scanf("%d", &w2);
        scanf("%d", &h1);
        scanf("%d", &h2);

        if (i == 0) {
            cw1.coord = w1;
            cw2.coord = w2;
            ch1.coord = h1;
            ch2.coord = h2;
        }

        cw1.updateInner(w1, i);
        cw2.updateOuter(w2, i);
        ch1.updateInner(h1, i);
        ch2.updateOuter(h2, i);

    }

    //dumpVector(cw1.offerIds);
    //dumpVector(cw2.offerIds);
    //dumpVector(ch1.offerIds);
    //dumpVector(ch2.offerIds);


    std::vector<int> intersection(cw1.offerIds.size());
    std::copy(cw1.offerIds.begin(), cw1.offerIds.end(), intersection.begin());

    updateIntersection(intersection, cw2.offerIds);
    updateIntersection(intersection, ch1.offerIds);
    updateIntersection(intersection, ch2.offerIds);

    //dumpVector(intersection);


    if (!intersection.empty()) {
        printf("TAK\n");
    } else {
        printf("NIE\n");
    }

}



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

    for (int i = 0; i < t; ++i) {
        runIteration();
    }
}