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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <bits/stdc++.h>

using namespace std;
typedef long long int64;
#define DEBUG(x) cerr << #x << " = " << x << endl;
#define DEBUGV(x) cerr << #x << " = ["; for (auto it : x) cerr << it << ", "; cerr << "]" << endl; 
#define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x)
#define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e)))
const int INF = 1000000001;
const double EPS = 10e-9;

struct Square {
    int id;
    int x, y;
    int length;

    friend ostream& operator<<(ostream& os, const Square& self);
};

ostream& operator<<(ostream& os, const Square& self) {
    os << "<Square(x=" << self.x << ", y=" << self.y << ", length=" << self.length << ")>";
    return os;
}

int distance(Square& a, Square& b) {
    return max(abs(a.x - b.x), abs(a.y - b.y));
}

struct Mosaic {

    vector<Square> squares;
    int width = -1, height = -1;
    int last_x = INF, last_y = INF;

    Mosaic(vector<Square> sqrs) : squares(sqrs) { }

    bool init() {
        sort(squares.begin(), squares.end(), [](const Square& a, const Square& b) -> bool { 
            return a.y == b.y ? a.x < b.x : a.y < b.y;
        });
        
        int minx = INF, miny = INF;
        for (auto& s : squares) {
            minx = min(s.x, minx);
            miny = min(s.y, miny);
        }

        int origin = -1;
        REP(x, squares.size()) {
            if (squares[x].x == minx && squares[x].y == miny) {
                origin = (int)x;
            }
        }
        if (origin == -1) {
            return false;
        }
        assert(origin == 0);

        REP(x, squares.size()) {
            squares[x].x -= minx;
            squares[x].y -= miny;
        }

        return true;
    }

    int last_with_y(int y) {
        int result = -1;
        REP(i, squares.size()) {
            if (squares[i].y == y) {
                result = (int)i;
            }
        }
        return result;
    }

    int last_with_x(int x) {
        int result = -1;
        REP(i, squares.size()) {
            if (squares[i].x == x) {
                result = (int)i;
            }
        }
        return result;
    }

    int closest_blocking(int s) {
        int result = -1;
        REP(i, squares.size()) {
            if ((int)i != s && squares[i].x >= squares[s].x && squares[i].y >= squares[s].y) {
                if (result == -1 || distance(squares[i], squares[s]) < distance(squares[result], squares[s])) {
                    result = (int)i;
                }
            }
        }
        return result;
    }

    void make_bottom_edge() {
        REP(i, squares.size()) {
            if (squares[i].y == 0) {
                int next = closest_blocking((int)i);
                if (next != -1) {
                    int max_length = last_y - squares[i].y;
                    REP(j, squares.size()) if (i != j && squares[j].x == 0 && squares[j].length != -1) {
                        if (squares[j].x + squares[j].length > squares[i].x) {
                            max_length = min(max_length, squares[j].y - squares[i].y);
                        }
                    }
                    squares[i].length = min(max_length, distance(squares[i], squares[next]));
                }
            }
        }
    }

    void make_left_edge() {
        REP(i, squares.size()) {
            if (squares[i].x == 0) {
                int next = closest_blocking((int)i);
                if (next != -1) {
                    int max_length = last_x - squares[i].x;
                    REP(j, squares.size()) if (i != j && squares[j].y == 0 && squares[j].length != -1) {
                        if (squares[j].y + squares[j].length > squares[i].y) {
                            max_length = min(max_length, squares[j].x - squares[i].x);
                        }
                    }
                    squares[i].length = min(max_length, distance(squares[i], squares[next]));
                }
            }
        }
    }

    bool make_edges() {
        int last_bottom = last_with_y(0);
        int last_left = last_with_x(0);
        int right_blocking = closest_blocking(last_bottom);
        int top_blocking = closest_blocking(last_left);
        if (right_blocking == -1 && top_blocking == -1) {
            last_x = squares[last_bottom].x;
            last_y = squares[last_left].y;
            make_bottom_edge();
            make_left_edge();
            squares[last_bottom].length = last_y;
            squares[last_left].length = squares[last_bottom].x + squares[last_bottom].length;
            width = squares[last_bottom].x + squares[last_bottom].length;
            height = squares[last_left].y + squares[last_left].length;
        } else if (right_blocking != -1) {
            make_bottom_edge();
            width = squares[last_bottom].x + squares[last_bottom].length;
            last_x = width;
            make_left_edge();
        } else if (top_blocking != -1) {
            make_left_edge();
            height = squares[last_left].y + squares[last_left].length;
            last_y = height;
            make_bottom_edge();
        }

        for (auto& s : squares) {
            if (s.x > last_x || s.y > last_y) {
                return false;
            }
        }
        return true;
    }

    int64 get_area() {
        int64 area = 0;
        for (auto& s : squares) {
            int64 l = s.length;
            area += l * l;
        }
        return area;
    }

    int64 get_expected_area() {
        return (int64)width * (int64)height;
    }

    bool fill_remaining() {
        REP(i, squares.size()) {
            if (squares[i].length == -1) {
                int dx = last_x - squares[i].x;
                int dy = last_y - squares[i].y;
                if (dx == 0 || dy == 0) {
                    return false;
                }
                int max_length = min(dx, dy);
                REP(j, squares.size()) if (i != j && squares[j].length != -1) {
                    if (squares[j].x > squares[i].x && squares[j].y < squares[i].y && squares[j].y + squares[j].length > squares[i].y) {
                        max_length = min(max_length, squares[j].x - squares[i].x);
                    }
                    if (squares[j].y > squares[i].y && squares[j].x < squares[i].x && squares[j].x + squares[j].length > squares[i].x) {
                        max_length = min(max_length, squares[j].y - squares[i].y);
                    }
                }
                squares[i].length = max_length;
            }
        }
        return true;
    }

    bool fill() {
        REP(i, squares.size()) {
            if (squares[i].length == -1) {
                int next = closest_blocking((int)i);
                if (next != -1) {
                    int dx = last_x - squares[i].x;
                    int dy = last_y - squares[i].y;
                    if (dx == 0 || dy == 0) {
                        return false;
                    }
                    int max_length = min(dx, dy);
                    REP(j, squares.size()) if (squares[j].length != -1) {
                        if (squares[j].x > squares[i].x && squares[j].y < squares[i].y && squares[j].y + squares[j].length > squares[i].y) {
                            max_length = min(max_length, squares[j].x - squares[i].x);
                        }
                        if (squares[j].y > squares[i].y && squares[j].x < squares[i].x && squares[j].x + squares[j].length > squares[i].x) {
                            max_length = min(max_length, squares[j].y - squares[i].y);
                        }
                    }
                    squares[i].length = min(max_length, distance(squares[i], squares[next]));
                }
            }
        }

        if (last_x == INF) {
            if (!fill_remaining()) {
                return false;
            }
            int max_x = -1;
            REP(x, squares.size()) {
                max_x = max(max_x, squares[x].x + squares[x].length);
            }
            last_x = max_x;
            width = last_x;
        } else if (last_y == INF) {
            if (!fill_remaining()) {
                return false;
            }
            int max_y = -1;
            REP(x, squares.size()) {
                max_y = max(max_y, squares[x].y + squares[x].length);
            }
            last_y = max_y;
            height = last_y;
        }

        int64 expected_area = get_expected_area();
        int64 area = get_area();
        return area == expected_area;
    }
};

#ifndef CATCH_TEST
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    REP(o, t) {
        int n;
        cin >> n;
        vector<Square> squares(n);
        REP(x, n) {
            cin >> squares[x].x >> squares[x].y;
            squares[x].id = x;
            squares[x].length = -1;
        }

        if (n == 1) {
            cout << "TAK 1\n";
            continue;
        }

        Mosaic mosaic(squares);
        if (!mosaic.init() || !mosaic.make_edges() || !mosaic.fill()) {
            cout << "NIE\n";
        } else {
            cout << "TAK ";
            sort(mosaic.squares.begin(), mosaic.squares.end(), [](const Square& a, const Square& b) -> bool { 
                return a.id < b.id;
            });
            for (auto& s : mosaic.squares) {
                cout << s.length << " ";
            }
            cout << endl;
        }
    }

    return 0;
}
#endif