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
#include <bits/stdc++.h>
using namespace std;

const string TAK = "TAK";
const string NIE = "NIE";

struct Action {
	int type;
	int left;
	int right;
	map<int, int> map1;
	map<int, int> map2;
	map<int, int> map3;
};
int n, m;

vector<Action> sets;

bool setXcontainsV(const int x, const int v) {
	if (x <= n) {
		return v % x == 0;
	} else {
		Action &a = sets[x - n];
		if (a.type == 1) {
			if (a.map1.count(v)) {
				return a.map1[v];
			} else {
				const int result = setXcontainsV(a.left, v)
						|| setXcontainsV(a.right, v);
				a.map1[v] = result;
				return result;
			}
		} else if (a.type == 2) {
			if (a.map2.count(v)) {
				return a.map2[v];
			} else {
				const int result = setXcontainsV(a.left, v)
						&& setXcontainsV(a.right, v);
				a.map2[v] = result;
				return result;
			}
		} else {
			if (a.map3.count(v)) {
				return a.map3[v];
			} else {
				const int result = !setXcontainsV(a.left, v);
				a.map3[v] = result;
				return result;
			}
		}
	}
}

int main() {
//	ifstream cin("tests/0a.in");

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	int q, x, v;
	cin >> n;
	cin >> m;
	cin >> q;

	sets.resize(m + 1);

	for (int i = 1; i <= m; i++) {
		Action &action = sets[i];
		cin >> action.type;
		cin >> action.left;
		if (action.type != 3) {
			cin >> action.right;
		}
	}
	for (int i = 0; i < q; i++) {
		cin >> x;
		cin >> v;
		cout << (setXcontainsV(x, v) ? TAK : NIE) << endl;
	}
}