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
295
#include <bits/stdc++.h>
#define int long long
#define X first
#define Y second

#define NIE do { \
        cout << "NIE\n"; \
        goto kuniec; \
    } while (0);

#define TAK do { \
        print_sol(swapped); \
        goto kuniec; \
    } while (0);
using namespace std;

typedef pair<int, int> PII;
typedef unsigned long long ULL;

const int INF = 2e9;
const ULL TRYLION = 1000000ULL * 1000000ULL * 1000000ULL;

struct Point {
    int x, y, s;

    Point(int x = 0, int y = 0, int s = -1): x(x), y(y), s(s) {}
};

bool cmp(const Point& a, const Point& b) {
    if (a.y != b.y) return a.y < b.y;
    return a.x < b.x;
}
struct setcmp {
    bool operator()(PII a, PII b) {
        if (a.second != b.second) return a.second < b.second;
        return a.first < b.first;
    }
};

int t, n;
vector<Point> points;
vector<int> leftovers;

map<pair<int,int>, int> id;


void print_sol(bool swapped) {
    vector<int> res(n);

    for (int i = 0; i < n; i++) {
        pair<int, int> p;
        p = swapped ? make_pair(points[i].y, points[i].x) : make_pair(points[i].x, points[i].y);
        res[id[p]] = points[i].s;
    }

    cout << "TAK ";
    for (auto x: res)
        cout << x << " ";
    cout << "\n";
}

bool check() {

    ULL upper = 0;
    ULL lower = TRYLION;
    ULL left = TRYLION;
    ULL right = 0;

    for (auto p: points) {
        if (p.s <= 0 || p.s > INF) return false;
        upper = max(upper, (ULL)p.y + p.s);
        lower = min(lower, (ULL)p.y);
        left = min(left, (ULL)p.x);
        right = max(right, (ULL)p.x + p.s);
    }

    ULL area = (upper - lower) * (right - left);

    ULL sum = 0;

    for (auto p: points) {
        sum += (ULL)p.s * p.s;
        if (sum > area) {
            return false;
        }
    }

    if (sum != area) return false;

    /*
    for (int i = 0; i < n; i++) {

        auto p = points[i];
        for (int j = i + 1; j < n; j++) {
            auto q = points[j];

            bool ok = false;
            ok |= p.x >= q.x + q.s;
            ok |= q.x >= p.x + p.s;
            ok |= p.y >= q.y + q.s;
            ok |= q.y >= p.y + p.s;

            if (!ok) {
                return false;
            }
        }
    }*/
    set<PII, setcmp> S;

    set<pair<int, PII>> evs;


    //for (auto p

    return true;
}


bool ins(set<PII, setcmp> &S, PII d) {
    auto it = S.insert(d).first;
    
    if (it != S.begin()) {
        auto pre = prev(it);
        if (pre->second > it->first) return false;
    }

    if (next(it) != S.end()) {
        auto ne = next(it);
        if ((*it).second > (*ne).first) return false;
    }

    return true;
}

int findlim(set<PII, setcmp> &S, int x) {
    auto lim = make_pair(INF + INF, x);

    auto it = S.lower_bound(lim);

    if (it == S.end()) return -1;

    return it->first;
}

int32_t main() {

    ios_base::sync_with_stdio(0);

    cin >> t;

    while (t--) {
        cin >> n;

        bool swapped = false;

        points.clear();
        points.resize(n);

        leftovers.clear();
        id.clear();



        for (int i = 0; i < n; i++) {
            cin >> points[i].x >> points[i].y;
            id[make_pair(points[i].x, points[i].y)] = i;
        }

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

        for (int z = 0; z < 2; z++) {
            sort(begin(points), end(points), cmp);

            int min_x = points[0].x;
            int min_y = points[0].y;
            int ln;

            int last_low = -1;

            for (auto p: points) {
                if (p.x < min_x) {
                    NIE;
                }
            }

            for (int i = 0; i < n; i++) {
                if (points[i].y == min_y) {
                    last_low = i;
                }
            }


            for (int i = last_low + 1; i < n; i++) {
                //cerr << "i = " << i << "\n";
                for (int j = 0; j < n; j++) points[j].s = -1;

                points[last_low].s = points[i].y - points[last_low].y;
                int right_border = points[last_low].s + points[last_low].x;



                bool ok = true;

                set<PII, setcmp> S;

                set<pair<int, PII>> evs;

                {
                    auto p = points[last_low];
                    auto inter = make_pair(p.x, p.x + p.s);
                    auto lol = make_pair(p.y + p.s, inter);
                    S.insert(inter);
                    evs.insert(lol);
                }

                for (int j = 0; j < n; j++) {
                    if (j == last_low) continue;

                    auto p = points[j];


                    //cerr << p.x << " "<< p.y << "\n";
                    while (!evs.empty() && (*evs.begin()).first <= p.y) {
                        S.erase((*evs.begin()).second);
                        evs.erase(evs.begin());
                        //cerr << "erase \n";
                    }


                    int s = right_border - p.x;
                    //cerr << "s1 " << s << "\n";

                    if (j + 1 < n && points[j+1].y == points[j].y) {
                        s = min(s, points[j+1].x - points[j].x);
                    }

                    //cerr << "s2 " << s << "\n";
                    auto lim = findlim(S, p.x);

                    //cerr << "lim " << lim << "\n";

                    if (lim != -1) 
                        s = min(s, lim - p.x);
                    /*for (int j = 0; j < n; j++) {
                        auto q = points[j];
                        if (q.s != -1 && q.x + q.s > p.x && q.y + q.s > p.y) {
                            s = min(s, q.x - p.x);
                        }
                    }*/

                    //cerr << "s = " << s << "\n";
                    if (s <= 0) {
                        ok = false;
                        break;
                    }
                    points[j].s = s;

                    if (!ins(S, make_pair(p.x, p.x+s))) {
                        ok = false;
                        break;
                        //cerr << "EEE " << S.size() << "\n";
                    }
                    auto lol = make_pair(p.y + s, make_pair(p.x, p.x + s));
                    evs.insert(lol);

                    //cerr << "insert " << p.y+s << " " << p.x << " " << p.x + s << "\n";
                }

                if (ok && check()) {
                    TAK;
                }

            }

            if (z == 0) {
                swapped = true;
                for (auto &p: points) {
                    int t = p.x;
                    p.x = p.y;
                    p.y = t;
                }

            }
        }
        NIE;

kuniec:
        1;
    }

    return 0;
}