#include <iostream>

using namespace std;

int main() {
    class Dice {
        public:
            int men[6];
            void setMen(int i, int value) {men[i] = value;}
            void getMen() {cout << men[0] << endl;}
            void SwitchMen (char Alpha) {
                switch (Alpha) {
                    case 'S':
                        ChangeMen(men[4], men[0], men[2], men[3], men[5], men[1]);
                        break;
                    case 'N':
                        ChangeMen(men[1], men[5], men[2], men[3], men[0], men[4]);
                        break;
                    case 'E':
                        ChangeMen(men[3], men[1], men[0], men[5], men[4], men[2]);
                        break;
                    case 'W':
                        ChangeMen(men[2], men[1], men[5], men[0], men[4], men[3]);
                        break;
                }
            }
            void ChangeMen (int tmp1, int tmp2, int tmp3, int tmp4, int tmp5, int tmp6) {
                men[0] = tmp1;
                men[1] = tmp2;
                men[2] = tmp3;
                men[3] = tmp4;
                men[4] = tmp5;
                men[5] = tmp6;
            }
    };
    Dice dice;
    for(int i = 0; i < 6; i++) {
        int tmp;
        cin >> tmp;
        dice.setMen(i, tmp);
    }
    char Alpha;
    while(cin >> Alpha) {
        dice.SwitchMen(Alpha);
    }

    dice.getMen();

    // for(int i = 0; i < 6; i++) {
    //     cout << dice.men[i] << endl;
    // }

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main(){
	int n,a,b,c;
	while(1){
		cin>>a>>b>>c;
		n=a+b+c;
		if(n==0) break;
		int m;
		cin>>m;
		vi d(n+1,2),x(m),y(m),z(m),f(m);
		rep(i,0,m){
			cin>>x[i]>>y[i]>>z[i]>>f[i];
			if(f[i]==1){
				d[x[i]]=1;
				d[y[i]]=1;
				d[z[i]]=1;
			}
		}
		rep(i,0,m){
			if(f[i]==0){
				if(d[x[i]]==1 && d[y[i]]==1) d[z[i]]=0;
				else if(d[z[i]]==1 && d[y[i]]==1) d[x[i]]=0;
				else if(d[x[i]]==1 && d[z[i]]==1) d[y[i]]=0;
			}
		}
		rep(i,1,n+1){
			o(d[i]);
		}
	}
}