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
#include<iostream>
#include<vector>

using namespace std;

bool DFS(vector < vector < bool > > pole, int y, int x, int n, int m){
	if(y==n and x == m){
		return true;
	}else{
		bool right=false;;
		if(pole[y][x+1]==true){
			right = DFS(pole, y, x+1, n, m);
		}
		if(right){
			return true;
		}else{
			bool down=false;
			if(pole[y+1][x]==true){
				down = DFS(pole, y+1, x, n, m);
			}	
			return down;
		}
	}
}

int main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n, m;
	cin >> n >> m;
	
	vector < vector < bool > >pole;
	
	vector < int > row;
	
	for(int i=0; i<n+2; i++){
		vector < bool > row;
		if(i==0 or i==n+1){
			for(int j=0; j<m+2; j++){
				row.push_back(false);
			}
		}else{
			row.push_back(false);
			for(int j=0; j<m; j++){
				row.push_back(true);
			}
			row.push_back(false);
		}
		pole.push_back(row);
	}
	
	int k;
	
	cin >> k;
	
	int x=0;
	
	
	for(int i=0; i<k; i++){
		int r,c,z;
		cin >> r >> c >>z;
		r = (r^x)%n+1;
		c = (c^x)%m+1;
		//cout << r << " " << c << endl;
		pole[r][c]=false;
		
		bool road = DFS(pole, 1, 1, n ,m);
		
		/*cout << endl ;
		for(int l=0; l<n+2; l++){
			for (int j=0; j<m+2; j++){
				cout << pole[l][j];
			}
			cout << endl;
		}
		cout << endl;*/
		if(road){
			cout << "NIE" << endl;
			
		}else{
			cout << "TAK" << endl;
			pole[r][c]=true;
			x=x^z;
		}
	}
	
}