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: Wrong Answer)

public class Main {
    public static void main(String[] args) {

        // ??????????????????????????????.
        java.util.Scanner sc = new java.util.Scanner(System.in);

        // ???????????????????????????.
        dice d1 = new dice();
        int[] line = new int[6];
        for (int i = 0; i < 6; i ++) { line[i] = sc.nextInt(); }
        d1.setNumber(line);

        // ?????¢??¨?????¢???????????¢?????????????????????.
        int q = sc.nextInt();
        int right = - 1;
        for (int i = 0; i < q; i ++) {
            right = d1.getRight(sc.nextInt(), sc.nextInt());
            System.out.println(right);
        }
    }
}

class dice {
    // ?????????????????¶???
    private int start[]  = new int[6];  // ?????????
    private int before[] = new int[6];  // ?????????
    private int after[]  = new int[6];  // ?????????

    // ??????????????????????????????
    String command;

    // ???????????????????????????????????????.
    public void setNumber(int[] line) {
        for (int i = 0; i < 6; i ++) { after[i] = before[i] = start[i] = line[i]; }
    }

    // ?????????????????????????????¶????????????.
    public void backStart() {
        for (int i = 0; i < 6; i ++) { after[i] = before[i] = start[i]; }
    }

    // ?????????????????????????????¨?????????.
    public void showNumber() {
        System.out.print("after: ");
        for (int i = 0; i < 6; i ++) {
            System.out.print(after[i] + " "); }
        System.out.println();
    }

    // ???????????????????¨????????????????.
    public void roleRight() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[0];
        after[1] = before[2];
        after[2] = before[4];
        after[3] = before[1];
        after[4] = before[3];
        after[5] = before[5];
    }

    // ?????????????????????????????????????????¢??????.
    public void toNorth() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[1];
        after[1] = before[5];
        after[2] = before[2];
        after[3] = before[3];
        after[4] = before[0];
        after[5] = before[4];
    }
    public void toSouth() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[4];
        after[1] = before[0];
        after[2] = before[2];
        after[3] = before[3];
        after[4] = before[5];
        after[5] = before[1];
    }
    public void toWest() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[2];
        after[1] = before[1];
        after[2] = before[5];
        after[3] = before[0];
        after[4] = before[4];
        after[5] = before[3];
    }
    public void toEast() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[3];
        after[1] = before[1];
        after[2] = before[0];
        after[3] = before[5];
        after[4] = before[4];
        after[5] = before[2];
    }

    // ?????¢??¨?????¢???????????¢???????????????.
    public int getRight(int top, int front) {

        // ?????????????????¶??? (????????????) ?????¨???????????????.
        // ???????????£????????¢??¨?????¢????????????????????´?????????????????????????????¢?????????.
        int right = - 1;
        for (int i = 0; i <= 5; i ++) {
            for (int j = 0; j <= 4; j ++) {
                switch (i) {
                case 0: { break; }
                case 1: { toNorth(); break; }
                case 2: { toSouth(); break; }
                case 3: { toWest(); break; }
                case 4: { toEast(); break; }
                case 5: { toNorth(); toNorth(); break; }
                }
                switch (j) {
                case 0: { break; }
                case 1: { roleRight(); break; }
                case 2: { roleRight(); roleRight(); break; }
                case 3: { roleRight(); roleRight(); roleRight(); break; }
                }
                if (top == after[0] && front == after[1]) { right = after[2]; }
            }
        }
        return right;
    }
}