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
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <cstdio>
#include <cmath>
#include <iostream>

#define ll long long

using namespace std;

ll x[2005];
ll y[2005];
int n;
const ll INF = 8888888888888888888LL;
int sides[2005];
int nullSides[2005];
int c[100];
int gr[1000][1000];

ll naivePow(ll a,ll b) {
  ll c = 1;
  for(ll i=0;i<b;i++) {
    c *= a;
  }
  return c;
}

bool solve() {
  if(n == 1) {
    sides[0] = 1;
    return true;
  }
  ll miX = INF;
  ll miY = INF;
  ll maX = 0;
  ll maY = 0;
  for(int i=0;i<n;i++) {
    miX = min(miX,x[i]);
    miY = min(miY,y[i]);
    maX = max(maX,x[i]);
    maY = max(maY,y[i]);
  }
  for(int i=0;i<n;i++) {
    x[i] -= miX;
    y[i] -= miY;
  }
  ll maSide = max(maX - miX, maY - miY) * 2;
  for(int i=0;i<n;i++) {
    ll cx = x[i];
    ll cy = y[i];
    ll z = -1;
    for(int s=1;s<=maSide;s++) {
      for(int j=0;j<=s;j++) {
        for(int p=0;p<2;p++) {
          ll dx = cx + (p==0?j:s);
          ll dy = cy + (p==0?s:j);
          bool found = false;
          for(int k=0;k<n;k++) {
            if(x[k] == dx && y[k] == dy) {
              found = true;
              break;
            }
          }
          if(found && ((z == -1) || (z > s))) {
            z = s;
          }
        }
      }
    }
    if(z != -1) {
      sides[i] = z;
    }
  }

  int m2 = (int)((maSide/2)+3);
  for(int i=0;i<n;i++) {
    for(int j=0;j<=x[i];j++) {
      for(int k=0;k<=y[i];k++) {
        gr[k][j] = 1;
      }
    }
  }
  for(int i=0;i<n;i++) {
    if(gr[(int)y[i]+1][(int)x[i]+1] == 0) {
      sides[i] = 0;
    }
  }

  int ns = 0;
  for(int i=0;i<n;i++) {
    if(sides[i] == 0) {
      nullSides[ns] = i;
      ns++;
    }
  }

  int mm = (int)(maSide*2);
  ll p = naivePow(maSide, ns);
  for(ll ii=0;ii<p;ii++) {
    ll u = ii;
    for(int k=0;k<ns;k++) {
      c[k] = (int)(u%maSide);
      u /= maSide;
    }
    for(int i=0;i<ns;i++) {
      sides[nullSides[i]] = c[i]+1;
    }
    for(int i=0;i<mm;i++) for(int j=0;j<mm;j++) gr[i][j] = 0;
    bool ok = true;
    for(int i=0;ok && (i<n);i++) {
      int s = sides[i];
      int bx = (int)x[i];
      int by = (int)y[i];
      for(int j=0;ok && (j<s);j++) for(int k=0;ok && (k<s);k++) {
        int cx = bx+j;
        int cy = by+k;
        if(gr[cy][cx] == 0) {
          gr[cy][cx] = 1;
        } else {
          ok = false;
          break;
        }
      }
    }
    if(ok) {
      int zx = 0;
      for(int i=0;i<mm;i++) {
        if(gr[0][i] == 0) {
          zx = i;
          break;
        }
      }
      int zy = 0;
      for(int i=0;i<mm;i++) {
        if(gr[i][0] == 0) {
          zy = i;
          break;
        }
      }
      if(zx > 0 && zy > 0) {
        for(int i=0;ok && (i<mm);i++) {
          for(int j=0;ok && (j<mm);j++) {
            bool e = (i<zy) && (j<zx);
            if((gr[i][j]==1) != e) {
              ok = false;
              break;
            }
          }
        }
        if(ok) {
          return true;
        }
      }
    }
  }
  return false;
}

int main(int argc, char** argv) {
  int t;
  scanf("%d",&t);
  for(int i=0;i<t;i++) {
    scanf("%d",&n);
    for(int j=0;j<n;j++) {
      scanf("%lld %lld",&x[j],&y[j]);
    }

    bool res = solve();

    printf("%s", res?"TAK":"NIE");
    if(res) {
      for(int j=0;j<n;j++) {
        printf(" %d", sides[j]);
      }
    }
    printf("\n");
  }
  return 0;
}