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
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (decltype(n) i = (a), i##__ = (n); i <= i##__; ++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define FORD(i,a,n) for (decltype(a) i = (a), i##__ = (n); i >= i##__; --i)
#define REPD(i,n) FORD(i,(n)-1,0)
#define ALL(x) x.begin(), x.end()
#define EB emplace_back
#define ST first
#define ND second
#define SZ(x) ((int)x.size())
#define RS resize
#define V vector
constexpr char nl = '\n';

using LL = long long;
using PII = pair<int, int>;
using VI = V<int>;
using VVI = V<VI>;
using VPII = V<PII>;
using VVPII = V<VPII>;
using VB = V<bool>;
using VVB = V<VB>;

template<class T, class B> void mini(T &&a, B &&b) { if (b < a) a = b; }
template<class T, class B> void maxi(T &&a, B &&b) { if (b > a) a = b; }
int pow2(int x) { return x < 2 ? 0 : sizeof(x) * 8 - __builtin_clz(x - 1); }
 
template<class T> struct Off { T a, b; };
template<class T, class X = typename enable_if<!is_same<string, T>::value, T>::type> auto O(T &x) -> Off<decltype(x.begin())> { return {x.begin(), x.end()}; }
template<class T> auto O(T &x) -> decltype(cerr << x, x) { return x; }
template<class T> auto O(T &x) -> decltype(x.first, x) { return x; }
struct Debug {
    ~Debug() { cerr << nl; }
    template<class T> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << O(x); return *this; }
    template<class T> auto operator<<(T x) -> decltype(x.begin(), typename enable_if<!is_same<string, T>::value, T>::type(), *this) {
        cerr << "{\n";
        for (auto a = x.begin(); a != x.end(); ++a)
            *this << "  " << distance(x.begin(), a) << ": " << O(*a) << '\n';
        return *this << "}";
    }
    template<class T, class B> Debug& operator<<(pair<T, B> p) { 
        return *this << "(" << O(p.first) << ", " << O(p.second) << ")"; 
    }
    template<class T> Debug& operator<<(Off<T> d) {
        cerr << "{";
        for (auto a = d.a, b = d.b; a != b; ++a)
            *this << O(*a) << (next(a) == b ? "" : ", ");
        return *this << "}";
    }
};
struct DebugOff { template<class T> DebugOff& operator<<(T) { return *this; } };
 
#ifdef DEBUG
# define D Debug()
#else
# define D DebugOff()
#endif
#define I(x...) #x ": " << x << "   "
#define TD(X, ...) ostream& operator<<(ostream &os, X &x) { __VA_ARGS__; return os; }

//end of templates

struct Point { int x, y, id; };
TD(Point, os << '(' << x.x << ", " << x.y << ')');
bool operator<(const Point &a, const Point &b) { return a.y == b.y ? a.x < b.x : a.y > b.y; }
using VP = V<Point>;

struct Segment { int l, r, h; };
TD(Segment, os << '(' << x.l << ", " << x.r << ": " << x.h << ')');
using VS = V<Segment>;

struct Tree {
    VS v;
    void add(Point p1, Point p2) { v.EB(Segment{p1.x, p2.x, p1.y}); }
    int get(Point p) {
        int d = 2e9;
        for(Segment &s : v)
            if(s.l >= p.x) {
                if(s.l - p.x > s.h - p.y)
                    mini(d, s.l - p.x);
                else
                    mini(d, s.h - p.y);
            } else if(s.r > p.x)
                mini(d, s.h - p.y);
            //// if(edges[k].begin < points[j].x + edges[k].y - points[j].y && edges[k].end > points[j].x)
            // if(p.x - p.y + s.h > s.l && s.r > p.x)
            //     mini(d, s.h - p.y);
        return d;
    }
};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t --> 0) {

        int n;
        cin >> n;
        VP in(n);
        REP(i, n) {
            int x, y;
            cin >> x >> y;
            in[i] = {x, y, i};
        }
        D << I(n) << I(in);

        bool found = 0;

        REP(xd, 2) {
            if(xd)
                for(auto &p : in)
                    swap(p.x, p.y);
            sort(ALL(in));
            D << I(in);

            Point left_up = {int(2e9), int(2e9), -1};
            for(auto &p : in)
                if(p.x == left_up.x && p.y > left_up.y)
                    left_up = p;
                else if(p.x < left_up.x)
                    left_up = p;
            D << I(left_up);

            VI possible_h;
            for(auto &p : in)
                possible_h.EB(left_up.y + p.x - left_up.x);
            D << I(possible_h);

            for(int h : possible_h) {
                Tree t;
                t.add(Point{0, h, -1}, Point{int(2e9), h, -1});

                VI sz(n);
                for(auto &p : in) {
                    sz[p.id] = t.get(p);
                    t.add(p, Point{p.x + sz[p.id], p.y, p.id});
                }
                D << I(h) << I(sz);

                bool valid = 1;
                REP(i, n)
                    if(sz[i] <= 0)
                        valid = 0;
                int minx = 2e9, maxx = 0, miny = 2e9, maxy = 0;
                LL pole = 0;
                REP(i, n) {
                    mini(minx, in[i].x);
                    mini(miny, in[i].y);
                    maxi(maxx, in[i].x + sz[in[i].id]);
                    maxi(maxy, in[i].y + sz[in[i].id]);
                    pole += LL(sz[i]) * sz[i];
                    // D << in[i] << " " << sz[i] << " " << in[i].y + sz[i];
                }
                D << I(pole) << I(maxx) << I(minx) << I(maxy) << I(miny);
                if(pole == LL(maxx - minx) * (maxy - miny) && valid) {
                    found = 1;
                    cout << "TAK ";
                    for(int i : sz)
                        cout << i << " ";
                    cout << nl;
                    break;
                }
            }
            if(found)
                break;
        }
        if(!found)
            cout << "NIE\n";

    }
}