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
#include <stdio.h>
#include <vector>
#include <algorithm>

struct Corner {
    unsigned long long x;
    unsigned long long y;
    int x_index;
    int y_index;
    unsigned long long size;

    void constructor() {
        this->size = 0;
    }
};

bool valueInRange(unsigned long long value, unsigned long long min, unsigned long long max)
{ return (value > min) && (value < max); }

bool rectOverlap(Corner A, Corner B)
{
    bool xOverlap = valueInRange(A.x, B.x, B.x + B.size) ||
                    valueInRange(B.x, A.x, A.x + A.size);

    bool yOverlap = valueInRange(A.y, B.y, B.y + B.size) ||
                    valueInRange(B.y, A.y, A.y + A.size);

    return xOverlap && yOverlap;
}

bool overlaps(Corner &processed_corner, std::vector<Corner*> &corners_x_sorted) {
    for (int i = 0; i < corners_x_sorted.size(); i++) {
        if (rectOverlap(processed_corner, *corners_x_sorted[i])) {
            return true;
        }
    }
    return false;
}

bool solve(Corner &processed_corner, std::vector<Corner*> &corners_x_sorted, std::vector<Corner*> &corners_y_sorted, unsigned long long area_sum, unsigned long long max_x, unsigned long long max_y) {
    if (processed_corner.x_index == corners_x_sorted.size() - 1) {

        if (max_y > processed_corner.y) {
            unsigned long long y_delta = max_y - processed_corner.y;
            unsigned long long _max_x = std::max(max_x, processed_corner.x + y_delta);
            unsigned long long _max_y = std::max(max_y, processed_corner.y + y_delta);
            processed_corner.size = y_delta;
            if (!overlaps(processed_corner, corners_x_sorted) && area_sum + y_delta*y_delta == (_max_x - corners_x_sorted[0]->x) * (_max_y - corners_y_sorted[0]->y)) {
                return true;
            }
        }
        if (max_x > processed_corner.x) {
            unsigned long long x_delta = max_x - processed_corner.x;
            unsigned long long _max_x = std::max(max_x, processed_corner.x + x_delta);
            unsigned long long _max_y = std::max(max_y, processed_corner.y + x_delta);
            processed_corner.size = x_delta;
            if (!overlaps(processed_corner, corners_x_sorted) && area_sum + x_delta*x_delta == (_max_x - corners_x_sorted[0]->x) * (_max_y - corners_y_sorted[0]->y)) {
                return true;
            }
        }

        processed_corner.size = 0;
        return false;
    }
    for (int i = processed_corner.x_index + 1; i < corners_x_sorted.size(); i++) {
        if (corners_x_sorted[i]->x > processed_corner.x) {
            processed_corner.size = corners_x_sorted[i]->x - processed_corner.x;
            if (!overlaps(processed_corner, corners_x_sorted) && processed_corner.size <= 2*10e9 && solve(
                *corners_x_sorted[processed_corner.x_index + 1],
                corners_x_sorted,
                corners_y_sorted,
                area_sum + processed_corner.size * processed_corner.size,
                std::max(max_x, processed_corner.x + processed_corner.size),
                std::max(max_y, processed_corner.y + processed_corner.size)
            )) {
                return true;
            }
        }

        if (corners_x_sorted[i]->y > processed_corner.y) {
            processed_corner.size = corners_x_sorted[i]->y - processed_corner.y;
            if (!overlaps(processed_corner, corners_x_sorted) && processed_corner.size <= 2*10e9 && solve(
                *corners_x_sorted[processed_corner.x_index + 1],
                corners_x_sorted,
                corners_y_sorted,
                area_sum + processed_corner.size * processed_corner.size,
                std::max(max_x, processed_corner.x + processed_corner.size),
                std::max(max_y, processed_corner.y + processed_corner.size)
            )) {
                return true;
            }
        }
    }
    processed_corner.size = 0;
    return false;
}


int main() {
    int t;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        int n;
        scanf("%d", &n);
        std::vector<Corner> corners(n);

        for (Corner &corner : corners) {
            scanf("%llu %llu", &corner.x, &corner.y);
        }

        std::vector<Corner*> corners_x_sorted(n);
        std::vector<Corner*> corners_y_sorted(n);

        for (int i = 0; i < n; i++) {
            corners_x_sorted[i] = &corners[i];
            corners_y_sorted[i] = &corners[i];
        }

        std::sort(corners_x_sorted.begin(), corners_x_sorted.end(), [](const Corner* a, const Corner* b) {
            if (a->x == b->x) {
                return a->y < b->y;
            }
            return a->x < b->x;
        });

        for (int i = 0; i < corners_x_sorted.size(); i++) {
            corners_x_sorted[i]->x_index = i;
        }

        std::sort(corners_y_sorted.begin(), corners_y_sorted.end(), [](const Corner* a, const Corner* b) {
            if (a->y == b->y) {
                return a->x < b->x;
            }
            return a->y < b->y;
        });

        if(solve(*corners_x_sorted[0], corners_x_sorted, corners_y_sorted, 0, 0, 0)) {
            printf("TAK");

            for (Corner &corner : corners) {
                printf(" %llu", corner.size);
            }

            puts("");
        } else {
            puts("NIE");
        }
    }

    return 0;
};