const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
const length = Number(await readline());
const arr = (await readline()).split(" ").map(Number);
const [len, tate] = (await readline()).split(" ").map(Number);
const matrix = [];
for (let i = 0; i < len; i++) {
matrix.push((await readline()).split(" ").map(Number));
}
const visited = new Array(len).fill(0).map(() => new Array(tate).fill(false));
const offsets = [
[-1, 0],
[0, -1],
[0, 1],
[1, 0],
];
const dfs = (x, y, i, path) => {
if (i == length) return true;
for (const [offsetX, offsetY] of offsets) {
const newX = x + offsetX;
const newY = y + offsetY;
if (
newX >= 0 &&
newX < len &&
newY >= 0 &&
newY < tate &&
matrix[newX][newY] == arr[i] &&
!visited[newX][newY]
) {
visited[newX][newY] = true;
path.push(`${newX} ${newY}`);
if (dfs(newX, newY, i + 1, path)) return true;
visited[newX][newY] = false;
path.pop();
}
}
return false;
};
const path = [];
for (let i = 0; i < len; i++) {
for (let j = 0; j < tate; j++) {
if (matrix[i][j] == arr[0]) {
visited[i][j] = true;
path.push(`${i} ${j}`);
if (dfs(i, j, 1, path)) console.log(path.join(" "));
visited[i][j] = false;
path.pop();
}
}
}
if (!path.length) console.log("error");
})();