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
187
188
189
190
191
192
193
194
195
196
#include <algorithm>
#include <stdint.h>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
using namespace std;

/****************************************************************************/

#define INF 2100000001

struct SeqTreeNode {
    uint32_t a, b;
    uint32_t value;
    SeqTreeNode *left, *right;

    void set_value(uint32_t A, uint32_t B, uint32_t V) {
        if (a>B || b<A) return;
        if (a>=A && b<=B) value = V;
        else if (!isLeaf()){
            left->set_value(A, B, V);
            right->set_value(A, B, V);
        }
    }

    bool isLeaf(){ return left == 0 && right == 0; }
    SeqTreeNode(int _a = 0, int _b = 0, SeqTreeNode *_left = 0, SeqTreeNode *_right = 0):a(_a),b(_b),left(_left),right(_right),value(INF){}
    ~SeqTreeNode(){
        if (!isLeaf()) {
            delete this->left;
            delete this->right;
        }
    }
};

struct SeqTree {
    SeqTreeNode *root;
    set<uint32_t> points;
    uint32_t max_real_point;
    uint32_t min_inf_point;

    void set_value(uint32_t A, uint32_t B, uint32_t V){
        if (B>max_real_point) B=min_inf_point;
        root->set_value(A, B, V);
    }

    uint32_t get_min(uint32_t A) {
        SeqTreeNode *node = root;
        uint32_t result = node->value;
        while (!node->isLeaf()) {
            node = node->right->a <= A ? node->right : node->left;
            result = min(result, node->value);
        }
        return result;
    }

    SeqTree(set<uint32_t> &P) {
        points = P;
        int new_length = 1;
        max_real_point = 0;
        while (new_length < points.size()) new_length *= 2;
        new_length = new_length - points.size() + 1;
        for (set<uint32_t>::iterator it=points.begin(); it!=points.end(); ++it) max_real_point = max(max_real_point, *it);
        for (int i=0; i<new_length; ++i){ min_inf_point = INF - i - 1; points.insert(min_inf_point); }

        vector<SeqTreeNode *> Q;
        for (set<uint32_t>::iterator it=points.begin(); it!=points.end(); ++it) {
            set<uint32_t>::iterator next = it; next++;
            if (next == points.end()) continue;
            Q.push_back(new SeqTreeNode(*it, *next));
        }
        while (Q.size() != 1) {
            vector<SeqTreeNode *> newQ;
            for(int i=0; i<Q.size(); i+=2) newQ.push_back(new SeqTreeNode(Q[i]->a, Q[i+1]->b, Q[i], Q[i+1]));
            Q = newQ;
        }
        root = Q[0];
    }
    ~SeqTree() {delete root; }
};

/****************************************************************************/

struct Point {
    uint32_t x, y;
    uint32_t max_size, size;
    uint32_t result_pos;
    Point():x(0),y(0){}
};

bool cmp_points_by_x_and_y(const Point &A, const Point &B) {
    if (A.x != B.x) return A.x > B.x;
    return A.y < B.y;
}

bool cmp_points_by_init_pos(const Point &A, const Point &B) {
    return A.result_pos < B.result_pos;
}

bool solve(vector<Point> &P, uint32_t min_x, uint32_t min_y, uint32_t max_x) {
    uint64_t big_sum = 0;
    uint32_t max_y = 0;
    set<uint32_t> S; for(int i=0; i<P.size(); ++i) S.insert(P[i].y);
    SeqTree T(S);
    T.set_value(0, INF, max_x);
    sort(P.begin(), P.end(), cmp_points_by_x_and_y);
    for(int i=0; i<P.size(); ++i) {
        uint32_t right_x = T.get_min(P[i].y);
        if (P[i].x == right_x) return false;
        P[i].size = right_x - P[i].x;
        if (P[i].size > P[i].max_size) return false;
        max_y = max(max_y, P[i].y+P[i].size);
        big_sum += (uint64_t)P[i].size * (uint64_t)P[i].size;
        T.set_value(P[i].y, P[i].y+P[i].size, P[i].x);
    }
    return big_sum == (uint64_t)(max_x - min_x) * (uint64_t)(max_y - min_y);
}

/****************************************************************************/

int z, n;
int main() {
    scanf("%d", &z);
    while (z--) {

        /** reading input **/
        vector<Point> P;
        scanf("%d", &n); P.resize(n);
        for(int i=0; i<n; ++i) scanf("%d %d", &P[i].x, &P[i].y);

        /** preprocessing **/
        uint32_t LB_id = 0, RB_id = 0, LT_id = 0;
        uint32_t min_x = INF, min_y = INF;
        for(int i=0; i<n; ++i) {
            P[i].result_pos = i;
            P[i].max_size = INF;
            for(int j=0; j<n; ++j) {
                if (i == j) continue;
                if (P[j].x<P[i].x || P[j].y<P[i].y) continue;
                P[i].max_size = min(P[i].max_size, max(P[j].x - P[i].x, P[j].y - P[i].y));
            }

            if ((P[LB_id].y>P[i].y) || (P[LB_id].y==P[i].y && P[LB_id].x>P[i].x)) LB_id = i;
            if ((P[RB_id].y>P[i].y) || (P[RB_id].y==P[i].y && P[RB_id].x<P[i].x)) RB_id = i;
            if ((P[LT_id].x>P[i].x) || (P[LT_id].x==P[i].x && P[LT_id].y<P[i].y)) LT_id = i;
            min_x = min(min_x, P[i].x);
            min_y = min(min_y, P[i].y);
        }
        if (n==1){ printf("TAK 1\n"); continue; }
        if (P[LB_id].x!=min_x || P[LB_id].y!=min_y) { printf("NIE\n"); continue; }
        if (LB_id == RB_id) {
            if (LB_id == LT_id){ printf("NIE\n"); continue; }
            for (int i=0; i<P.size(); ++i) swap(P[i].x, P[i].y);
            swap(min_x, min_y);
            swap(LT_id, RB_id);
        }

        /** main solution **/
        bool found = false;
        Point RB = P[RB_id];
        for (int i=0; i<n; ++i) {
            if (P[i].y == RB.y) continue;
            if (P[i].max_size != INF && P[i].x + P[i].max_size <= RB.x) continue;
            found = solve(P, min_x, min_y, RB.x + (P[i].y - RB.y));
            sort(P.begin(), P.end(), cmp_points_by_init_pos);
            if (found) break;
        }
        if (!found) {
            for (int i=0; i<n; ++i) {
                if (P[i].result_pos == RB.result_pos) {
                    swap(P[i], P.back());
                    P.pop_back();
                    break;
                }
            }
            found = solve(P, min_x, min_y, RB.x);
            if (found) {
                uint32_t max_y = 0;
                for (int i=0; i<P.size(); ++i) max_y = max(max_y, P[i].y + P[i].size);
                RB.size = max_y - min_y;
                P.push_back(RB);
            }
        }

        /** print result **/
        if (!found) printf("NIE\n");
        else {
            vector<uint32_t> result(P.size());
            for (int i=0; i<P.size(); ++i) result[P[i].result_pos] = P[i].size;
            printf("TAK ");
            for (int i=0; i<result.size(); ++i) printf("%d ", result[i]);
            printf("\n");
        }
    }
}