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
#include<cstdio>
#include<algorithm>

int t;

int n;
int * x;
int * y;

int index_of_lowest_x;
int index_of_highest_x;
int index_of_lowest_y;
int index_of_highest_y;

void input() {
  scanf("%d", &n);
  x = new int[n];
  y = new int[n];

  index_of_highest_y = 0;
  index_of_lowest_y = 0;
  index_of_highest_x = 0;
  index_of_lowest_x = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d %d", x + i, y + i);

    if (x[index_of_lowest_x] > x[i]) {
      index_of_lowest_x = i;
    }

    if (x[index_of_highest_x] < x[i]) {
      index_of_highest_x = i;
    }

    if (y[index_of_lowest_y] > y[i]) {
      index_of_lowest_y = i;
    }

    if (y[index_of_highest_y] < y[i]) {
      index_of_highest_y = i;
    }
  }
}

int max_possible_x;
int max_possible_y;

int * indexx;

int diameter;

void print_sorted() {
  printf("POINTS:\n");
  for (int i = 0; i < n; i++) {
    printf("(%2d, %2d)\n", x[indexx[i]], y[indexx[i]]);
  }
  printf("\n");
}

void assign_indexes() {
  indexx = new int[n];
  for (int i = 0; i < n; i++) {
    indexx[i] = i;
  }
}

bool compare_indexes(int i, int j) {
  if (y[i] < y[j]) {
    return false;
  } else if (y[i] > y[j]) {
    return true;
  }

  if (x[i] > x[j]) {
    return true;
  }
  return false;
}

void sort_indexes() {
  std::sort(indexx, indexx + n, compare_indexes);
}

int * bottom;

void print_bottom() {
  printf("BOTTOM:\n");
  for (int i = 0; i < diameter; i++) {
    printf("%3d", bottom[i]);
  }
  printf("\n");
}

void init_bottom(int top) {
  bottom = new int[diameter];
  for (int i = 0; i < diameter; i++) {
    bottom[i] = top;
  }
}

int get_bottom_x(int i) {
  return bottom[i - x[index_of_lowest_x]];
}

void set_bottom_x(int i, int new_value) {
  bottom[i - x[index_of_lowest_x]] = new_value;
}

bool can_be_placed(int i) {
  return y[indexx[i]] < get_bottom_x(x[indexx[i]]);
}

int compute_size(int i) {
  int x_start = x[indexx[i]];
  return get_bottom_x(x_start) - y[indexx[i]];
}

bool update_bottom(int i, int size) {
  int idx = indexx[i];
  int top = y[idx] + size;
  //printf("x=%d y=%d size=%d top=%d\n", x[idx], y[idx], size, top);
  for (int xi = 0; xi < size; xi++) {
    if (get_bottom_x(x[idx] + xi) != top) {
      return false;
    }
    set_bottom_x(x[idx] + xi, y[idx]);
  }
  //print_bottom();
  return true;
}

int * solution;

bool place(int i) {
  int size = compute_size(i);
  solution[indexx[i]] = size;
  return update_bottom(i, size);
}

void print_solution() {
  printf("TAK ");
  for (int i = 0; i < n; i++) {
    printf("%d ", solution[i]);
  }
  printf("\n");
}

bool is_solution_correct() {
  //print_bottom();
  bool is_not_zero_started = false;
  int top = 0;
  for (int i = 0; i < diameter; i++) {
    if (is_not_zero_started) {
      if (bottom[i] != top) {
        return false;
      }
    } else {
      if (bottom[i] != y[index_of_lowest_y]) {
        is_not_zero_started = true;
        top = bottom[i];
      }
    }
  }
  return true;
}

bool solve_for_top(int top) {
  //printf("top=%d\n", top);
  solution = new int[n];
  assign_indexes();
  sort_indexes();
  init_bottom(top);
  //print_sorted();
  //print_bottom();
  for (int i = 0; i < n; i++) {
    if (can_be_placed(i)) {
      if (!place(i)) {
      //  printf("inner\n");
        //print_bottom();
        return false;
      }
    } else {
      //printf("outer\n");
      //print_bottom();
      return false;
    }
  }
  if (is_solution_correct()) {
    print_solution();
    return true;
  }
  return false;
}

void solve() {
  int width = x[index_of_highest_x] - x[index_of_lowest_x];
  int height = y[index_of_highest_y] - y[index_of_lowest_y];
  diameter = (height > width ? height : width) * 2;

  max_possible_y = y[index_of_highest_y] + diameter;
  max_possible_x = x[index_of_highest_x] + diameter;

  for (int top = y[index_of_highest_y] + 1; top <= max_possible_y; top++) {
    if (solve_for_top(top)) {
      return;
    }
    //return;//TODO remove
  }
  printf("NIE\n");
}

int main(int argc, char ** argv) {
  scanf("%d", &t);
  while(t--) {
    input();
    solve();
  }
  return 0;
}