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;

namespace Test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var labels = Array.ConvertAll(Console.ReadLine().Split(),int.Parse);
            var num = int.Parse(Console.ReadLine());
            var questions = new int[num][];
            for (int i = 0; i < questions.Length; i++)
            {
                questions[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            }

            var result=new int[questions.Length];
            var dice = new Dice(labels);
            for (int i = 0; i < questions.Length; i++)
            {
                var top = questions[i][0];
                if (top != dice.Top)
                {
                    if (top == dice.Front) dice.RollY(true);
                    else if (top == dice.Rear) dice.RollY(false);
                    else if (top == dice.Left) dice.RollZ(true);
                    else if (top == dice.Right) dice.RollZ(false);
                    else
                    {
                        dice.RollZ(true);
                        dice.RollZ(true);
                    }
                }
                var front = questions[i][1];
                if (front == dice.Front) result[i] = dice.Right;
                else
                {
                    if (front == dice.Right) result[i] = dice.Rear;
                    else if (front == dice.Left) result[i] = dice.Front;
                    else result[i] = dice.Left;
                }
            }

            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }

    class Dice
    {
        public int Front { get; private set; }
        public int Bottom { get; private set; }
        public int Right { get; private set; }
        public int Left { get; private set; }
        public int Top { get; private set; }
        public int Rear { get; private set; }

        public Dice(int[] label)
        {
            Front = label[0];
            Bottom = label[1];
            Right = label[2];
            Left = label[3];
            Top = label[4];
            Rear = label[5];
        }

        public void RollX(bool direction)
        {
            var temp = 0;
            if (direction)
            {
                temp = Front;
                Front = Left;
                Left = Rear;
                Rear = Right;
                Right = temp;
            }
            else
            {
                temp = Front;
                Front = Right;
                Right = Rear;
                Rear = Left;
                Left = temp;
            }
        }
        public void RollY(bool direction)
        {
            var temp = 0;
            if (direction)
            {
                temp = Front;
                Front = Bottom;
                Bottom = Rear;
                Rear = Top;
                Top = temp;
            }
            else
            {
                temp = Front;
                Front = Top;
                Top = Rear;
                Rear = Bottom;
                Bottom = temp;
            }
        }
        public void RollZ(bool direction)
        {
            var temp = 0;
            if (direction)
            {
                temp = Top;
                Top = Left;
                Left = Bottom;
                Bottom = Right;
                Right = temp;
            }
            else
            {
                temp = Top;
                Top = Right;
                Right = Bottom;
                Bottom = Left;
                Left = temp;
            }
        }
    }
}