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
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <set>


// M*N < 25kk version START
const int MAX = 25000005;
char M[MAX];
int n,m,k;
char trash;

char& Map(int x, int y) {
    if (x==0 && y==0) {
        trash = 'S';
        return trash;
    }
    if (x==m-1 && y==n-1) {
        trash = 'D';
        return trash;
    }
    if (x >= m || y < 0) {
        trash = 'T';
        return trash;
    }
    if (x < 0 || y >= n) {
        trash = 'B';
        return trash;
    }
    return M[y*m+x];
}

void Dbg() {
    for (int y=0;y<n;++y) {
        for (int x=0;x<m;++x)
            std::clog << (Map(x,y) == 0 ? '.' : Map(x,y));
        std::clog << std::endl;
    }
}

std::unordered_set<char> neighbour(int x, int y) {
    std::unordered_set<char> C;
    C.insert(Map(x-1, y-1));
    C.insert(Map(x-1, y));
    C.insert(Map(x-1, y+1));
    C.insert(Map(x, y-1));
    C.insert(Map(x, y+1));
    C.insert(Map(x+1, y-1));
    C.insert(Map(x+1, y));
    C.insert(Map(x+1, y+1));
    return C;
}

void floodTop(int x, int y) {
    if (x==0 && y==0) return;
    if (x==m-1 && y==n-1) return;
    if (x < 0 || x >= m || y < 0 || y >= n) return;
    char& c = Map(x,y);
    if (c == 'T') return;
    c = 'T';
    if (Map(x-1,y+1) == 'M')
        floodTop(x-1, y+1);
    if (Map(x,y+1) == 'M')
        floodTop(x, y+1);
    if (Map(x-1,y) == 'M')
        floodTop(x-1, y);
    floodTop(x+1, y);
    floodTop(x, y-1);
}

void floodBot(int x, int y) {
    if (x==0 && y==0) return;
    if (x==m-1 && y==n-1) return;
    if (x < 0 || x >= m || y < 0 || y >= n) return;
    char& c = Map(x,y);
    if (c == 'B') return;
    c = 'B';
    if (Map(x+1,y-1) == 'M')
        floodBot(x+1, y-1);
    if (Map(x,y-1) == 'M')
        floodBot(x, y-1);
    if (Map(x+1,y) == 'M')
        floodBot(x+1, y);
    floodBot(x-1, y);
    floodBot(x, y+1);
}

bool solve(int x, int y) {
    auto ne =  neighbour(x, y);
    if (ne.count('B') && ne.count('T')) return true;
    else if (ne.count('B')) floodBot(x, y);
    else if (ne.count('T')) floodTop(x, y);
    else Map(x, y) = 'M';
    return false;
}
// M*N < 25kk version END

// UNLIMITED POWER version START

const int MAX2 = 100010;
// const int MAX2 = 13000000;

std::set<int> XMT[MAX2];
std::set<int> YMT[MAX2];
std::set<int> XMB[MAX2];
std::set<int> YMB[MAX2];

void AddMid(int x, int y) {
    XMT[x +5].insert(-y);
    YMT[y +5].insert(x);
    XMB[x +5].insert(y);
    YMB[y +5].insert(-x);
}

std::set<int> XT[MAX2];
std::set<int> YT[MAX2];

void AddTop(int x, int y) {
    XT[x +5].insert(y);
    YT[y +5].insert(-x);

    auto itx = XMT[x-1 +5].lower_bound(-y-1);
    if (itx != XMT[x-1 +5].end()) {
        int yy = *itx;
        XMT[x-1 +5].erase(itx);
        YMT[-yy +5].erase(x-1);
        AddTop(x-1, -yy);
    }
    auto ity = YMT[y+1 +5].lower_bound(x-1);
    if (ity != YMT[y+1 +5].end()) {
        int xx = *ity;
        YMT[y+1 +5].erase(ity);
        XMT[xx +5].erase(-y-1);
        AddTop(xx, y+1);
    }
}


std::set<int> XB[MAX2];
std::set<int> YB[MAX2];

void AddBot(int x, int y) {
    XB[x +5].insert(-y);
    YB[y +5].insert(x);

    auto itx = XMB[x+1 +5].lower_bound(y-1);
    if (itx != XMB[x+1 +5].end()) {
        int yy = *itx;
        XMB[x+1 +5].erase(itx);
        YMB[yy +5].erase(-x-1);
        AddBot(x+1, yy);
    }
    auto ity = YMB[y-1 +5].lower_bound(-x-1);
    if (ity != YMB[y-1 +5].end()) {
        int xx = *ity;
        YMB[y-1 +5].erase(ity);
        XMB[-xx +5].erase(y-1);
        AddBot(-xx, y-1);
    }
}

bool solve2(int x, int y) {
    bool top = XT[x+1 +5].lower_bound(y-1) != XT[x+1 +5].end() || YT[y-1 +5].lower_bound(-x-1) != YT[y-1 +5].end();
    bool bot = XB[x-1 +5].lower_bound(-y-1) != XB[x-1 +5].end() || YB[y+1 +5].lower_bound(x-1) != YB[y+1 +5].end();
    // std::clog << x << " " << y << " : " << top << " " << bot << std::endl;
    if (top && bot) return true;
    else if (top) AddTop(x, y);
    else if (bot) AddBot(x, y);
    else AddMid(x, y);
    return false;
}

void Init() {
    AddTop(-1, -1);
    AddTop(m, n);
    AddBot(-1, -1);
    AddBot(m, n);
}
// UNLIMITED POWER version END

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin >> n >> m >> k;
    Init();
    int xx = 0;
    for (int i=0;i<k;++i) {
        int r,c,z;
        std::cin >> r >> c >> z;

        int y = (r^xx)%n;
        int x = (c^xx)%m;

        if (solve2(x, y)) {
            std::cout << "TAK\n";
            xx^=z;
        }
        else {
            std::cout << "NIE\n";
        }

        // std::clog << "@ " << solve(x, y) << std::endl;
        // std::clog << "### " << x << " " << y << std::endl;
        // Dbg();
        // std::clog << "### ^^^^^^^^^^^^^ " << std::endl; 
    }

    return 0;
}