#include <bits/stdc++.h>
using namespace std;
bool Bajtogrom (int z, int y, set < pair <int, int> > S, bool odw[], int n, int m)
{
if (odw[m * z + y]) return true;
// cout << z << ' ' << y << "\n";
if (z == n - 1 && y == m - 1) return false;
odw[m * z + y] = true;
pair <int, int> P;
P.first = z;
P.second = y;
if (S.find(P) != S.end()) return true;
bool a, b, c = false, d = false;
++(P.first);
if (S.find(P) != S.end()) c = true;
if (z < n - 1) a = Bajtogrom (z+1, y, S, odw, n, m);
else a = true;
if (a || c)
{
--(P.first);
++(P.second);
if (S.find(P) != S.end()) d = true;
if (y < m - 1) b = Bajtogrom (z, y+1, S, odw, n, m);
else b = true;
if ((c && d) || (a && b)) return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k, x = 0;
cin >> n >> m >> k;
int dane[k][3];
for (int i = 0; i < k; ++i)
for (int j = 0; j < 3; ++j)
cin >> dane[i][j];
set < pair <int, int> > S;
for (int i = 0; i < k; ++i)
{
bool odw[25000001] = {false};
pair <int, int> P ((dane[i][0]^x) % n, (dane[i][1]^x) % m);
// cout << P.first << ' ' << P.second << '\n';
S.insert(P);
// cout << j << ' ' << war[j][0] << ' ' << war[j][1] << '\n';
bool czy = Bajtogrom (0, 0, S, odw, n, m);
if (czy)
{
x = x^dane[i][2];
S.erase(P);
cout << "TAK\n";
}
else cout << "NIE\n";
}
return 0;
}