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
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

struct square {
    long long x;
    long long y;
    bool done;
    long long side;
    int ind;
};

struct originalOrder {
    bool operator()(const square& a, const square& b) const {
        return a.ind < b.ind;
    }
};

struct squareLexi {
    bool operator()(const square& a, const square& b) const {
        long long s11, s12, s21, s22;
        if (a.x <= a.y) {
            s11 = a.x;
            s12 = a.y;
        } else {
            s11 = a.y;
            s12 = a.x;
        }
        if (b.x <= b.y) {
            s21 = b.x;
            s22 = b.y;
        } else {
            s21 = b.y;
            s22 = b.x;
        }
        if (s11 < s21) {
            return true;
        }
        if (s11 == s21 && s12 < s22) {
            return true;
        }
        return false;
    }
};

struct sref {
    long long x;
    long long y;
    int pos;
};

struct refX {
    bool operator()(const sref& a, const sref& b) const {
        if (a.x == b.x)
            return a.y < b.y;
        return a.x <= b.x;
    }
};

struct refY {
    bool operator()(const sref& a, const sref& b) const {
        if (a.y == b.y)
            return a.x < b.x;
        return a.y <= b.y;
    }
};


void procesuj_zestaw() {
    int n;
    cin >> n;
//    cout << "Zestaw dlugosci: " << n << endl;

    vector<square> pts(n);
    set<sref, refX> refx;
    set<sref, refY> refy;

    for(int i = 0; i < n; i++) {
        long long x;
        long long y;
        cin >> x;
        cin >> y;
        square s;
        s.x = x;
        s.y = y;
        s.done = false;
        s.side = 0;
        s.ind = i;
        pts[i] = s;
    }
    
    sort(pts.begin(), pts.end(), squareLexi());
    
    for(int i = 0; i < n; i++) {
        sref s;
        s.x = pts[i].x;
        s.y = pts[i].y;
        s.pos = i;
        refx.insert(s);
        refy.insert(s);
    }
    
//    sort(refx.begin(), refx.end(), refX());
//    sort(refy.begin(), refy.end(), refY());
/*
    for(int i = 0; i < n; i++) {
        cout << "(" << pts[i].x << ", " << pts[i].y <<")" << endl;
    }
    
    cout << "---" << endl;
    
    for(set<sref, refX>::iterator it = refx.begin(); it != refx.end(); it++) {
        cout << "(" << it->x << ", " << it->y <<") -> " << it->pos << endl;
    }

    cout << "---" << endl;
    
    for(set<sref, refY>::iterator it = refy.begin(); it != refy.end(); it++) {
        cout << "(" << it->x << ", " << it->y <<") -> " << it->pos << endl;
    }
*/

    long long maxx = 0;
    long long maxy = 0;
    
    for(int i = 0; i < n; i++) {
        long long x = pts[i].x;
        long long y = pts[i].y;
        pts[i].done = true;
        
        sref refpx, refpy;
        refpx.x = x+1;
        refpx.y = y;
        refpy.x = x;
        refpy.y = y+1;
        
        // consider edge case first (so open from both or one of the sides) using maxx, maxy
        // but take care -> if these have not been set, they can be set to anything, so be careful
        
        set<sref, refX>::iterator xit = refx.lower_bound(refpx);
        set<sref, refY>::iterator yit = refy.lower_bound(refpy);
        
        // both open
        if (xit == refx.end() && yit == refy.end()) {
            if (maxx == 0 && maxy == 0) {
                pts[i].side = 1;
                maxy = y + pts[i].side;
                maxx = x + pts[i].side;
        //        cout << "boom " << i << endl;
                continue;
            } else if (maxx == 0) {
                pts[i].side = maxy - y;
                maxy = y + pts[i].side;
                maxx = x + pts[i].side;
                continue;
            }
            if (maxy == 0) {
                pts[i].side = maxx - x;
                maxx = x + pts[i].side;
                maxy = y + pts[i].side;
                continue;
            }
            
            // both non-zero, make it a square :)
        //    cout << "forcing " << i << " x " << x << " y " << y << " maxx " << maxx << " maxy " << maxy << endl;
            long long sx = maxx - x;
            long long sy = maxy - y;
            if (sx < 1 && sy < 1)
                pts[i].side = 1;
            else if (sx < 1)
                pts[i].side = sy;
            else if (sy < 1)
                pts[i].side = sx;
            else
                pts[i].side = min(sx, sy);
            maxx = max(maxx, x + pts[i].side);
            maxy = max(maxy, y + pts[i].side);
            continue;
        }
        
        // remember to update maxx and maxy after finish
        
        // x open
        if (xit == refx.end()) {
            // use y for side resolution
            // is taking x a correct approach???
            pts[i].side = yit -> y - y;
//            cout << "sidex allocated " << i << " " << pts[i].side << endl;
            maxx = max(maxx, x + pts[i].side);
            maxy = max(maxy, y + pts[i].side);
            continue;
        }
        
        // y open
        if (yit == refy.end()) {
            // use y for side resolution
            // is taking x a correct approach???
            pts[i].side = xit -> x - x;
    //        cout << "sidey allocated " << i << " " << pts[i].side << endl;
            maxx = max(maxx, x + pts[i].side);
            maxy = max(maxy, y + pts[i].side);
            continue;
        }    
        
        // inside
        
        long long sidex = xit -> x - x;
        long long sidey = yit -> y - y;
    //    cout << "side allocated " << i << " " << sidex << " " << sidey << endl;
        pts[i].side = min(sidex, sidey);
        maxx = max(maxx, x + pts[i].side);
        maxy = max(maxy, y + pts[i].side);
    }
    
    long long area = (maxx - pts[0].x) * (maxy - pts[0].y);
    long long sarea = 0;

    // process square sizes and match with maxx * maxy
    // if ok, print all, else print NIE
    
    for(int i = 0; i < n; i++) {
//        cout << "debug " << pts[i].x << " " << pts[i].y << " side: " << pts[i].side << " " << pts[i].side * pts[i].side << " sarea " << sarea << endl;
        sarea += (pts[i].side * pts[i].side);
    }
//    cout << " sarea " << sarea << " area " << area << endl;
    if (sarea != area) {
        cout << "NIE"  << endl;
        return;
    }
    
    sort(pts.begin(), pts.end(), originalOrder());

    for(int i = 0; i < n; i++) {
        if (i != 0)
            cout << " ";
        else
            cout << "TAK ";
        cout << pts[i].side;
    }
    cout << endl;
}

int main() {
    
    int t;
    cin >> t;
    
    for(int i = 0; i < t; i++) {
        procesuj_zestaw();
    }


    return 0;
}