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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ss(x) (int) x.size()
#define fi first
#define se second
#define cat(x) cout << #x << " = " << x << endl
#define root1 dol.t
#define root2 gora.t
#define bst set <int>

using namespace std;

const int MAX = 15000000;
const int pod = (1 << 17);
const int nax = 1e5 + 111;

struct seg1 {
	struct nod {
		nod *l, *r;
		int max;
	};
	nod t[MAX];
	nod *wsk = t + 1;
	nod *alo() {
		nod* r = wsk++;
		r->max = 0;
		return r;
	}
	auto mak(auto &x) {
		x = x ? x : alo();
		return x;
	}
	void go(const int& x, const int& y, const int& c, nod *v, int l = 0, int r = pod - 1) {
		if(x <= l && r <= y) {
			v->max = max(v->max, c);
			return;
		}
		int m = (l + r) / 2;
		if(x <= m)
			go(x, y, c, mak(v->l), l, m);
		if(m < y)
			go(x, y, c, mak(v->r), m + 1, r);
	}
	
	int query(const int&  x, nod *v, int l = 0, int r = pod - 1) {
		if(v == NULL) return 0;
		if(l == r) {
			return v->max;
		}
		int m = (l + r) / 2;
		int s = 0;
		if(x <= m)
			s = query(x, v->l, l, m);
		if(m < x)
			s = query(x, v->r, m + 1, r);
		return max(s, v->max);
	}
} dol;

struct seg2 {
	struct nod {
		nod *l, *r;
		int min = 1000000000;
	};
	nod t[MAX];
	nod *wsk = t + 1;
	nod *alo() {
		nod* r = wsk++;
		r->min = 1000000000;
		return r;
	}
	auto mak(auto &x) {
		x = x ? x : alo();
		return x;
	}
	void go(const int& x, const int& y, const int& c, nod *v, int l = 0, int r = pod - 1) {
		if(x <= l && r <= y) {
			v->min = min(v->min, c);
			return;
		}
		int m = (l + r) / 2;
		if(x <= m)
			go(x, y, c, mak(v->l), l, m);
		if(m < y)
			go(x, y, c, mak(v->r), m + 1, r);
	}
	
	int query(const int& x, nod *v, int l = 0, int r = pod - 1) {
		if(v == NULL) return 1000000000;
		if(l == r) return v->min;
		int m = (l + r) / 2;
		int s = 1000000000;
		if(x <= m)
			s = query(x, v->l, l, m);
		if(m < x)
			s = query(x, v->r, m + 1, r);
		return min(s, v->min);
	}
} gora;

int n, m, q;

inline bool inter1(const int& row, const int& col) {
	return dol.query(col, root1) >= row && (col == 0 ? 1 : dol.query(col - 1, root1) <= row);
}

inline bool inter2(const int& row, const int& col) {
	return gora.query(col, root2) <= row && (col == m - 1 ? 1 : gora.query(col + 1, root2) >= row);
}

int z;

bst downx[nax];
bst downy[nax];

void update1(const int& r);
void update2(const int& c);

inline void ogar1(const int& r, const int& c) {
	update1(r);
	update1(c);
}

void del(const int& r, const int& c) {
	dol.go(c, m - 1, r, root1);
	update1(r);
	update2(c);
}

inline bool under1(const int& r, const int& c) {
	return r <= dol.query(c, root1);
}

void update1(const int& r) { 
	while(ss(downy[r]) && under1(r, *--downy[r].end())) {
		z = *--downy[r].end();
		downy[r].erase(z);
		del(r + 1, z - 1);
	}
}

void update2(const int& c) { 
	while(ss(downx[c]) && under1(*downx[c].begin(), c)) {
		z = *downx[c].begin();
		downx[c].erase(z);
		del(z + 1, c - 1);
	}
}

bst upx[nax];
bst upy[nax];

void up1(const int& r);
void up2(const int& c);

inline void ogar2(const int& r, const int& c) {
	up1(r);
	up2(c);
}

void del2(const int& r, const int& c) {
	//cout << "del2 " << r << " " << c << endl;
	gora.go(0, c, r, root2);
	up1(r);
	up2(c);
}

inline bool under2(const int& r, const int& c) {
	return gora.query(c, root2) <= r;
}

void up1(const int& r) {
	while(ss(upy[r]) && under2(r, *upy[r].begin())) {
		int z = *upy[r].begin();
		upy[r].erase(z);
		del2(r - 1, z + 1);
	}
}

void up2(const int& c) {
	while(ss(upx[c]) && under2(*--upx[c].end(), c)) {
		int z = *--upx[c].end();
		upx[c].erase(z);
		del2(z - 1, c + 1);
	}
}
		
void add(const int& r, const int& c) {
	downx[c].insert(r);
	downy[r].insert(c);
	upx[c].insert(r);
	upy[r].insert(c);
}
	
/*void daj() {
 * 	cout << "dol \n";
	//for(int i = 0; i < m; ++i) {
	//	cout << dol.query(i, root1) << " a ";
	//}
	//cout << endl << endl;
	cout << "gora \n";
	for(int i = 0; i < m; ++i)
		cout << gora.query(i, root2) << " ";
	cout << endl << endl << endl;
}
*/

int main() {
	scanf("%d %d %d", &n, &m, &q);
	dol.go(0, m - 1, 0, root1);
	dol.go(m - 1, m - 1, n - 1, root1);
	
	gora.go(0, m - 1, n - 1, root2);
	gora.go(0, 0, 0, root2);
	
	int x = 0;
	
	while(q--) {
		int r, c, z;
		scanf("%d %d %d", &r, &c, &z);
		r ^= x;
		c ^= x;
		r %= n;
		c %= m;
		
		//cat(r);
		//cat(c);
		
		//cat(inter1(r, c));
		//cat(inter2(r, c));
		
		
		if(inter1(r, c) && inter2(r, c)) {
			printf("TAK\n");
			x ^= z;
			continue;
		}
		add(r, c);
		ogar1(r, c);
		ogar2(r, c);
		printf("NIE\n");
	}

	//cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
	
	
	return 0;
}