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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include <set>
#include <map>
#include <limits>
#include <vector>
#include <queue>

#include <iomanip>               // for std::setw
#include <ios>                   // for std::noskipws, streamsize
#include <istream>               // for std::istream
#include <ostream>               // for std::ostream
#include <sstream>               // for std::ostringstream
#include <cstddef>               // for NULL
#include <stdexcept>             // for std::domain_error
#include <string>                // for std::string implicit constructor
#include <cstdlib>               // for std::abs
#include <limits>                // for std::numeric_limits
#include <cassert>

#include<type_traits>

using namespace std;

// #define DEBUG

#ifdef DEBUG
#define dbg cerr
// #define dbg2 cerr
#define dbg2 0 && cerr
#else
#define dbg 0 && cerr
#define dbg2 0 && cerr
#endif


struct FieldState {
    uint8_t state;

    bool marked() {
        return (state & 0b1) == 1;
    }

    void mark() {
        state = 1;
    }

    void clear() {
        state = 0;
    }
};


struct Board {
    int width, height;
    vector<FieldState> board;

    Board(int width, int height) : width(width), height(height) {
        for(int i = 0; i < width*height; ++i) {
            board.push_back(FieldState());
        }
    }

    int index(int x, int y) {
        return x + y*width;
    }

    bool ok(int x, int y) {
        if (x >= width || y >= height || x < 0 || y < 0) {
            return false;
        }
        if (x == 0 && y == 0) {
            return false;
        }
        return true;
    }

    bool ok(pair<int, int> p) {
        return ok(p.first, p.second);
    }

    FieldState& get(int x, int y) {
        if (x >= width || y >= height || x < 0 || y < 0) {
            static FieldState state_marked;
            state_marked.mark();
            return state_marked;
        }
        if (x == 0 && y == 0) {
            static FieldState state_empty;
            state_empty.clear();
            return state_empty;
        }
        return board[index(x, y)];
    }

    FieldState& get(pair<int, int> p) {
        return get(p.first, p.second);
    }

    FieldState& state_end() {
        return get(width-1, height-1);
    }

#ifdef DEBUG
    void print() {
        for (int x = 0; x < width+2; ++x) {
            dbg << "-";
        }
        dbg << "\n";
        for (int y = 0; y < height; ++y) {
            dbg << "|";
            for (int x = 0; x < width; ++x) {
                if (get(x, y).marked()) {
                    dbg << "x";
                } else {
                    dbg << ".";
                }
            }
            dbg << "|\n";
        }
        for (int x = 0; x < width+2; ++x) {
            dbg << "-";
        }
        dbg << "\n\n";
    }
#endif
};

Board* board;

struct Coords {
    unsigned int r, c, z;
    unsigned int x;
    Board* board;

    Coords(Board* board) : x(0), board(board) {}

    std::pair<int, int> move() {
        cin >> r >> c >> z;

        dbg << "r = " << r << ", c = " << c << ", x = " << x << endl;

        return make_pair((r^x) % board->height, (c^x)  % board->width);
    }

    void mark_destroy() {
        x ^= z;
    }
};

bool query() {
    static Coords coords(board);

    queue<FieldState*> nodes_deleted;

    auto _coords = coords.move();
    auto y = _coords.first;
    auto x = _coords.second;

    // int x, y;
    // cin >> x >> y;

    queue<pair<int, int>> Q;

    Q.push(make_pair(x, y));

    board->get(x, y).mark();
    nodes_deleted.push(&board->get(x, y));

    dbg << "Query " << x << ", " << y << endl;


    while(!Q.empty()) {
#ifdef DEBUG
        board->print();
#endif

        auto now = Q.front();
        dbg << "now " << now.first << ", " << now.second << endl;
        Q.pop();

        for (int dx = -1; dx <= 1; ++dx) {
            for (int dy = -1; dy <= 1; ++dy) {
                auto now2 = make_pair(now.first + dx, now.second + dy);

                if (! board->ok(now2)) {
                    continue;
                }


                if (! board->get(now2).marked()) {
                    if (board->get(make_pair(now2.first-1, now2.second)).marked() && board->get(make_pair(now2.first, now2.second-1)).marked()) {
                        board->get(now2).mark();
                        
                        nodes_deleted.push(&board->get(now2));

                        Q.push(now2);
                    }
                }
            }
        }
    }

#ifdef DEBUG
    board->print();
#endif


    bool result = false;

    if (board->state_end().marked()) {
        result = true;

        while (! nodes_deleted.empty()) {
            auto now = nodes_deleted.front();
            nodes_deleted.pop();

            now->clear();
        }

        coords.mark_destroy();
    }
    
    while (! nodes_deleted.empty()) {
        nodes_deleted.pop();
    }




    return result;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int height, width, queries;
    cin >> height >> width >> queries;

    Board _board(width, height);
    board = &_board;

    while(queries--) {
        if (query()) {
            cout << "TAK\n";
        } else {
            cout << "NIE\n";
        }
    }
}