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;
using System.Linq;

namespace ITP1_11_B {
    class Dice {
        private int[] _dice;

        public int Top {
            get {
                return _dice[0];
            }
        }

        public Dice(int[] dice) {
            _dice = dice;
        }

        public int CheckRight(int top,int front) {
            SetTop(top);
            if (_dice[1] == front)
                return _dice[2];
            string dir = "RRR";
            foreach (var d in dir) {
                Move(d);
                if (_dice[1] == front)
                    return _dice[2];
            }
            return -1;
        }

        private void SetTop(int top) {
            if (_dice[0] == top)
                return;
            string dir = "NNNEEE";
            foreach (var d in dir) {
                Move(d);
                if (_dice[0] == top)
                    return;
            }
        }

        public void Move(string dir) {
            foreach (var d in dir) {
                Move(d);
            }
        }

        public void Move(char dir) {
            switch (dir) {
                case 'E':
                    Replace(0, 3, 5, 2);
                    break;
                case 'W':
                    Replace(0, 2, 5, 3);
                    break;
                case 'N':
                    Replace(0, 1, 5, 4);
                    break;
                case 'S':
                    Replace(0, 4, 5, 1);
                    break;
                case 'R':
                    Replace(1, 2, 4, 3);
                    break;
            }
        }

        private void Replace(int a, int b, int c, int d) {
            int temp = _dice[a];
            _dice[a] = _dice[b];
            _dice[b] = _dice[c];
            _dice[c] = _dice[d];
            _dice[d] = temp;
        }
    }

    class Program {
        static void Main(string[] args) {
            int[] nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int n = int.Parse(Console.ReadLine());
            Dice dice = new Dice(nums);
            for(int i = 0; i < n; i++) {
                string[] input = Console.ReadLine().Split(' ');
                int top = int.Parse(input[0]);
                int front = int.Parse(input[1]);
                Console.WriteLine(dice.CheckRight(top, front));
            }
        }
    }
}