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 aoj11b
{
    class Program
    {
        static void Main(string[] args)
        {
            DICE dice = makeDice();                     //DICE??????????????????????????????dice?????????
            int n = int.Parse(Console.ReadLine());      //???????????????????????°
            
            for(int i = 0; i < n; i++)
            {
                string[] top_and_front = Console.ReadLine().Split(' ');
                int top = int.Parse(top_and_front[0]);
                int front = int.Parse(top_and_front[1]);

                /*??????N???????????¢??§TOP??¨top???????????????????????????*/
                for (int j = 1; j <= 4; j++)
                {
                    if( top == dice.TOP)
                    {
                        break;
                    }
                    dice.rotateN();     //?????´??????????????°N?????¢??????????????´???
                }
                /*??????????????§???top???TOP?????\?????????????????????????????????????????????????????£?????????*/
                if( top != dice.TOP)//???????????£??????????????´???
                {
                    dice.rotateE();     //E?????¢??????
                }
                if (top != dice.TOP)//?????????????????£??????????????´????¬????????????§?????´??????????????°??¨??????
                {
                    dice.rotateE();
                    dice.rotateE();
                }
                if (top != dice.TOP)
                {
                    Console.WriteLine("error");
                    return;
                }
                /*top???TOP????????£??????????????§front???FRONT??¨?????´????????°?????????*/
                for (int j = 1; j <= 4; j++)
                {
                    if (front == dice.FRONT)
                    {
                        break;
                    }
                    dice.rotateCW();     //?????´??????????????°CW?????¢??????????????´???
                }

                /*????????¨???*/
                Console.WriteLine(dice.RIGHT);
            }
            


 
        }

        private static DICE makeDice()
        {
            int[] label = new int[6];
            string[] line = Console.ReadLine().Split(' ');
            if (line.Length != 6)    //line????????????6??§???????????°??¨??????
            {
                throw new NotImplementedException();
            }
            for (int i = 0; i < 6; i++)
            {
                label[i] = int.Parse(line[i]);
            }
            DICE newdice = new DICE(label);
            return newdice;
        }


    }

    internal class DICE
    {
        #region ?????£?????????
        public int TOP { get; set; }
        public int FRONT { get; set; }
        public int RIGHT { get; set; }
        public int LEFT { get; set; }
        public int BACK { get; set; }
        public int BOTTOM { get; set; }
        #endregion

        public DICE(int[] label)

        {
            TOP = label[0];
            FRONT = label[1];
            RIGHT = label[2];
            LEFT = label[3];
            BACK = label[4];
            BOTTOM = label[5];
        }

        public DICE()//?????°???????????´???

        {
            TOP = 1;
            FRONT = 2;
            RIGHT = 3;
            LEFT = 4;
            BACK = 5;
            BOTTOM = 6;
        }


        internal void show()
        {
            Console.WriteLine(TOP + " " + FRONT + " " + RIGHT + " " + LEFT + " " + BACK + " " + BOTTOM);
        }

        internal void rotateN()//N???????????¢
        {
            int tmp = TOP;
            TOP = FRONT;
            FRONT = BOTTOM;
            BOTTOM = BACK;
            BACK = tmp;
        }
        internal void rotateS()//S???????????¢
        {
            rotateN();
            rotateN();
            rotateN();
        }
        internal void rotateE()//E???????????¢
        {
            int tmp = TOP;
            TOP = LEFT;
            LEFT = BOTTOM;
            BOTTOM = RIGHT;
            RIGHT = tmp;
        }
        internal void rotateW()//W???????????¢
        {
            rotateE();
            rotateE();
            rotateE();
        }
        internal void rotateCW()//TOP-BOTTOM??????????¨???????
        {
            int tmp = FRONT;
            FRONT = RIGHT;
            RIGHT = BACK;
            BACK = LEFT;
            LEFT = tmp;
        }
    }
}