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

using namespace std;


bool ok(vector< vector<int> > &mat) {
  static int q;
  int n = mat.size();
  int m = mat[0].size();
  
  q++;
  
  static vector< pair<int, int> > Q;
  if(Q.empty()) {
    Q.resize(n * m);
  }
  
  int qstart = 0;
  int qread = 0;
  Q[qstart++] = make_pair(0, 0);
  
  while(qread < qstart) {
    int r = Q[qread].first;
    int c = Q[qread].second;
    qread++;
    
    if(r+1 < n and mat[r+1][c] != -1 and mat[r+1][c] != q) {
      if(r+1 == n-1 and c == m-1) return true;
      
      Q[qstart++] = make_pair(r+1, c);
      mat[r+1][c] = q;
    }
    
    if(c+1 < m and mat[r][c+1] != -1 and mat[r][c+1] != q) {
      if(r == n-1 and c+1 == m-1) return true;
      
      Q[qstart++] = make_pair(r, c+1);
      mat[r][c+1] = q;
    }
  }
  
  return false;
}


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n, m, k;
  cin >> n >> m >> k;
  
  int x = 0;
  vector< vector<int> > mat(n, vector<int>(m, 0));
  
  for(int i = 0; i < k; i++) {
    int r, c, z;
    cin >> r >> c >> z;
    r = (r ^ x) % n;
    c = (c ^ x) % m;
    
    mat[r][c] = -1;
    
    if(ok(mat)) {
      cout << "NIE\n";
    } else {
      cout << "TAK\n";
      mat[r][c] = 0;
      x ^= z;
    }
  }
  
  return 0;
}