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
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

void mini_self(int & a, int b) { a = min(a, b); }

struct P {
	int x, y, len;
	void read() { scanf("%d%d", &x, &y); len = 0; }
	bool operator <= (const P & he) const {
		return x <= he.x && y <= he.y;
	}
	int ukos() const { return x - y; }
};

void test_case() {
	debug() << "--";
	int n;
	scanf("%d", &n);
	vector<P> points(n);
	for(P & p : points)
		p.read();
	int x0 = points[0].x, y0 = points[0].y;
	int in_x_max = x0, in_y_max = y0;
	for(const P & p : points) {
		x0 = min(x0, p.x);
		y0 = min(y0, p.y);
		in_x_max = max(in_x_max, p.x);
		in_y_max = max(in_y_max, p.y);
	}
	if(n == 1) {
		puts("TAK 1");
		return;
	}
	vector<int> bad(n, 0);
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
			if(i != j && points[i] <= points[j]) {
				++bad[i];
			}
	
	map<int, vector<pair<int,int>>> ukosy_g;
	for(int i = 0; i < n; ++i) {
		const P & p = points[i];
		ukosy_g[p.ukos()].push_back({p.x, i});
	}
	for(auto it = ukosy_g.begin(); it != ukosy_g.end(); ++it) {
		vector<pair<int,int>> & w = it -> second;
		sort(w.begin(), w.end());
	}
	
	set<pair<int,int>> considered;
	for(int last = 0; last < n; ++last) if(bad[last] == 0) {
		for(int other = 0; other < n; ++other)
			if(other != last)
				if(bad[other] == 0 || (bad[other] == 1 && points[other] <= points[last])) {
					const int dy = points[other].y - points[last].y;
					const int dx = points[last].x - points[other].x;
					const int przekatna = abs(dx + dy);
					if(przekatna == 0) continue;
					int x1 = points[last].x + przekatna;
					int y1 = points[last].y + przekatna;
					if(x1 <= in_x_max) continue;
					if(y1 <= in_y_max) continue;
					debug() << imie(x1) imie(y1);
					if(considered.count({x1, y1})) continue;
					considered.insert({x1, y1});
					
					// TODO: move outside the loops
					auto ukosy = ukosy_g;
					
					vector<int> answer(n, -1);
					
					map<int, int> forX, forY;
					
					auto addForX = [&] (int x, int y) {
						if(!forX.count(x)) forX[x] = y;
						else mini_self(forX[x], y);
					};
					
					auto addForY = [&] (int x, int y) {
						if(!forY.count(y)) forY[y] = x;
						else mini_self(forY[y], x);
					};
					
					long long AREA = 0;
					
					bool maybe_ok = true;
					
					priority_queue<pair<long long,long long>> kolejka;
					auto addKolejka = [&] (long long x, long long y) {
						kolejka.push({x + y, y});
					};
					
					addKolejka(x1, y1);
					pair<long long,long long> prev_p = {LONG_LONG_MAX, LONG_LONG_MAX};
					while(!kolejka.empty()) {
						pair<int,int> tmp = kolejka.top();
						kolejka.pop();
						const long long y = tmp.second;
						const long long x = tmp.first - tmp.second;
						assert(x >= x0 && y >= y0);
						if(x == x0 || y == y0) continue;
						
						{
							auto it = forX.find(x);
							if(it != forX.end() && it -> second < y) {
								debug() << "continue x  " imie(x) imie(y) imie(it -> second);
								continue;
							}
						}
						{
							auto it = forY.find(y);
							if(it != forY.end() && it -> second < x) {
								debug() << "continue y  " imie(x) imie(y);
								continue;
							}
						}
						
						//~ for(int i = 0; i < n; ++i)
							//~ if(answer[i] != -1)
								//~ if(points[i].x < x && points[i].y < y && x <= points[i].x + answer[i] && y <= points[i].y + answer[i])// answer[i] == x &&      && points[i].x < x && points[i].y < y)
								//~ sense = 0;
						//~ if(sense == 0) continue;
						
						if(prev_p == make_pair(x, y)) continue;
						prev_p = make_pair(x, y);
						
						auto it = ukosy.find(P{x, y}.ukos());
						if(it == ukosy.end() || it -> second.empty()) {
							debug() << "bo nie ukosy" imie(x) imie(y);
							//~ continue;
							maybe_ok = false;
							break;
						}
						const int i = it -> second.back().second;
						it -> second.pop_back();
						
						const int side = x - points[i].x;
						if(side <= 0) {
							debug() << "bo size <= 0";
							maybe_ok = false;
							break;
						}
						
						assert(answer[i] == -1);
						answer[i] = side;
						debug() << imie(side) imie(i) imie(x) imie(y);
						AREA += (long long) side * side;
						
						//~ addForX(points[i].x, points[i].y + side);
						//~ addForY(points[i].x + side, points[i].y);
						addForX(points[i].x + side, points[i].y);
						addForY(points[i].x, points[i].y + side);
						
						addKolejka(points[i].x, points[i].y + side);
						addKolejka(points[i].x + side, points[i].y);
						
					}
					
					debug() << imie(maybe_ok) imie(answer);
					
					if(maybe_ok) for(int x : answer) if(x == -1) maybe_ok = false;
					
					if(AREA != (long long) (x1 - x0) * (y1 - y0)) maybe_ok = false;
					
					if(!maybe_ok) continue;
					
					for(int i = 0; i < n; ++i) if(maybe_ok)
						for(int j = 0; j < n; ++j)
							if(i != j) {
								if(points[i] <= points[j]) {
									if(answer[i] > max(points[j].x - points[i].x, points[j].y - points[i].y)) {
										maybe_ok = false;
										break;
									}
								}
							}
					
					for(int i = 0; i < n; ++i) if(maybe_ok)
						for(int j = 0; j < n; ++j)
							if(points[i].x < points[j].x) {
								if(i != j && !(points[i] <= points[j]) && !(points[j] <= points[i])) {
									if(points[i].x + answer[i] > points[j].x && points[j].y + answer[j] > points[i].y) {
										maybe_ok = false;
										break;
									}
								}
							}
					if(!maybe_ok) continue;
					
					printf("TAK");
					for(int i = 0; i < n; ++i) {
						printf(" %d", answer[i]);
					}
					puts("");
					return;
					
				}
	}
	puts("NIE");
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) test_case();
}