/*
1732 주사위를 굴려봅시다.
*/
#include <iostream>
using namespace std;
#define SIZE 25
int map[SIZE][SIZE];
int N, M, startX, startY, K;
int command[1000];
int dice[7];
int dx[] = { 0,0,0,-1,1 };
int dy[] = { 0,1,-1,0,0 }; // 1234 동 서 북 남
int x, y, nx, ny;
void problemIn() {
cin >> N >> M >> startX >> startY >> K;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> map[i][j];
}
}
for (int i = 0; i < K; i++) {
cin >> command[i];
}
}
void dice_turn(int a) {
if (a == 1) {
int tmp = dice[4];
dice[4] = dice[1];
dice[1] = dice[3];
dice[3] = dice[6];
dice[6] = tmp;
}
else if (a == 2) {
int tmp = dice[4];
dice[4] = dice[6];
dice[6] = dice[3];
dice[3] = dice[1];
dice[1] = tmp;
}
else if (a == 3) {
int tmp = dice[1];
dice[1] = dice[5];
dice[5] = dice[6];
dice[6] = dice[2];
dice[2] = tmp;
}
else if (a == 4) {
int tmp = dice[1];
dice[1] = dice[2];
dice[2] = dice[6];
dice[6] = dice[5];
dice[5] = tmp;
}
}
void solve() {
x = startX;
y = startY;
for (int i = 0; i < K; i++) {
nx = x + dx[command[i]];
ny = y + dy[command[i]];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue; // 다음 이동이 바깥이면, 무시합니다.
dice_turn(command[i]);
if (map[nx][ny] == 0) { // 바닥면이 0 인 경우에는,
map[nx][ny] = dice[6]; // 바닥에 복사.
}
else { // 바닥면이 0이 아닌 경우에는,
dice[6] = map[nx][ny];
map[nx][ny] = 0;
}
x = nx;
y = ny;
cout << dice[1] << endl;
}
}
int main() {
problemIn();
solve();
return 0;
}