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

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, int> PILL;
typedef pair<ll, ll> PLL;

const int MAX_N = 1e6+5;
const int M = 1e6;
const ll INF = (ll)(1e18);
const int inf = 1e9;
const ll MOD = 1000000007LL;
const ll SLOW = (ll)(1e8);

void solve() {

}

int n, m, k;
vector<vector<bool>> grid;

bool check() {
    queue<PII> q;
    q.push({0, 0});

    while (!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        if (r == n-1 && c == m-1) return true;
        q.pop();
        if (r < n-1 && grid[r+1][c]) {
            q.push({r+1, c});
        }
        if (c < m-1 && grid[r][c+1]) {
            q.push({r, c+1});
        }
    }

    return false;
}

void brute_force() {
    grid.resize(n);
    for (int i = 0; i < n; i++) {
        grid[i].resize(m, true);
    }

    int x = 0;
    for (int i = 0; i < k; i++) {
        int r, c, z;
        cin >> r >> c >> z;
        r = (r^x) % n;
        c = (c^x) % m;
        grid[r][c] = false;
        if (check()) {
            cout << "NIE\n";
        } else {
            cout << "TAK\n";
            grid[r][c] = true;
            x = x^z;
        }
    }
}

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

    cin >> n >> m >> k;
    brute_force();






    return 0;
}