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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <iostream>
#include <climits>

struct Point {
    int x;
    int y;
    int size;
    int pos;

    Point() : x(-1), y(-1), size(0), pos(-1) {}
};

struct ReferenceToPoint {
    int key;
    int position;
    int x;
    int y;
 
    ReferenceToPoint(int inKey, int inPosition, int inX, int inY) : key(inKey), position(inPosition), x(inX), y(inY) { }
};

bool compareByX(ReferenceToPoint a, ReferenceToPoint b) {
    return (a.x < b.x); 
}

bool findBoundaryPoint(Point &p, std::vector<Point> &points) {
        bool found = false;
        int min = INT_MAX;
	for (int i = 0; i < points.size(); i++) {
		if (points[i].x >= p.x && points[i].y >= p.y) {
                	int xDiff = points[i].x - p.x;
                        int yDiff = points[i].y - p.y;
                        int diff = 0;
                        if (xDiff > 0 && yDiff > 0) {
                            diff = std::min(xDiff, yDiff);
                        } else if (xDiff > 0) {
                            diff = xDiff;
                        } else {
                            diff = yDiff;
                        }
                        min = std::min(min, diff);
                        found = true;
                }

	}
        if (found) {
		p.size = min;
        }
        return found;
}

void analyzeSinglePoint(std::map<int,std::vector<Point> >::iterator it, Point &p, std::map<int,std::vector<Point> > &pSumMap) {
	for (std::map<int,std::vector<Point> >::iterator it2 = it; it2 != pSumMap.end(); it2++) {
            if (it == it2) {
		continue;
            }
            if (findBoundaryPoint(p, it2->second)) {
                return;
            }
        }
}

void analyzePoints(std::map<int,std::vector<Point> >::iterator it, std::vector<Point> &points, std::map<int,std::vector<Point> > &pSumMap) {
	for (int i = 0; i < points.size(); i++) {
		analyzeSinglePoint(it, points[i], pSumMap);
	}
}

std::map<int,std::vector<Point> > rectangles(int setIdx)
{
  	int m; // liczba punktów
        int x,y;
       
        int sum[2000];
        std::map<int,std::vector<Point> > pSumMap;
        scanf("%d", &m);
        for (int j = 0; j < m; j++) {
           scanf("%d %d", &x, &y);
           Point p = Point();
           p.x = x;
           p.y = y;
           p.pos = j;
           pSumMap[x+y].push_back(p);
        }
        for (std::map<int,std::vector<Point> >::iterator j = pSumMap.begin(); j != pSumMap.end(); j++) {
            analyzePoints(j, j->second, pSumMap);
        }
        return pSumMap;
}

int main( ) {
    
    //printf("Hello\n");
    int n; // liczba setów
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
	std::map<int,std::vector<Point> > pSumMap = rectangles(i);
        if (pSumMap.begin()->second.size() > 1) {
            std::cout << "NIE" << "\n";
            continue;
        }
        std::vector<ReferenceToPoint> unSized;
        int minMaxX = 0;
        int minMaxY = 0;
        for (std::map<int,std::vector<Point> >::iterator j = pSumMap.begin(); j != pSumMap.end(); j++) {
            for (int k = 0; k < j->second.size(); k++) {
                Point p = j->second.at(k);
                minMaxX = std::max(p.x+1, minMaxX);
                minMaxY = std::max(p.y+1, minMaxY);
                int size = p.size;
                if (size == 0) {
                    unSized.push_back( ReferenceToPoint(j->first, k, p.x, p.y) );
                }
	    }
        }
        std::sort(unSized.begin(), unSized.end(), compareByX);
        bool allReachYOfFirst = true;
        for (int j = 0; j < unSized.size(); j++) {
            ReferenceToPoint pointRef = unSized[j];
            int size = 0;
            Point first = pSumMap[unSized[0].key][unSized[0].position];
            if (j+1 < unSized.size()) {
                size = unSized[j+1].x - unSized[j].x;
            } else {
                size = unSized[0].y + first.size - unSized[j].y;
                if (size == 0) {
                    size = std::max(minMaxX - unSized[j].x, minMaxY - unSized[j].y);
                }
            }
            pSumMap[pointRef.key][pointRef.position].size = size;
            if (j>0 && unSized[j].y + size != first.y + first.size) {
                allReachYOfFirst = false;
            }
            
        }

        if (!allReachYOfFirst) {
            std::cout << "NIE" << "\n";
            continue;
        }

        Point leftBottom = pSumMap.begin()->second[0];
        int minX = leftBottom.x;
        int minY = leftBottom.y;
        int maxX = 0;
        int maxY = 0;

        int sizeSum = 0;
        Point allPoints[2000];
        int m = 0;
        bool allPointsWithSize = true;
        for (std::map<int,std::vector<Point> >::iterator j = pSumMap.begin(); j != pSumMap.end(); j++) {
            for (int k = 0; k < j->second.size(); k++) {
                Point p = j->second.at(k);
                maxX = std::max(p.x+p.size, maxX);
                maxY = std::max(p.y+p.size, maxY);
                sizeSum += p.size*p.size;
                allPoints[p.pos] = p;
                if (p.size == 0) {
                    allPointsWithSize = false;
                }
                m++;
            }
        }
        long area = (maxX-minX) * (maxY-minY);
        if (area != sizeSum) {
            std::cout << "NIE"  << "\n";
            continue;
        }
        if (allPointsWithSize) {
            std::cout << "TAK";
            for (int j = 0; j < m; j++) {
                Point p = allPoints[j];
                std::cout << " " << p.size;
            }
        } else {
            std::cout << "NIE";
        }

        std::cout << "\n";
    }
    return 0;
}