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.Linq;
using System.Text;
//using System.Threading.Tasks;

namespace Dice_II
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Split(' ');
            Dice dice = new Dice(input);
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; ++i)
            {
                var work = Console.ReadLine().Split(' ');
                int a = int.Parse(work[0]);
                int b = int.Parse(work[1]);
                Console.WriteLine(dice.Right_Surface(a, b));
            }
        }
    }


    class Dice
    {
        private int[] surface;

        public Dice()
        {
            Console.WriteLine("???????????\????????????");
            Environment.Exit(-1);
        }
        public Dice(String[] str)
        {
            surface = new int[6];
            for (int i = 0; i < 6; ++i)
            {
                this.surface[i] = int.Parse(str[i]);
            }
        }// End_Constructor

        public int[] Get_Surface()
        {
            return this.surface;
        }
        public int Get_Surface(int n)
        {
            return this.surface[n];
        }

        public void Rotation(char c)
        {
            switch (c)
            {
                case 'E':
                    this.Rotation_E();
                    break;
                case 'N':
                    this.Rotation_N();
                    break;
                case 'S':
                    this.Rotation_S();
                    break;
                case 'W':
                    this.Rotation_W();
                    break;
                default:
                    Console.WriteLine("??\?????¨???????");
                    Environment.Exit(-1);
                    break;
            }
        }// End_Rotation

        private void Rotation_E()
        {
            this.Rotation_Run(0, 3, 5, 2);
        }

        private void Rotation_N()
        {
            this.Rotation_Run(0, 1, 5, 4);
        }

        private void Rotation_S()
        {
            this.Rotation_Run(0, 4, 5, 1);
        }
        private void Rotation_W()
        {
            this.Rotation_Run(0, 2, 5, 3);
        }

        static void Swap(ref int a, ref int b)
        {
            var t = a;
            a = b;
            b = t;
        }

        void Rotation_Run(int a, int b, int c, int d)
        {
            var work = this.surface[a];
            this.surface[a] = this.surface[b];
            this.surface[b] = this.surface[c];
            this.surface[c] = this.surface[d];
            this.surface[d] = work;
        }// End_Rotation_Run

        public int Right_Surface(int a, int b)
        {
            if (a == this.surface[0])
            {
                if (b == this.surface[1]) return this.surface[2];
                if (b == this.surface[2]) return this.surface[4];
                if (b == this.surface[3]) return this.surface[1];
                if (b == this.surface[4]) return this.surface[3];
            }   
            else if (a == this.surface[1])
            {
                if (b == this.surface[0]) return this.surface[3];
                if (b == this.surface[2]) return this.surface[0];
                if (b == this.surface[3]) return this.surface[5];
                if (b == this.surface[5]) return this.surface[2];
            }
            else if (a == this.surface[2])
            {
                if (b == this.surface[0]) return this.surface[1];
                if (b == this.surface[1]) return this.surface[5];
                if (b == this.surface[5]) return this.surface[4];
                if (b == this.surface[4]) return this.surface[0];
            }
            else if (a == this.surface[3])
            {
                if (b == this.surface[0]) return this.surface[4];
                if (b == this.surface[1]) return this.surface[0];
                if (b == this.surface[4]) return this.surface[5];
                if (b == this.surface[5]) return this.surface[1];
            }
            else if (a == this.surface[4])
            {
                if (b == this.surface[0]) return this.surface[2];
                if (b == this.surface[2]) return this.surface[5];
                if (b == this.surface[3]) return this.surface[0];
                if (b == this.surface[5]) return this.surface[3];
            }
            else if (a == this.surface[5])
            {
                if (b == this.surface[1]) return this.surface[3];
                if (b == this.surface[2]) return this.surface[1];
                if (b == this.surface[3]) return this.surface[4];
                if (b == this.surface[4]) return this.surface[2];
            }
            else
            {
                Console.WriteLine("???????????????");
                return -1;
            }
            return -1;
        }// End_Right_Surface
    }// End_CLass
}