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
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   Vi         = vector<int>;
using   Pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

struct Board {
	int wid, hei;
	vector<set<int>> rows, cols;
	set<Pii> down;

	void init(int w, int h) {
		wid = w;
		hei = h;
		rows.resize(h+2);
		cols.resize(w+2);
		down.insert({0, 1});
		down.insert({w, h+1});
	}

	bool touch(int x, int y) {
		auto it = down.lower_bound({x, -1});
		//assert(it != down.begin() && it != down.end());
		if (y+1 >= it->y) return y+1 == it->y;
		auto prv = prev(it);
		return prv->x == x-1 && y+1 >= prv->y;
	}

	void check(int x, int y) {
		if (!touch(x, y)) return;
		auto it = down.insert({x, y}).x;

		while (1) {
			auto prv = prev(it);
			if (prv->y < it->y) break;
			down.erase(prv);
		}

		while (1) {
			auto nxt = next(it);
			if (nxt->x > it->x) break;
			down.erase(nxt);
		}

		auto c1 = rows[y-1].upper_bound(x+1);
		if (c1 != rows[y-1].begin()) check(*prev(c1), y-1);

		auto c2 = cols[x+1].lower_bound(y-1);
		if (c2 != cols[x+1].end()) check(x+1, *c2);
	}

	void add(int x, int y) {
		rows[y].insert(x);
		cols[x].insert(y);
		check(x, y);
	}
};

int n, m, k;
Board normal, flipped;

bool add(int x, int y) {
	if (normal.touch(x, y) && flipped.touch(y, x)) {
		return 1;
	}
	normal.add(x, y);
	flipped.add(y, x);
	return 0;
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(18);

	cin >> n >> m >> k;
	normal.init(m, n);
	flipped.init(n, m);

	int x = 0;

	rep(i, 0, k) {
		int r, c, z; cin >> r >> c >> z;
		r = (r ^ x) % n;
		c = (c ^ x) % m;
		bool ans = add(c+1, r+1);
		if (ans) x ^= z;
		cout << (ans ? "TAK\n": "NIE\n");
	}

	return 0;
}