# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	while (cin >> n&&n) {
		long long member[4001] = {};
		bool flag[4001] = {};
		for (int i = 1; i <= 4000; i++) {
			member[i] = 0;
			flag[i] = false;
		}
		long long a, b, c;
		int count = 0;
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> c;
			member[a] += b*c;
			if (member[a] >= 1000000&&!flag[a]) {
				count++;
				flag[a] = true;
				cout << a << endl;
			}
		}
		if (count == 0)cout << "NA" << endl;
	}
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int a, b, c;
	while (cin >> a >> b >> c, a | b | c) {
		int N;
		cin >> N;
		vector<int> res(a + b + c, 2);
		vector<vector<int>> no;
		for (int i = 0; i < N; i++) {
			int x, y, z, t;
			cin >> x >> y >> z >> t; x--, y--, z--;
			if (t) {
				res[x] = res[y] = res[z] = 1;
			}
			else {
				no.push_back(vector<int>({ x, y, z }));
			}
		}
		for (auto t : no) {
			int x = t[0], y = t[1], z = t[2];
			if (res[x] == 1 && res[y] == 1) {
				res[z] = 0;
			}
			else if (res[y] == 1 && res[z] == 1) {
				res[x] = 0;
			}
			else if (res[z] == 1 && res[x] == 1) {
				res[y] = 0;
			}
		}
		for (int i = 0; i < a + b + c; i++) {
			cout << res[i] << endl;
		}
	}
	return 0;
}

