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
#include <algorithm>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;

namespace {

struct Impossible {};

#define impossible() throw Impossible{};

constexpr int inf = 2000000000;

struct Square {
  int x;
  int y;
  int min_size = 1;
  int max_size = inf;

  Square(int x_, int y_): x{x_}, y{y_}
  {
  }

  void check() const
  {
    if (min_size > max_size) impossible();
  }

  void set_min_size(int s)
  {
    if (min_size < s) {
      min_size = s;
      check();
    }
  }

  void set_max_size(int s)
  {
    if (max_size > s) {
      max_size = s;
      check();
    }
  }

  void set_size(int s)
  {
    if (s < min_size || s > max_size) impossible();
    min_size = max_size = s;
  }

  friend ostream& operator<<(ostream& os, Square const& s)
  {
    os << s.x << ", " << s.y << ": " << s.min_size;
    if (s.min_size != s.max_size) os << "…" << s.max_size;
    return os;
  }

  friend bool intersect(Square const& a, Square const& b)
  {
    if (max(a.x, b.x) >= min(a.x + a.min_size, b.x + b.min_size)) return false;
    if (max(a.y, b.y) >= min(a.y + a.min_size, b.y + b.min_size)) return false;
    return true;
  }

  int long long min_area() const
  {
    return static_cast<int long long>(min_size) * min_size;
  }

  bool size_known() const
  {
    return min_size == max_size;
  }
};

void try_right(vector<Square>& squares, int right)
{
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> to_delete;
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> bto_delete;
  multiset<int> walls;
  multiset<int> bwalls;
  auto rb = squares.begin();
  while (rb != squares.end()) {
    auto re = rb;
    while (re != squares.end() && re->y == rb->y) ++re;
    auto y = rb->y;
    while (!to_delete.empty() && to_delete.top().first <= y) {
      auto x = to_delete.top().second;
      to_delete.pop();
      auto it = walls.find(x);
      walls.erase(it);
    }
    while (!bto_delete.empty() && bto_delete.top().first <= y) {
      auto x = bto_delete.top().second;
      bto_delete.pop();
      auto it = bwalls.find(x);
      bwalls.erase(it);
    }
    // wiersz to [rb, re)
    auto it = rb;
    while (it != re) {
      auto& s = *it;
      if (s.x >= right) impossible();
      auto q = right;
      auto it2 = next(it);
      if (it2 != re) q = min(q, it2->x);
      auto wit = walls.upper_bound(s.x);
      while (wit != walls.end()) {
        auto q = bwalls.find(*wit);
        if (q == bwalls.end()) break;
        wit = walls.upper_bound(*q);
      }
      if (wit != walls.end()) q = min(q, *wit);
      auto w = q - s.x;
      s.set_size(w);
      bwalls.insert(s.x);
      walls.insert(s.x + w);
      to_delete.emplace(s.y + w, s.x + w);
      bto_delete.emplace(s.y + w, s.x);
      ++it;
    }
    rb = re;
  }
  int right2 = 0;
  int top = 0;
  long long area = 0;
  for (auto const& s: squares) {
    area += s.min_area();
    right2 = max(right2, s.x + s.min_size);
    top = max(top, s.y + s.min_size);
  }
  if (area != static_cast<long long>(right2) * top) impossible();
}

void try_all(vector<Square>& squares)
{
  vector<int> ys; ys.reserve(squares.size());
  for (auto& s: squares) {
    ys.emplace_back(s.y);
  }
  sort(ys.begin(), ys.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  vector<Square*> by_yx(squares.size());
  iota(by_yx.begin(), by_yx.end(), squares.data());
  sort(by_yx.begin(), by_yx.end(), [&](Square const* a, Square const* b) {
    return make_pair(a->y, a->x) < make_pair(b->y, b->x);
  });

  if (by_yx.front()->x != 0 || by_yx.front()->y != 0) impossible();

  auto last0 = by_yx.begin();
  while (last0 != by_yx.end() && (*last0)->y == 0) ++last0;
  --last0;

  vector<Square> new_squares; new_squares.reserve(squares.size());
  for (auto s: by_yx) {
    new_squares.emplace_back(*s);
  }

  try {
    auto ss = new_squares;
    ss.erase(ss.begin() + (last0 - by_yx.begin()));
    try_right(ss, (*last0)->x);
    auto top = 0;
    for (auto& s: ss) {
      top = max(top, s.y + s.min_size);
    }
    auto s = **last0;
    s.set_size(top);
    ss.insert(ss.begin() + (last0 - by_yx.begin()), s);
    for (int i = 0; i < int(by_yx.size()); ++i) {
      by_yx[i]->set_size(ss[i].min_size);
    }
    return;
  } catch (Impossible) {
  }
  for (auto const& y: ys) {
    try {
      auto ss = new_squares;
      try_right(ss, (*last0)->x + y);
      for (int i = 0; i < int(by_yx.size()); ++i) {
        by_yx[i]->set_size(ss[i].min_size);
      }
      return;
    } catch (Impossible) {
    }
  }
  impossible();
}

void solve(vector<Square>& squares)
{
  if (squares.size() == 1) {
    squares.front().set_size(1);
    return;
  }

  int mx = inf;
  int my = inf;
  for (auto const& s: squares) {
    mx = min(mx, s.x);
    my = min(my, s.y);
  }

  for (auto& s: squares) {
    s.x -= mx;
    s.y -= my;
    s.set_max_size(inf - s.x);
    s.set_max_size(inf - s.y);
  }

  for (auto it = squares.begin(); it != squares.end(); ++it) {
    auto& s = *it;
    for (auto it2 = squares.begin(); it2 != squares.end(); ++it2) {
      if (it == it2) continue;
      auto& t = *it2;
      if (t.x < s.x || t.y < s.y) continue;
      s.set_max_size(max(t.x - s.x, t.y - s.y));
    }
  }

  try_all(squares);

  // for (auto it = squares.begin(); it != squares.end(); ++it) {
  //   auto& s = *it;
  //   for (auto it2 = next(it); it2 != squares.end(); ++it2) {
  //     auto& t = *it2;
  //     if (intersect(s, t)) {
  //       impossible();
  //     }
  //   }
  // }
}

}

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

  int t;
  cin >> t;
  while (t > 0) {
    --t;
    int n;
    cin >> n;

    vector<Square> squares; squares.reserve(n);
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      squares.emplace_back(x, y);
    }

    try {
      solve(squares);
    } catch (Impossible) {
      cout << "NIE" << endl;
      continue;
    }
    cout << "TAK";
    for (auto const& s: squares) {
      cout << ' ' << s.min_size;
    }
    cout << endl;
  }

  return 0;
}