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 C# (judge verdict: Accepted)

using System;

class Program
{
    class DICE {
        public int top = 0;
        public int right = 0;
        int left = 0;
        int front = 0;
        int back = 0;
        int bottom = 0;

        public DICE(string[] no) {
            top = int.Parse(no[0]);
            back = int.Parse(no[1]);
            right = int.Parse(no[2]);
            left = int.Parse(no[3]);
            front = int.Parse(no[4]);
            bottom = int.Parse(no[5]);
        }

        public void Move(string mv) {
            for (int i = 0; i < mv.Length; i++) {
                string ch = mv.Substring(i, 1);
                if (ch == "N") {
                    MOVE_N();
                }
                else if (ch == "E") {
                    MOVE_E();
                }
                else if (ch == "W") {
                    MOVE_W();
                }
                else if (ch == "S") {
                    MOVE_S();
                }
            }
        }

        public void Move(int setTop, int setBack) {
            if (setTop == right) { MOVE_W(); }
            if (setTop == left) { MOVE_E(); }
            if (setTop == front) { MOVE_S(); }
            if (setTop == back) { MOVE_N(); }
            if (setTop == bottom) { MOVE_N(); MOVE_N(); }

            if (setBack == right) { TURN_RIGHT(); }
            if (setBack == left) { TURN_LEFT(); }
            if (setBack == front) { TURN_RIGHT(); TURN_RIGHT(); }
        }

        private void TURN_RIGHT() {
            int wk = front;
            front = left;
            left = back;
            back = right;
            right = wk;
        }

        private void TURN_LEFT() {
            int wk = front;
            front = right;
            right = back;
            back = left;
            left = wk;
        }

        private void MOVE_N() {
            int wk = top;
            top = back;
            back = bottom;
            bottom = front;
            front = wk;
        }

        private void MOVE_E() {
            int wk = top;
            top = left;
            left = bottom;
            bottom = right;
            right = wk;
        }

        private void MOVE_W() {
            int wk = top;
            top = right;
            right = bottom;
            bottom = left;
            left = wk;
        }

        private void MOVE_S() {
            int wk = top;
            top = front;
            front = bottom;
            bottom = back;
            back = wk;
        }
    }

    static void Main(string[] args) {
        string[] x = Console.ReadLine().Split(' ');
        int q = int.Parse(Console.ReadLine());

        for (int i = 0; i < q; i++) {
            string[] z = Console.ReadLine().Split(' ');

            DICE d = new DICE(x);
            d.Move(int.Parse(z[0]), int.Parse(z[1]));
            Console.WriteLine(d.right);
        }
    }
}