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

using namespace std;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {} 
};

bool operator==(const Point &p1, const Point &p2) {
    return p1.x == p2.x && p1.y == p2.y;
}
bool operator!=(const Point &p1, const Point &p2) {
    return p1.x != p2.x || p1.y != p2.y;
}

bool operator<(const Point &p1, const Point &p2) {
    if (p1.y == p2.y) {
        return p1.x < p2.x;
    }
    return p1.y < p2.y;
};

int n;
int min_x = 1000001;
int min_y = 1000001;
int iter;
vector<Point> points;
vector<Point> sorted_points;
map<Point, int> result;
set<Point> chain;
set<int> ys;

map<Point, int> result_back;
set<Point> chain_back;
int iter_back;

void clearState() {
    iter = 0;
    min_x = 1000001;
    min_y = 1000001;
    points.clear();
    ys.clear();
    result.clear();
    chain.clear();
}

void saveState() {
    result_back = result;
    chain_back = chain;
    iter_back = iter;
}

void loadState() {
    result = result_back;
    chain = chain_back;
    iter = iter_back;
}

bool loadPoints() {
    cin >> n;
    points.resize(n);
    int x, y;
    for (int i = 0; i < n; i++) {
        cin >> points[i].x >> points[i].y;
        min_x = min(min_x, points[i].x);
        min_y = min(min_y, points[i].y);
        ys.insert(points[i].y);
    }
    if (n == 1) {
        cout << "TAK 1" << endl;
        return false;
    }
    sorted_points = points;
    sort(sorted_points.begin(), sorted_points.end());
    Point min_p = *sorted_points.begin();
    if (min_p.x != min_x || min_p.y != min_y) {
        cout << "NIE" << endl;
        return false;
    }
    chain.insert(min_p);
    return true; 
}

void checkPoint(const Point &p) {
    auto iter1 = chain.find(p);
    if (iter1 == chain.end()) {
        chain.insert(p);
    } else {
        chain.erase(iter1);
    }
}

void newSquare(Point &p, int size, bool isLastBottom = false) {
    result[p] = size;
    chain.erase(p);
    if (!isLastBottom) {
        checkPoint(Point{p.x + size, p.y});
    }
    checkPoint(Point{p.x, p.y + size});
    checkPoint(Point{p.x + size, p.y + size});
}

Point bottomLine() {
    ys.erase(ys.begin());
    for (iter = 0; iter < n-1; iter++) {
        Point p = sorted_points[iter];
        Point next = sorted_points[iter+1];
        if (next.y == min_y) {
            newSquare(p, next.x - p.x);
        } else {
            saveState();
            return p;
        }
    }
    saveState();
    return sorted_points[iter+1];
}

bool checkRectangle() {
    return chain.size() == 2 && (*chain.begin()).y == (*++chain.begin()).y;
}

bool tryIt() {
    while (++iter < n) {
        Point p = sorted_points[iter]; 
        if (p != *chain.begin()) {
            return false;
        }
        int size = 1000001;
        if (iter + 1 < n) {
            Point next = sorted_points[iter+1];
            if (next.y == p.y) {
                size = next.x - p.x;
            }
        }
        Point nextInChain = *++chain.begin();
        assert(nextInChain.y == p.y);
        size = min(size, nextInChain.x - p.x);
        newSquare(p, size);
    }
    return checkRectangle();
}

void putResult() {
    cout << "TAK";
    for (Point p1 : points) {
        cout << " " << result[p1];
    }
    cout << endl;
}

void solveTest() {
    clearState();
    if (loadPoints()) {
        Point p = bottomLine();
        for (int y : ys) {
            newSquare(p, y - p.y, true);
            if (tryIt()) {
                putResult();
                return;
            }
            loadState();
        }
        if (chain.size() > 1) {
            chain.erase(p);
            if (tryIt()) {
                result[p] = sorted_points[n-1].y + result[sorted_points[n-1]] - p.y;
                putResult();
                return;
            } 
        }
        cout << "NIE" << endl;
   }
}

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

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solveTest();
    }
}