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
#include <iostream>
#include <vector>

using namespace std;

struct pkt
{
    int x, y, odp;
};

const int INF = 1000000000;

bool ok;
vector<int> odp;

bool Spr(vector<pkt>& p)
{
    int min_x = INF, max_x = -INF;
    int min_y = INF, max_y = -INF;
    int P = 0;
    for (int i = 0; i < p.size(); ++i)
    {
        min_x = min(min_x, p[i].x);
        max_x = max(max_x, p[i].x + p[i].odp);
        min_y = min(min_y, p[i].y);
        max_y = max(max_y, p[i].y + p[i].odp);
        P += p[i].odp * p[i].odp;
    }
    for (int i = 0; i < p.size(); ++i)
    {
        for (int j = i + 1; j < p.size(); ++j)
        {
            int le = max(p[i].x, p[j].x);
            int pr = min(p[i].x + p[i].odp, p[j].x + p[j].odp);
            int dol = max(p[i].y, p[j].y);
            int gora = min(p[i].y + p[i].odp, p[j].y + p[j].odp);
            if (le < pr && dol < gora)
                return false;
        }
    }
    if (P == (max_x - min_x) * (max_y - min_y))
        return true;
    return false;
}

void rec(vector<pkt> p, int nr, int x)
{
    p[nr].odp = x;
    if (nr == p.size() - 1)
    {
        if (Spr(p))
        {
            ok = true;
            for (int i = 0; i < p.size(); ++i)
                odp.push_back(p[i].odp);
        }
        return;
    }
    else
        for (int i = 1; i <= 6; ++i)
            rec(p, nr + 1, i);
}

int main()
{
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        int n;
        cin >> n;
        vector<pkt> p(n);
        for (int j = 0; j < n; ++j)
            cin >> p[j].x >> p[j].y;
        if (n > 6)
        {
            cout << "NIE" << endl;
            continue;
        }
        ok = false;
        odp.clear();
        for (int j = 1; j <= 6; ++j)
            rec(p, 0, j);
        if (ok)
        {
            cout << "TAK" << endl;
            for (int j = 0; j < n; ++j)
                cout << odp[j] << " ";
        }
        else
            cout << "NIE";
        cout << endl;
    }
    return 0;
}