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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <map>
using namespace std;


struct Point{
  int x,y;
  int size;
  int type;
};


struct Wrap{
  Point* p;
  bool operator<(const Wrap& other) const {
    if(p->x == other.p->x){
      return p->y == other.p->y ? p->type > other.p->type : p->y > other.p->y;
    } else {
      return p->x > other.p->x;
    }
  }
};

Point* makePoint(int x, int y, int type){
  Point* ret = new Point();
  ret->x = x;
  ret->y = y;
  ret->type = type;
  return ret;
}

Point* makePoint(int x, int y, int type, int size){
  Point* ret = makePoint(x,y,type);
  ret->size = size;
  return ret;
}


Wrap makeWrap(Point* p){
  Wrap ret;
  ret.p = p;
  return ret;
}

Wrap makeWrap(int x, int y, int type){
  return makeWrap(makePoint(x,y,type));
}

bool validate(set<Wrap>& points){
  return true;
  set<Wrap>::reverse_iterator it = points.rbegin();
//   printf("size =%d", points.size() );
  while(it != points.rend()){
    Point* one = (*it).p;
    it++;
    if(it == points.rend()){
      return true;
    }
    Point* next = (*it).p;
//     printf("%d %d %d\n", one->x, one->y, one->size);
//     printf("%d %d %d\n", next->x, next->y, next->size);
    if(one->type == 2 && one->x == next->x && one->y + one->size != next->y){
      return false;
    }
  }
}

void insertPoints(int from, int d, int h, priority_queue<Wrap>& q, set<int>& positions){
  set<int>::iterator it = positions.upper_bound(from);
  while(it != positions.end()){
    int pos = *it;
    if(pos < from + d){
//       printf("inserting %d %d %d\n", pos, h, d);
      q.push(makeWrap(makePoint(pos,h,1,d)));
    }
    if(pos > from + d){
      break;
    }
    it++;
  }
//   printf("insert %d %d %d of type 2\n", from+d, h, d);
  q.push(makeWrap(makePoint(from+d,h,2,d)));
}

bool solve(int ymin, vector<Wrap>& points, set<int>& xSet, set<int>& possible){
  priority_queue<Wrap> q;
  for(int i=0;i<points.size();++i){
    q.push(points[i]);
  }
  set<Wrap> test;
  while(!q.empty()){
    Point* curr = q.top().p;
//     printf("curr is %d %d %d %d\n", curr->x, curr->y, curr->type, curr->size);
    q.pop();
//     test.insert(q.top());
    //corner
    if(curr->type == 0){
      //pop right corners
      while(!q.empty() && q.top().p->x == curr->x && q.top().p->type == 2){
        q.pop();
        test.insert(q.top());
      }
      //that was the last one
      if(q.empty()){
        break;
      } 
      //point in another column
      if(q.top().p->x != curr->x){
        continue;
      }
      //proper points
//       printf("%d %d %d\n", curr->x, curr->y, curr->size);
//       printf("doing proper point\n");
      int d = q.top().p->y - curr->y;
      curr->size = d;
      insertPoints(curr->x, d, curr->y, q, xSet);
    }
    if(curr->type == 1){
      test.insert(makeWrap(curr));
      if(!q.empty() && q.top().p->x == curr->x && q.top().p-> y != curr->y + curr->size){
//         printf("%d %d %d\n", curr->x, curr->y, curr->size);
//         printf("NIE bo type1 nie ma pokrycia z góry\n");
        return false;
      }
    }
    //something wrong or end
    //as type 2 is popped by type 0 or is on right edge at the bottom
    if(curr->type == 2){
      if(curr->y != ymin){
        return false;
      }
      while(!q.empty()){
        test.insert(makeWrap(curr));
        if(q.top().p-> y == curr->y + curr->size && q.top().p-> type == 2){
          curr = q.top().p;
          q.pop();
//           test.insert(q.top());
          possible.insert(curr->x);
        } else {
//           printf("%d %d %d\n", curr->x, curr->y, curr->size);
//           printf("NIE bo type2 nie robi krawedzi\n");
          q.pop();
//           test.insert(q.top());
          return false;
        }
      }
    }
  }
  return true;
  
}




bool fixup(int ymin, vector<Wrap>& points, set<int>& xSet, int limit){
  priority_queue<Wrap> q;
  for(int i=0;i<points.size();++i){
    q.push(points[i]);
  }
  set<Wrap> test;
  
  while(!q.empty()){
    Point* curr = q.top().p;
//     printf("curr is %d %d %d %d\n", curr->x, curr->y, curr->type, curr->size);
//     test.insert(q.top());
    q.pop();
    //corner
    if(curr->type == 0){
      //pop right corners
      int rCorners = 0;
      
      while(!q.empty() && q.top().p->x == curr->x && q.top().p->type == 2){
        test.insert(q.top());
        q.pop();
      }
      //that was the last one
      if(q.empty()){
        curr->size = limit - curr->y;
        insertPoints(curr->x, curr->size, curr->y, q, xSet);
        break;
      } 
      //point in another column
      if(q.top().p->x != curr->x){
        curr->size = limit - curr->y;
//         printf("set %d\n", curr->size);
        insertPoints(curr->x, curr->size, curr->y, q, xSet);
        continue;
      }
      //proper points
//       printf("%d %d %d\n", curr->x, curr->y, curr->size);
//       printf("doing proper point\n");
      int d = q.top().p->y - curr->y;

      curr->size = d;
      insertPoints(curr->x, d, curr->y, q, xSet);
    }
    if(curr->type == 1){
      test.insert(makeWrap(curr));
      if(!q.empty() && q.top().p->x == curr->x && q.top().p-> y != curr->y + curr->size){
//         printf("%d %d %d\n", curr->x, curr->y, curr->size);
//         printf("NIE bo type1 nie ma pokrycia z góry\n");
        return false;
      }
    }
    //something wrong or end
    //as type 2 is popped by type 0 or is on right edge at the bottom
    if(curr->type == 2){
      if(curr->y != ymin){
        return false;
      }
      while(!q.empty()){
        test.insert(makeWrap(curr));
        
        if(q.top().p-> y == curr->y + curr->size && q.top().p-> type == 2){
          curr = q.top().p;
          
          q.pop();
        } else {
//           printf("%d %d %d\n", curr->x, curr->y, curr->size);
//           printf("NIE bo type2 nie robi krawedzi\n");
//           test.insert(q.top());
          q.pop();
          return false;
        }
      }
    }
  }
  return validate(test);
  
}


void solve(){
  int n;
  scanf("%d\n", &n);
  if(n == 1){
    printf("TAK 1\n");
    return;
  }
  
  vector<Wrap> points(n);
  set<int> xSet;
  set<int> ySet;
  set<int> possible;
  for(int i=0;i<n;++i){
    int x,y;
    scanf("%d%d\n", &x, &y);
    points[i] = makeWrap(x,y,0);
    xSet.insert(x);
    ySet.insert(y);
    possible.insert(x);
  }
  bool corner = false;
  for(int i=0;i<n;++i){
    corner |= points[i].p->x == *xSet.begin() && points[i].p->y == *ySet.begin();
  }
  if(!corner){
    printf("NIE\n");
    return;
  }
  
  if(xSet.size() == 1){
    vector<Wrap> tmp(n);
    for(int i=0;i<n;++i){
      tmp[i] = points[i];
    }
    sort(tmp.rbegin(), tmp.rend());
    int delta = tmp[1].p->y - tmp[0].p->y;
    tmp[0].p->size = delta;
    for(int i=1;i<n;++i){
      if(tmp[i].p->y - tmp[i-1].p->y!= delta){
        printf("NIE\n");
        return;
      }
      tmp[i].p->size = delta;
    }
    printf("TAK ");
    for(int i=0;i<n;++i){
      printf("%d ", delta);
    }
    printf("\n");
    return;
  }
  
  if(ySet.size() == 1){
    vector<Wrap> tmp(n);
    for(int i=0;i<n;++i){
      tmp[i] = points[i];
    }
    sort(tmp.rbegin(), tmp.rend());
    int delta = tmp[1].p->x - tmp[0].p->x;
    tmp[0].p->size = delta;
    for(int i=1;i<n;++i){
      if(tmp[i].p->x - tmp[i-1].p->x!= delta){
        printf("NIE\n");
        return;
      }
      tmp[i].p->size = delta;
    }
    printf("TAK ");
    for(int i=0;i<n;++i){
      printf("%d ", delta);
    }
    printf("\n");
    return;
  }
  
  solve(*ySet.begin(), points, xSet, possible);
//   for(int i=0;i<points.size();++i){
//     printf("%d %d %d\n", points[i].p->x, points[i].p->y, points[i].p->size);
//   }
  
  int xMin = *(xSet.begin());
  int maxFirst = -1;
  vector<Wrap> toFix;
  
  for(int i=0;i<n;++i){
    if(points[i].p->x == xMin){
      maxFirst = max(maxFirst, points[i].p->y);
    }
    if(points[i].p->size == 0){
      toFix.push_back(points[i]);
    }
  }
  set<int>::iterator it = possible.begin();
  it++;
  while(it != possible.end()){
    int limit = maxFirst + (*it) - xMin;
//     printf("zgaduje %d ", limit);
    if(fixup(*ySet.begin(), points, xSet, limit)){
      printf("TAK ");
      for(int i=0;i<n;++i){
        printf("%d ", points[i].p->size);
      }
      printf("\n");
      return;
    } else {
      for(int i=0;i<toFix.size();++i){
        toFix[i].p->size = 0;
      }
    }
    it++;
  }
  printf("NIE\n");
  
}

int main(){
  int t;
  for(scanf("%d", &t);t;t--){
    solve();
  }
  return 0;
}