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.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Dice dice = new Dice(new int[]{sc.nextInt(), sc.nextInt(), sc.nextInt(),
                                       sc.nextInt(), sc.nextInt(), sc.nextInt()});

        int count = sc.nextInt();
        for (int i = 0; i < count; i++) {
            int[] a = new int[]{sc.nextInt(), sc.nextInt()};
            while (dice.eyes[dice.now[0]] != a[0]) {
                int idx = dice.find(a[0]);
                if (idx == 2 || idx == 4) {
                    dice.move('E');
                } else {
                    dice.move('N');
                }
            }
            while (dice.eyes[dice.now[3]] != a[1]) {
                dice.spin('R');
            }
            System.out.println(dice.eyes[dice.now[2]]);
        }
    }

    static class Dice {

        int[] eyes;
        int[/*TOP N E S W BOT*/] now;

        public Dice(int[] eyes) {
            this.eyes = eyes;
            this.now = new int[]{0, 4, 2, 1, 3, 5};
        }

        int find(int eye) {
            for (int i = 0; i < now.length; i++) {
                if (eyes[now[i]] == eye) {
                    return i;
                }
            }
            return -1; // never comes
        }

        void move(char way) {
            int tmp;
            switch (way) {
                case 'N':
                    tmp = now[0];
                    now[0] = now[3];
                    now[3] = now[5];
                    now[5] = now[1];
                    now[1] = tmp;
                    break;
                case 'E':
                    tmp = now[0];
                    now[0] = now[4];
                    now[4] = now[5];
                    now[5] = now[2];
                    now[2] = tmp;
                    break;
                case 'S':
                    tmp = now[0];
                    now[0] = now[1];
                    now[1] = now[5];
                    now[5] = now[3];
                    now[3] = tmp;
                    break;
                case 'W':
                    tmp = now[0];
                    now[0] = now[2];
                    now[2] = now[5];
                    now[5] = now[4];
                    now[4] = tmp;
                    break;
            }
        }

        void spin(char way) {
            int tmp;
            switch (way) {
                case 'R':
                    tmp = now[1];
                    now[1] = now[4];
                    now[4] = now[3];
                    now[3] = now[2];
                    now[2] = tmp;
                    break;
                case 'L':
                    tmp = now[1];
                    now[1] = now[2];
                    now[2] = now[3];
                    now[3] = now[4];
                    now[4] = tmp;
                    break;
            }
        }
    }
}