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
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

constexpr int INF = 2e9+5;

struct Point {
	int x, y, size;

	bool operator<(const Point& r) const {
		return y < r.y || (y == r.y && x < r.x);
	}

	bool operator==(const Point& r) const {
		return x == r.x && y == r.y;
	}
};

struct Border {
	mutable int beginX, y;

	bool operator<(const Border& r) const {
		return beginX < r.beginX;
	}
};

std::vector<Point> points;
std::vector<Point*> pointsSorted;
std::set<Border> borders;
Point bottomRight;

bool solve(int width, bool lastFree) {
	borders.clear();
	borders.insert({ 0, 0 });
	borders.insert({ width, INF });

	for (size_t i = 0; i < pointsSorted.size(); i++) {
		Point& cur = *pointsSorted[i];
		if (lastFree && cur == bottomRight) {
			continue;
		}
		if (cur.x >= width) {
			return false;
		}

		// Get limit by next point

		int xLimit = width;
		if (i+1 < pointsSorted.size() && pointsSorted[i+1]->y == cur.y) {
			xLimit = pointsSorted[i+1]->x;
		}

		// Find first block

		Border border{cur.x, 0};
		auto iter = --borders.upper_bound(border);

		if (iter->beginX < cur.x || iter->y != cur.y) {
			return false;
		}

		// Remove covered blocks

		iter++;
		while (iter->beginX < xLimit && iter->y <= cur.y) {
			if (iter->y < cur.y) {
				return false;
			}
			iter = borders.erase(--iter);
			iter++;
		}

		xLimit = std::min(xLimit, iter->beginX);
		int nextXLimit = iter->beginX;
		int nextY = (--iter)->y;

		// Split block

		cur.size = xLimit - cur.x;
		if (cur.size <= 0) {
			return false;
		}

		iter->beginX = cur.x;
		iter->y = cur.y + xLimit - cur.x;
		if (xLimit != nextXLimit) {
			borders.insert({ xLimit, nextY });
		}
	}

	int height = borders.begin()->y;
	if (height <= 0) {
		return false;
	}

	for (auto border : borders) {
		if (border.y != height && border.y < INF) {
			return false;
		}
	}

	std::cout << "TAK ";

	for (Point& p : points) {
		std::cout << (lastFree && p == bottomRight ? height : p.size) << " ";
	}
	std::cout << std::endl;
	return true;
}

void run() {
	int nPoints; std::cin >> nPoints;
	points.clear();
	pointsSorted.clear();
	points.resize(nPoints);

	Point shift{INF, INF, 0};
	bottomRight = {0, INF, 0};

	for (Point& p : points) {
		std::cin >> p.x >> p.y;
		shift.x = std::min(shift.x, p.x);
		shift.y = std::min(shift.y, p.y);
	}

	if (nPoints == 1) {
		std::cout << "TAK 1\n";
		return;
	}

	for (Point& p : points) {
		pointsSorted.push_back(&p);
		p.x -= shift.x;
		p.y -= shift.y;

		if (p.y < bottomRight.y || (p.y == bottomRight.y && p.x > bottomRight.x)) {
			bottomRight = p;
		}
	}

	sort(pointsSorted.begin(), pointsSorted.end(), [](Point* l, Point* r)->bool { return *l < *r; });

	// Check sizes

	std::set<int> sizes;
	for (Point& p : points) {
		if (p.y >= 0) {
			sizes.insert(bottomRight.x + p.y);
		}
	}

	for (int wid : sizes) {
		if (wid > 0 && solve(wid, false)) {
			return;
		}
	}

	if (!solve(bottomRight.x, true)) {
		std::cout << "NIE\n";
	}
}

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

	int total; std::cin >> total;
	while (total--) {
		run();
	}
	return 0;
}