import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Dice {
	public int[] value;
	public Dice(String[] info) {
		this.value = new int[6];
		for (int i = 0; i < value.length; i++) {
			this.value[i] = Integer.parseInt(info[i]);
		}
	}
}

public class Main {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Dice dice = new Dice(br.readLine().split(" "));

		int q = Integer.parseInt(br.readLine());
		for (int i = 0; i < q; i++) {
			int cnt = 0;
			String[] question = br.readLine().split(" ");
			int top = Integer.parseInt(question[0]);
			int surface = Integer.parseInt(question[1]);
			boolean isTop = checkTop(top, dice);
			boolean isSurface = checkSurface(surface, dice);
			while (!isTop || !isSurface) {
				if (isTop == true && isSurface == false) {
					// ?????¢??????????????¢????????¢???????????´???
					turn("S", dice);
					isSurface = checkSurface(surface, dice);
				} else if (isTop == false && isSurface == true) {
					// ?????¢??????????????¢????????¢???????????´???
					turn("V", dice);
					isTop = checkTop(top, dice);
				} else {
					// ?????¢?????¨??¢??¨??????????????´?????´???
					if (cnt < 4) {
						rolling("N", dice);
					} else {
						rolling("E", dice);
					}
				isTop = checkTop(top, dice);
				isSurface = checkSurface(surface, dice);
				cnt++;
				}
			}
			System.out.println(dice.value[2]);
		}
	}

	/**
	 * <pre>
	 * <b>rolling</b>
	 * ?¬¬1?????°??§?????????????????????????????????????????¢???????????????
	 * <br>
	 * ?????£?±\??´
	 * ==================================================
	 * </pre>
	 */
	private static void rolling(String direction, Dice dice) {
		int work = dice.value[0];
        if (direction.equals("N")) {
            dice.value[0] = dice.value[1];
            dice.value[1] = dice.value[5];
            dice.value[5] = dice.value[4];
            dice.value[4] = work;
        } else if (direction.equals("E")) {
            dice.value[0] = dice.value[3];
            dice.value[3] = dice.value[5];
            dice.value[5] = dice.value[2];
            dice.value[2] = work;
        }
	}
	/**
	 * <pre>
	 * <b>turn</b>
	 * ?¬¬1?????°??§?????????????????????????????????????????¢???????????????
	 * <br>
	 * ?????£?±\??´
	 * ==================================================
	 * </pre>
	 */
	private static void turn(String direction, Dice dice) {
		int work = 0;;
		if (direction.equals("S")) {
			// ?????????????¨??????¢
			work = dice.value[1];
			dice.value[1] = dice.value[3];
			dice.value[3] = dice.value[4];
			dice.value[4] = dice.value[2];
			dice.value[2] = work;
		} else {
			// ????????????????????¢
			work = dice.value[0];
			dice.value[0] = dice.value[2];
			dice.value[2] = dice.value[5];
			dice.value[5] = dice.value[3];
			dice.value[3] = work;
		}
	}

	/**
	 * <pre>
	 * <b>checkTop</b>
	 * ?¬¬1?????°??§??????????????°????????¢??¨?????????????????§??????????????????
	 * <br>
	 * ?????£?±\??´
	 * ==================================================
	 * </pre>
	 */
	private static boolean checkTop(int top, Dice dice) {
		if (top == dice.value[0]) {
			return true;
		}
		return false;
	}

	/**
	 * <pre>
	 * <b>checkSurface</b>
	 * ?¬¬1?????°??§??????????????°????????¢??¨?????????????????§??????????????????
	 * <br>
	 * ?????£?±\??´
	 * ==================================================
	 */
	private static boolean checkSurface(int surface, Dice dice) {
		if (surface == dice.value[1]) {
			return true;
		}
		return false;
	}
}

Measured memory_bytes: 24204