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
#include <bits/stdc++.h>
using namespace std;
#define pr if(0)
vector<int> s,sb;
vector<pair<int, int>> t;
vector<int> iv;
map<int, int> m;
int lm,dm,ul,dr,uli,dri,um,rm;
int w,h;

void do_swap() {
	swap(lm, dm);
	swap(um, rm);
	swap(ul, dr);
	swap(uli, dri);
	swap(w, h);
	for (auto &p : t) 
		swap(p.first, p.second);

	for (int i = 0; i < s.size(); i++) {
		sb[i] = s[i];
	}
}

inline int wspolna(int a, int b, int c, int d) {
	return max(0, min(b, d) - max(a, c));
}
void ins_min(pair<int, int> p) {
	m[p.first] = p.second;
	map<int, int>::iterator ins = m.find(p.first), pit;
	while (ins != m.begin()) {
		pit = ins;
		--pit;
		if (pit->second != ins->second)
			break;
		swap(ins, pit);
		m.erase(pit);
	}
	while (ins != m.end()) {
		pit = ins;
		++pit;
		if (pit == m.end() || pit->second != ins->second) break;
		m.erase(pit);
	}
}
bool wyjscie() {
	int right = 0, top = 0;
	for (int i = 0; i < t.size(); i++) {
		right = max(right, t[i].first + s[i]);
		top = max(top, t[i].second + s[i]);
	}
	long long int pole = right-lm;
	pole *= (top - dm);
	for (auto &i : s)
		pole -= ((long long int)i)*i;
	if (pole) return false;
	printf("TAK");
	for (auto i : s) 
		printf(" %d", i);
	printf("\n");
}

int main() {
	int tt;
	scanf("%d", &tt);
	while (tt--) {
		pr printf("___________________\n");
		int n;
		s.clear();t.clear();m.clear();
		scanf("%d", &n);
		s.resize(n);
		sb.resize(n);
		iv.resize(n);

		//left-most, down-most
		w = h = lm = dm = INT_MAX; 
		um = rm = 0;
		for (int i = 0; i < n; i++) {
			int a,b;
			scanf("%d%d", &a, &b);
			t.emplace_back(a,b);
			iv[i] = i;
			sb[i] = INT_MAX;
			lm = min(lm, a);
			dm = min(dm, b);
			rm = max(rm, a);
			um = max(um, b);
		}
		// nie ma lewego dolnego rogu
		if (std::find(t.begin(), t.end(), make_pair(lm, dm)) == t.end()) { 
			printf("NIE\n");
			continue;
		}

		pr printf("lm: %d, dm: %d, rm: %d, um: %d\n", lm, dm, rm, um);

		/*	mamy obliczony rozmiar ramki w*h
			posortuje indeksy odwrotnie
			po kolei od gory do dolu bede sprawdzal 
			czy wyznaczone punkty to kwadrat 
			i czy dotyka wszystkiego z prawej strony
		*/
		int FIRST_IF = 0;
		for (;FIRST_IF < 2; FIRST_IF++) {
		bool stop = false;
		for (int swaps = 0; swaps < 4; swaps++) {
			std::sort(iv.begin(), iv.end(), [](const int &a, const int &b) {
				return t[a] > t[b];
			});
			int wmin = 0, wmax = 2*1e9+1;
			while (wmin <= wmax) {
				w = wmin/2 + wmax/2 + ((wmin&1) + (wmax&1)) / 2;
				pr printf("%d %d %d\n", wmin, w, wmax);
				m.clear();
				m[0] = lm + w;
				pr printf("def: %d\n", m[0]);
				char stop = 0;
				for (int i = 0; i < s.size(); i++) {
					s[i] = sb[i];
				}
				for (const auto &i : iv) {
					map<int, int>::iterator it = --m.upper_bound(t[i].second);
					int bok = it->second - t[i].first;
					if (bok < 1) {
						pr printf("bok < 1\n");
						stop = 1;
						break;
					}
					if (h != INT_MAX && bok > h - t[i].second + dm) {
						pr printf("bok za duzy, bok: %d, h: %d\n", bok, h);
						stop = 2;
						break;
					}
					map<int, int>::iterator pit = --m.lower_bound(t[i].second + bok);
					if (it != pit) {
						pr printf("pit != it, %d / %d\n", pit->second, it->second);
						if (FIRST_IF ? pit->second > it->second : *pit < *it) // 19
						// if (*pit < *it) // 18
							stop = 1;					
						else
							stop = 2;
						break;
					}
					s[i]=min(s[i], bok);
					++pit;
					int ph = it->second;
					if (pit == m.end() || pit->first > t[i].second+bok)
						m[t[i].second+bok] = ph;
						
					ins_min({t[i].second, t[i].first});
					pr printf("dodaje kwadrat [%d :%d] {%d | %d}, ph: %d\t\t->", bok, s[i], t[i].first, t[i].second, ph);
					pr for (auto &i : m) {
						printf("[%d]=%d; ", i.first, i.second);
					}
					pr printf("\n");

				}
				map<int, int>::iterator it = m.lower_bound(um+1);
				if (!stop) {
					if (it != m.end() && it->second != lm+w) {
						pr printf("elo2\n");
						stop = 2;
					}
				}
				if (!stop) {
					wmin = wmax = w;
					break;
				} 
				if (stop == 1) {
					// za mala wielkosc
					wmin = w+1;
				} else {
					wmax = w-1;
				}
			}
			if (wmin > wmax) {
				stop = true;
				break;
			}
			do_swap();
		}
		if (wyjscie())
			break;
		}
		if (FIRST_IF == 2)
			printf("NIE\n");
	}
}