#include <cstdio>
#include <algorithm>
using namespace std;

class Dice {
	int num[6];
public:
	void set(int n, int s) {
		num[s] = n;
	}

	void move(char c) {
		if (c == 'E') {
			swap(num[0], num[3]);
			swap(num[3], num[5]);
			swap(num[5], num[2]);
		}
		else if (c == 'N') {
			swap(num[0], num[1]);
			swap(num[1], num[5]);
			swap(num[5], num[4]);
		}
		else if (c == 'S') {
			swap(num[0], num[4]);
			swap(num[4], num[5]);
			swap(num[5], num[1]);
		}
		else if (c == 'W') {
			swap(num[0], num[2]);
			swap(num[2], num[5]);
			swap(num[5], num[3]);
		}
	}

	int up() {
		return num[0];
	}
};

int main() {
	Dice d;
	int n;
	for (int i = 0; i < 6; i++) {
		scanf("%d", &n);
		d.set(n, i);
	}
	getchar();
	char c;
	while (scanf("%c", &c), c != '\n') {
		d.move(c);
	}
	printf("%d\n", d.up());
}
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  int a, b, c;
  while (cin >> a >> b >> c) {
    if ((a|b|c) == 0)
      break;

    int N;
    cin >> N;

    vector<int> aa(N), bb(N), cc(N), r(N);
    for (int i = 0; i < N; ++i) {
      cin >> aa[i] >> bb[i] >> cc[i] >> r[i];
      --aa[i];
      --bb[i];
      --cc[i];
    }


    vector<int> ans(a+b+c, 2);
    for (int i = 0; i < N; ++i) {
      if (r[i] == 1)
	ans[aa[i]] = ans[bb[i]] = ans[cc[i]] = 1;
    }

    for (int i = 0; i < N; ++i) {
      if (r[i] == 0) {
	if (ans[aa[i]] == 1 && ans[bb[i]] == 1)
	  ans[cc[i]] = 0;
	else if (ans[aa[i]] == 1 && ans[cc[i]] == 1)
	  ans[bb[i]] = 0;
	else if (ans[bb[i]] == 1 && ans[cc[i]] == 1)
	  ans[aa[i]] = 0;
      }
    }

    for (int i = 0; i < a+b+c; ++i)
      cout << ans[i] << endl;
  }
  return 0;
}