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
#include <utility>
#include <set>
#include <map>
#include <iostream>
#include <vector>
using namespace std;

using int64 = long long;

void solve(int N)
{
    std::map<int64, std::set<std::pair<int64,int>>> horizontal;
    std::map<int64, std::set<std::pair<int64,int>>> vertical;


    for (int n=0;n<N;n++){
        int64 xi,yi;

        scanf("%lld", &xi);
        scanf("%lld", &yi);

        if (horizontal.count(xi) == 0){
            horizontal.insert({xi, {}});
        }
        horizontal.at(xi).insert({yi,n});

        if (vertical.count(yi) == 0){
            vertical.insert({yi, {}});
        }
        vertical.at(yi).insert({xi,n});
    }

    if (N == 1) {
        cout << "TAK 1"<<endl;
        return;
    }

    int numberOfDone = 0;

    std::vector<std::pair<std::pair<int64, int64>, int>> outPoints;
    std::map<int, int64> goodPoints;
    int64 maxHeight = 0;
    int64 maxWidth = 0;
    int64 minX = horizontal.begin()->first;
    int64 minY = vertical.begin()->first;
    while(numberOfDone < N){
        int64 y = horizontal.begin()->second.begin()->first;
        int64 x = vertical.at(y).begin()->first;
        int64 id = vertical.at(y).begin()->second;

        horizontal.begin()->second.erase(horizontal.begin()->second.begin());
        vertical.at(y).erase(vertical.at(y).begin());
        if (horizontal.begin()->second.empty()){
            horizontal.erase(horizontal.begin());
        }
        if (vertical.at(y).empty()){
            vertical.erase(y);
        }


        int64 dx=-1, dy=-1;


        if (vertical.find(y) != vertical.end())
        {
            dx = vertical.at(y).begin()->first - x;
        }

        if (horizontal.find(x) != horizontal.end())
        {
            dy = horizontal.at(x).begin()->first - y;
        }

        if (dy == -1 && dx == -1){
            outPoints.push_back({{x,y}, id});
        }
        else{
            if (dy == dx) {
                goodPoints.insert({id, dy});
                maxWidth = std::max(maxWidth, x+dx);
                maxHeight = std::max(maxHeight, y+dy);
            }
            else if (dx == -1 && dy>0){
                goodPoints.insert({id, dy});
                maxWidth = std::max(maxWidth, x+dy);
                maxHeight = std::max(maxHeight, y+dy);
            }
            else if (dy == -1 && dx>0) {
                goodPoints.insert({id, dx});
                maxWidth = std::max(maxWidth, x+dx);
                maxHeight = std::max(maxHeight, y+dx);
            }
            else
            {
                int64 lower = std::min(dx,dy);
                maxWidth = std::max(maxWidth, x+lower);
                maxHeight = std::max(maxHeight, y+lower);
                goodPoints.insert({id, lower});
            }
        }

        numberOfDone++;
    }

    if (outPoints.size() == 2){
        if (N == 2){
            cout << "NIE" << endl;
            return;
        }
        std::pair<std::pair<int64, int64>, int> top = outPoints[0];
        std::pair<std::pair<int64, int64>, int> bottom = outPoints[1];

        if (top.first.second < bottom.first.second){
            std::swap(top, bottom);
        }

        int64 distanceBetween = std::abs(top.first.first - bottom.first.first) + std::abs(top.first.second - bottom.first.second);

        if (top.first.first > minX){
            int64 forcedBottom = std::abs(top.first.second - bottom.first.second);
            if (top.first.second >= maxHeight){
                cout << "NIE" << endl;
                return;
            }

            distanceBetween = maxHeight - top.first.second;
            forcedBottom = distanceBetween + forcedBottom;

            if (bottom.first.second > minY){
                if (bottom.first.first >= maxWidth || bottom.first.first+forcedBottom != maxWidth)
                cout << "NIE" << endl;
                return;
            }

            goodPoints.insert({top.second, distanceBetween});
            goodPoints.insert({bottom.second, forcedBottom});
        }

        else if (bottom.first.second > minY){
            int64 forcedTop = std::abs(top.first.first - bottom.first.first);
            if (bottom.first.first >= maxWidth){
                cout << "NIE" << endl;
                return;
            }

            distanceBetween = maxWidth - bottom.first.first;
            forcedTop = distanceBetween + forcedTop;

            if (top.first.first > minX){
                if (top.first.second >= maxHeight || top.first.second+forcedTop != maxHeight)
                cout << "NIE" << endl;
                return;
            }
            goodPoints.insert({bottom.second, distanceBetween});
            goodPoints.insert({top.second, forcedTop});
        }

        else {
            goodPoints.insert({top.second, distanceBetween});
            goodPoints.insert({bottom.second, std::abs(top.first.second - bottom.first.second)});
        }


    }
    else if (outPoints.size() == 1){
        if (outPoints[0].first.first == minX){
            goodPoints.insert({outPoints[0].second, maxWidth - minX});
        }
        else if (outPoints[0].first.second == minY){
            goodPoints.insert({outPoints[0].second, maxHeight - minY});
        }
        else if (maxHeight - outPoints[0].first.second != maxWidth - outPoints[0].first.first){
            cout << "NIE" << endl;
            return;
        }
        else {
            goodPoints.insert({outPoints[0].second, maxHeight - outPoints[0].first.second});
        }
    }
    else
    {
        cout << "NIE" << endl;
        return;
    }

    cout << "TAK ";
    for (auto g : goodPoints)
    {
        cout << g.second << " ";
    }
    cout << endl;

    return;
}

int main()
{
    int T;
    scanf("%d", &T);
    for (int t=0;t<T;t++)
    {
        int N;
        scanf("%d", &N);
        solve(N);
    }

}