
주사위만 잘 굴리면 된다...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[][] map;
static int[] dice;
static int N, M, x, y, K;
// 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4
static int[] dr = {0, 0, 0, -1, 1};
static int[] dc = {0, 1, -1, 0, 0};
static boolean flag = true;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new int[N][M];
dice = new int[6];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < K; i++) {
int cmd = Integer.parseInt(st.nextToken());
// System.out.println(cmd);
if (!roll(cmd)) {
continue;
} else {
if (map[x][y] == 0) {
map[x][y] = dice[5];
} else {
dice[5] = map[x][y];
map[x][y] = 0;
}
System.out.println(dice[0]);
}
}
}
static boolean roll(int dir) {
x += dr[dir];
y += dc[dir];
if (x < 0 || y < 0 || x >= N || y >= M) {
// System.out.println("!!");
x -= dr[dir];
y -= dc[dir];
return false;
}
int t, b, w, e, n, s;
t = dice[0];
n = dice[1];
w = dice[2];
e = dice[3];
s = dice[4];
b = dice[5];
// 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4
if (dir == 1) {
dice[0] = w;
dice[2] = b;
dice[3] = t;
dice[5] = e;
} else if (dir == 2) {
dice[0] = e;
dice[2] = t;
dice[3] = b;
dice[5] = w;
} else if (dir == 3) {
dice[0] = s;
dice[1] = t;
dice[4] = b;
dice[5] = n;
} else if (dir == 4) {
dice[0] = n;
dice[1] = b;
dice[4] = t;
dice[5] = s;
}
return true;
}
}