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
// Krzysztof Małysa
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (auto i = (a), i ## __ = (n); i <= i ## __; ++i)
#define FORD(i,a,n) for (auto i = (a), i ## __ = (n); i >= i ## __; --i)
#define REP(i,n) FOR(i, 0, n - 1)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int(x.size()))
#define EB emplace_back
#define ST first
#define ND second
#define tpv typedef vector<

typedef long long LL;
typedef pair<int, int> PII;
tpv int> VI;
tpv VI> VVI;
tpv PII> VPII;
tpv LL> VLL;

constexpr char nl = '\n';
#define endl nl

#define ris return *this
#define tem template<class T

tem, class B> inline void mini(T&& a, B&& b) { if (b < a) a = b; }
tem, class B> inline void maxi(T&& a, B&& b) { if (b > a) a = b; }
int ceil2(int x) { return x < 2 ? 1 : 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); }

tem> struct Dump { T a, b; };
tem> auto dump(T&& x) -> Dump<decltype(x.begin())> { return {ALL(x)}; }
struct Debug {
	~Debug() { cerr << endl; }
	tem> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << boolalpha << x; return *this; }
	tem> auto operator<<(T x) -> decltype(x.begin(), *this) {
		auto a = x.begin(), b = x.end();
		*this << "{";
		for (; a != b;)
			*this << *a++, *this << (a == b ? "" : " ");
		return *this << "}";
	}
	tem, class B> Debug& operator<<(pair<T, B> p) { ris << "(" << p.ST << ", " << p.ND << ")"; }
	tem> Debug& operator<<(Dump<T> d) {
		*this << "{\n";
		for (T a = d.a, c = a; a != d.b; ++a)
			*this << "  " << distance(c, a) << ": " << *a << '\n';
		ris << "}";
	}
};
struct Foo {tem>Foo& operator<<(T) {ris;}};

#ifdef DEBUG
# define deb Debug()
#else
# define deb Foo()
#endif
#define imie(x...) #x ": " << (x) << " "
#define LOG(x...) deb << imie(x)
#define DOG(x...) deb << #x ": " << dump(x) << " "

struct Node {
	Node* l = nullptr;
	Node* r = nullptr;
	int to_set; // -1 means nothing (it cannot be -1 in a leaf)
	int min;
	int max;

	Node(int val) : to_set(val), min(val), max(val) {}
};

class SegTree {
	int size, default_val;
	Node* root;

	static void destruct(Node* x) {
		if (not x)
			return;

		destruct(x->l);
		destruct(x->r);
		delete x;
	}

	void propagate(Node* x) {
		if (not x->l)
			x->l = new Node(default_val);
		if (not x->r)
			x->r = new Node(default_val);

		if (x->to_set != -1) {
			x->l->to_set = x->l->min = x->l->max = x->to_set;
			x->r->to_set = x->r->min = x->r->max = x->to_set;
			x->to_set = -1;
		}
	}

	PII get_min_max(int a, int b, Node* x, int sz) {
		if (a == 0 and b + 1 == sz)
			return {x->min, x->max};

		sz >>= 1;
		propagate(x);

		if (b < sz)
			return get_min_max(a, b, x->l, sz);
		if (a >= sz)
			return get_min_max(a - sz, b - sz, x->r, sz);

		PII l = get_min_max(a, sz - 1, x->l, sz);
		PII r = get_min_max(0, b - sz, x->r, sz);
		return {min(l.ST, r.ST), max(l.ND, r.ND)};
	}

	void set_val(int a, int b, int val, Node* x, int sz) {
		if (a == 0 and b + 1 == sz) {
			x->to_set = x->min = x->max = val;
			return;
		}

		sz >>= 1;
		propagate(x);

		if (b < sz)
			set_val(a, b, val, x->l, sz);
		else if (a >= sz)
			set_val(a - sz, b - sz, val, x->r, sz);
		else {
			set_val(a, sz - 1, val, x->l, sz);
			set_val(0, b - sz, val, x->r, sz);
		}

		x->min = min(x->l->min, x->r->min);
		x->max = max(x->l->max, x->r->max);
	}

public:
	SegTree(int n, int def_val) : size(ceil2(n)), default_val(def_val), root(new Node(def_val)) {
	}

	PII get_min_max(int a, int b) {
		return get_min_max(a, b, root, size);
	}

	void set_val(int a, int b, int val) {
		return set_val(a, b, val, root, size);
	}

	~SegTree() {
		destruct(root);
	}
};

struct Point {
	int x, y, idx, sz = 0;

	// Point(int xx, int yy, int idxx) : x(xx), y(yy), idx(idxx) {}
};

ostream& operator <<(ostream& os, const Point& x) {
	return os << "(" << x.x << ", " << x.y << " i: " << x.idx << " sz: " << x.sz << ")";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t;
	cin >> t;
	REP (oooooooooooooooooo, t) {
		int n;
		cin >> n;

		int minx = INT_MAX, miny = INT_MAX;
		vector<Point> in(n);
		REP (i, n) {
			cin >> in[i].x >> in[i].y;
			mini(minx, in[i].x);
			mini(miny, in[i].y);
			in[i].idx = i;
		}

		for (Point& p : in) {
			p.x -= minx;
			p.y -= miny;
		}

		sort(ALL(in), [](Point const& a, Point const& b) { return a.x > b.x; });

		int num_of_iters = 3e4 / n / t;
		// int num_of_iters = 5;
		FOR (right_side, in.front().x + 1, in.front().x + 1 + num_of_iters) {
			deb << "\n=================================";
			LOG(right_side);
			SegTree tree(1 << 30, right_side); // It won't be enough on large tests but it does not matter then
			int maxy = 1;
			for (auto& p : in) {
				p.sz = tree.get_min_max(p.y, p.y).ST - p.x;
				maxi(maxy, p.y + p.sz);

				deb << "++++++++++++++++";
				LOG(p);

				if (p.sz <= 0)
					goto next_try;

				auto mm = tree.get_min_max(p.y, p.y + p.sz - 1);
				LOG(mm);
				if (mm.ST != mm.ND)
					goto next_try;

				tree.set_val(p.y, p.y + p.sz - 1, p.x);
			}

			deb << "Checking filling...";

			// Check if the filling is correct
			if (tree.get_min_max(0, maxy - 1).ND == 0) {
				deb << "  OK";
				goto yes;
			}

		next_try:;
		}

		cout << "NIE\n";
		continue;

	yes:
		cout << "TAK";
		VI ans(n);
		for (auto&& p : in)
			ans[p.idx] = p.sz;

		for (int x : ans)
			cout << ' ' << x;
		cout << nl;
	}

	return 0;
}