#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int s[501],q[1001][5];
int main()
{
	int i,j,n,m,a,b,c;
	while(cin>>a>>b>>c)
	{
		if(a==0&&b==0&&c==0)break;
		cin>>n;
		for(i=1;i<=500;i++)s[i]=2;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=4;j++)
			{
				cin>>q[i][j];
			}
			if(q[i][4]==1)
			{
				s[q[i][1]]=1;
				s[q[i][2]]=1;
				s[q[i][3]]=1;
			}
		}
		for(i=1;i<=n;i++)
		{
			if(s[q[i][1]]==1&&s[q[i][2]]==1&&q[i][4]==0)s[q[i][3]]=0;
			else if(s[q[i][1]]==1&&s[q[i][3]]==1&&q[i][4]==0)s[q[i][2]]=0;
			else if(s[q[i][3]]==1&&s[q[i][2]]==1&&q[i][4]==0)s[q[i][1]]=0;
		}
		for(i=1;i<=a+b+c;i++)
		{
			cout<<s[i]<<endl;
		}
	}
	return 0;
}
#include<iostream>
#include<cstring>
using namespace std;

int main(){
	int n,m,x[10001],y[10001],c;
	bool tomo[600][600],party[600][2];
	while(true){
		memset(tomo,false,sizeof(tomo));
		memset(party,false,sizeof(party));
		cin>>n;
		cin>>m;
		c=0;
		if(n==0 && m==0){
			break;
		}
		for(int i=0;i<m;i++){
			cin>>x[i]>>y[i];
			tomo[x[i]][y[i]]=true;
			tomo[y[i]][x[i]]=true;
		}
		for(int i=2;i<=n;i++){
			if(tomo[1][i]==true){
				party[i][0]=true;
			}
		}
		for(int i=2;i<=n;i++){
			if(party[i][0]==true){
				for(int j=2;j<=n;j++){
					if(tomo[i][j]==true){
						party[j][1]=true;
					}
				}
			}
		}
		for(int i=2;i<=n;i++){
			if(party[i][0]==true || party[i][1]==true){
				c++;
			}
		}
		cout<<c<<endl;
	}
	return 0;
}