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
#include<cstdio>

int n, m, k;
int ** M;
int x;

void input() {
  scanf("%d %d %d", &n, &m, &k);
  x = 0;
  M = new int*[n];
  for (int i = 0; i < n; i++) {
    M[i] = new int[m];
  }
}

bool possible(int i, int j, int q) {
  M[0][0] = q;
  for (int a = 0; a < n; a++) {
    for (int b = 0; b < m; b++) {
      if (M[a][b] != -1) {
        if (a > 0 && M[a - 1][b] == q) {
          M[a][b] = q;
        }
        if (b > 0 && M[a][b - 1] == q) {
          M[a][b] = q;
        }
      }
    }
  }
  return M[n-1][m-1] == q;
}

void solve(int r, int c, int z, int q) {
  int i = (r ^ x) % n;
  int j = (c ^ x) % m;
  M[i][j] = -1;
  if (possible(i, j, q)) {
    printf("NIE\n");
  } else {
    M[i][j] = 0;
    x ^= z;
    printf("TAK\n");
  }
}

void solve() {
  int r, c, z;
  for (int i = 0; i < k; i++) {
    scanf("%d %d %d", &r, &c, &z);
    solve(r, c, z, i+1);
  }
}

int main() {
  input();
  solve();
  return 0;
}