Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6

Solution in JAVA (judge verdict: Accepted)

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);
				}
			}
			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;
        } else if (direction.equals("W")) {
            dice.value[0] = dice.value[2];
            dice.value[2] = dice.value[5];
            dice.value[5] = dice.value[3];
            dice.value[3] = work;
        } else {
            dice.value[0] = dice.value[4];
            dice.value[4] = dice.value[5];
            dice.value[5] = dice.value[1];
            dice.value[1] = 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;
	}
}