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.Collections.Generic;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {

        static void Main(string[] args)
        {
            //??????????´??????????
            List<int> resultlist = new List<int>();

            //???????????????????????????
            string[] dice = Console.ReadLine().Split(' ');

            var diceclass = new Dice(int.Parse(dice[0]), int.Parse(dice[1]), int.Parse(dice[2]), int.Parse(dice[3]), int.Parse(dice[4]), int.Parse(dice[5]));

            string questionnum = Console.ReadLine();

            int roopnum = int.Parse(questionnum);

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

                //?????¨??????????????°????????????????§???????????????????
                var upflowPlan = diceclass.GetFlow(int.Parse(question[0]));


                //?????¨????????´?????????????????¢????§?????????? 
                diceclass.MoveUp(upflowPlan);


                //?????¢???????????????????????????????????????????????????
                var sflowPlan = diceclass.GetFlow(int.Parse(question[1]));

                //??????????????????????????????Add??????
                switch (sflowPlan)
                {
                    case "S":
                        resultlist.Add(diceclass.E);
                        break;

                    case "E":
                        resultlist.Add(diceclass.N);
                        break;

                    case "N":
                        resultlist.Add(diceclass.W);
                        break;

                    case "W":
                        resultlist.Add(diceclass.S);
                        break;
                }

            }

            //????????????
            foreach (var result in resultlist)
            {
                Console.WriteLine(result);
            }

        }

    }

    class Dice
    {
        //??????????????°
        public int N = -1;
        public int Under = -1;
        public int E = -1;
        public int W = -1;
        public int Up = -1;
        public int S = -1;

        Dictionary<string, int> diceEye;


        #region ?????????????????????
        //????????????????????¨????????????????????????
        public Dice(int up, int s, int e, int w, int n, int under)
        {
            CreateDictionary(up, s, e, w, n, under);

            Up = up;
            S = s;
            E = e;
            W = w;
            N = n;
            Under = under;
        }
        #endregion



        public void MoveDice(string direction)
        {
            //????????¨????????°???????§??????????????????????
            this.Under = this.diceEye[direction];


            //????????¨???Under?????°???????????°???????§??????????
            //??¢?????????????§??????°??????????????????(S ??? N, W ??? E)
            switch (direction)
            {
                case "S":
                    this.N = this.diceEye["Under"];
                    this.S = this.diceEye["Up"];
                    this.Up = this.diceEye["N"];
                    break;


                case "E":
                    this.W = this.diceEye["Under"];
                    this.E = this.diceEye["Up"];
                    this.Up = this.diceEye["W"];
                    break;


                case "N":
                    this.S = this.diceEye["Under"];
                    this.N = this.diceEye["Up"];
                    this.Up = this.diceEye["S"];
                    break;


                case "W":
                    this.E = this.diceEye["Under"];
                    this.W = this.diceEye["Up"];
                    this.Up = this.diceEye["E"];
                    break;
            }

            //?????£????????§??????????????????
            CreateDictionary(this.Up, this.S, this.E, this.W, this.N, this.Under);
        }


        //?????£????????§?????????????????????
        public void CreateDictionary(int up, int s, int e, int w, int n, int under)
        {
            this.diceEye = new Dictionary<string, int>();

            this.diceEye.Add("Up", up);
            this.diceEye.Add("S", s);
            this.diceEye.Add("E", e);
            this.diceEye.Add("W", w);
            this.diceEye.Add("N", n);
            this.diceEye.Add("Under", under);

        }


        //???????????°???????????????????????????????????????
        public string GetFlow(int eye)
        {
            List<string> keylist = new List<string>(this.diceEye.Keys);

            string result = string.Empty;

            foreach (var key in keylist)
            {
                if (eye == diceEye[key])
                {
                    result = key;
                    break;
                }
            }

            return result;
        }

        //????????????????????¢????§?????????????????????????
        public void MoveUp(string upflow)
        {
            switch (upflow)
            {
                case "Under":
                    MoveDice("N");
                    MoveDice("N");
                    break;

                case "S":
                    MoveDice("N");
                    break;

                case "E":
                    MoveDice("W");
                    break;

                case "W":
                    MoveDice("E");
                    break;

                case "N":
                    MoveDice("S");
                    break;

                case "Up":
                    break;
            }
        }
    }


}