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
// Piotr Szefler
/* I am terribly sorry if someone tries to read and understand this code...
   It just end up this way. */

#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#define INF   4000000006u
#define LIMIT 2000000000u
#define lld long long int

using namespace std;

struct point {
	int num;
	int num2;
	unsigned x;
	unsigned y;
	unsigned res;
	point(int num, unsigned x, unsigned y) {
		this->num = num;
		this->x = x;
		this->y = y;
		this->res = 0;
	}
	point() {}
};

bool cmpNum(const point &a, const point &b) {
	return a.num < b.num;
}

bool cmpXoverY(const point &a, const point &b) {
	if (a.x != b.x) return a.x < b.x;
	return a.y < b.y;
}

int t, n;
point p[2002];
vector <point> v; 


unsigned getOver(int i, unsigned trx, unsigned trY) {
	unsigned over = INF;
	if (trx) {
		over = min(over, trx - p[i].x);
	}
	if (trY) {
		over = min(over, trY - p[i].y);
	}

	for (int j = 0; j < i; j++) {
		if (p[j].res + p[j].x > p[i].x) {
			if (p[j].y > p[i].y) {
				over = min(over, p[j].y - p[i].y);
			}
		}
	}

	for (int j = i + 1; j < n; j++) {
		if (p[j].x >= p[i].x && p[j].y >= p[i].y) {
			unsigned dx = p[j].x - p[i].x;
			unsigned dy = p[j].y - p[i].y;
			if (dx <= dy) {
				over = min(over, dy);
			}
			if (dy <= dx) {
				over = min(over, dx);
			}
		}
		if (p[j].y < p[i].y && p[j].x > p[i].x && p[j].y + p[j].res > p[i].y) {
			over = min(over, p[j].x - p[i].x);
		}
	}
	return over;
}

int main() {
	scanf("%d", &t);
	for (int test = 0; test < t; test++) {
		scanf("%d", &n);

		v.clear();

		unsigned blx = INF, bly = INF;
		unsigned trx = 0,   trY = 0;
		unsigned maxx = 0, maxy = 0;
		lld sumArea = 0;

		for (int i = 0; i < n; i++) {
			unsigned x, y;
			scanf("%u%u", &x, &y);
			blx = min(blx, x);
			bly = min(bly, y);
			maxx = max(maxx, x);
			maxy = max(maxy, y);
			p[i] = point(i, x, y);
		}

		sort(p, p + n, cmpXoverY);

		for (int i = 0; i < n; i++) p[i].num2 = i;

		bool good = 1;

		if (p[0].x != blx || p[0].y != bly) good = 0; 

		stack<int> S;
		for (int i = 0; i < n; i++) {

			unsigned overy = INF, overx = INF, maxly = p[i].y, maxlx = p[i].x;
			for (int j = 0; j < i; j++) {
				if (p[j].res + p[j].x > p[i].x) {
					if (p[j].y > p[i].y) {
						overy = min(overy, p[j].y - p[i].y);
					}
				}
				if (p[j].res + p[j].x == p[i].x  && p[j].res != 0) {
					maxly = max(maxly, p[j].y + p[j].res);
				}
				if (p[j].res + p[j].y == p[i].y && p[j].res != 0) {
					maxlx = max(maxlx, p[j].x + p[j].res);
				}
			}
			for (int j = i + 1; j < n; j++) {
				if (p[j].x >= p[i].x && p[j].y >= p[i].y) {
					unsigned dx = p[j].x - p[i].x;
					unsigned dy = p[j].y - p[i].y;
					if (dx <= dy) {
						overy = min(overy, dy);
					}
					if (dy <= dx) {
						overx = min(overx, dx);
					}
				}
				
				if (p[j].y < p[i].y && p[j].x > p[i].x && p[j].y + p[j].res > p[i].y) {
					overx = min(overx, p[j].x - p[i].x);
				}
			}
			
			maxly = maxly - p[i].y;
			maxlx = maxlx - p[i].x;
			if (p[i].x == blx && overy != INF) maxly = LIMIT;
			if (p[i].y == bly && overx != INF) maxlx = LIMIT;

			if (max(maxly, maxlx) >= min(overy, overx)) {

				 p[i].res = min(overy, overx);
				sumArea += (lld) p[i].res * p[i].res;
				unsigned maxy = p[i].y + p[i].res;
				while (!S.empty() && p[S.top()].y < maxy) {
					int num = S.top();
					S.pop();
					unsigned over = getOver(num, 0, 0);
					sumArea += (lld) over * over;
					p[num].res = over;
					maxy = max(maxy, p[num].y + p[num].res);
				}
			} else {
				S.push(i);
			}
		}

		while (!S.empty()) {
			v.push_back(p[S.top()]);
			S.pop();
		}
		sort(v.begin(), v.end(), cmpXoverY);

		bool tlc = 0, brc = 0;
		for (auto &a : v) {
			if (a.x == blx) tlc = 1;
			if (a.y == bly) brc = 1;
		}

		if (!tlc) {
			for (int i = 0; i < n; i++) {
				trY = max(trY, p[i].y + p[i].res);
			}
		}

		if (!brc) {
			for (int i = 0; i < n; i++) {
				trx = max(trx, p[i].x + p[i].res);
			}
		}

		// go if top edge is set
		if (trY) {
			// printf("TRY %u %u:\n", trx, trY);
			unsigned ltrx = 0;
			for (auto &a : v) {
				unsigned over = getOver(a.num2, trx, trY);
				p[a.num2].res = over;
				sumArea += (lld) over * over;
				ltrx = max(ltrx, p[a.num2].res + p[a.num2].x);
			}
			if (!trx) trx = ltrx;
		} else 
		if (trx) {
			// printf("TRX %u %u:\n", trx, trY);
			unsigned ltry = 0;
			for (int i = v.size() - 1; i >= 0; i--) {
				auto a = v[i];
				unsigned over = getOver(v[i].num2, trx, trY);
				p[v[i].num2].res = over;
				sumArea += (lld) over * over;
				ltry = max(ltry, p[v[i].num2].res + p[v[i].num2].y);
			}
			if (!trY) trY = ltry;
		} else {
			char anyGood = 0;
			
			for (int i = 0; i < v.size(); i++) {
				unsigned ltry = 0, ltrx = 0;
				lld addArea = 0;
			 	v[i].res = LIMIT;
			 	stack<int> S;
			 	S.push(i);
				for (int j = i - 1; j >= 0; j--) {
					while (v[S.top()].y + v[S.top()].res <= v[j].y) {
						S.pop();
					}
					unsigned over = v[S.top()].x - v[j].x;
					S.push(j);

					v[j].res = over;
					addArea += (lld) over * over;
					ltry = max(ltry, v[j].res + v[j].y);
				}
				while (S.size() > 1) S.pop();
				for (int j = i + 1; j < v.size(); j++) {
					while (v[S.top()].x + v[S.top()].res <= v[j].x) {
						S.pop();
					}
					unsigned over = v[S.top()].y - v[j].y;
					S.push(j);

					v[j].res = over;
					addArea += (lld) over * over;
					ltrx = max(ltrx, v[j].res + v[j].x);
				}

				if (ltry > v[i].y) v[i].res = ltry - v[i].y;
				if (ltrx > v[i].x) v[i].res = ltrx - v[i].x;

				addArea += (lld) v[i].res * v[i].res;
				ltry = max(ltry, v[i].y + v[i].res);
				ltrx = max(ltrx, v[i].x + v[i].res);

				lld suppArea = (lld) (ltry - bly) * (lld) (ltrx - blx) - addArea - sumArea;

				char thisGood = (suppArea == 0ll);
				for (int j = 0; j < v.size(); j++) {
					if (v[j].res == 0 || v[j].res > LIMIT) {
						thisGood = 0;
					}
				}
				// if good
				if (thisGood) {
					anyGood = 1;
					for (int j = 0; j < v.size(); j++) {
						p[v[j].num2].res = v[j].res;
					}
					trx = ltrx;
					trY = ltry;
					sumArea += addArea;
					break;
				}
				for (int j = 0; j < v.size(); j++) {
					v[j].res = 0;
				}
				ltry = ltrx = 0;
			}
			if (!anyGood) {
				good = 0;
			}
		}

		lld suppArea = (lld) (trY - bly) * (lld) (trx - blx) - sumArea;
		if (suppArea != 0ll) good = 0;
		for (int i = 0; i < n; i++) {
			if (p[i].res == 0 || p[i].res > LIMIT) good = 0;
		}
		sort(p, p + n, cmpNum);
		
		if (!good) {
			printf("NIE\n");
		} else {
			printf("TAK ");
			for (int i = 0; i < n; i++) {
				printf("%u ", p[i].res);
			}
			printf("\n");
		}
	}
	return 0;
}