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 ITP1_11_B
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] dice = getdice();
            int q = int.Parse(Console.ReadLine());

            for (int i=0; i<q; i++)
            {
                string[] numbers = Console.ReadLine().Split(' ');
                int num1 = int.Parse(numbers[0]);
                int num2 = int.Parse(numbers[1]);

                int right = checkdice(dice, num1, num2);
                Console.WriteLine(right);
            }

        }

        static int checkdice(int[] a, int b, int c)
        {
            int right = 0;

            if(a[1] != b)
            {
                bool top_eq = false;
                bool all_noteq = false;
                int count = 0;
                while (top_eq == false && all_noteq == false)
                {
                    int tmp = a[1];
                    a[1] = a[5];
                    a[5] = a[6];
                    a[6] = a[2];
                    a[2] = tmp;

                    count++;

                    if (a[1] == b)
                    {
                        top_eq = true;
                    }
                    else if (count == 4)
                    {
                        all_noteq = true;
                    }
                }

                if (top_eq == false && all_noteq == true)
                {
                    while (top_eq == false)
                    {
                        int tmp2 = a[1];
                        a[1] = a[4];
                        a[4] = a[6];
                        a[6] = a[3];
                        a[3] = tmp2;

                        if (a[1] == b) top_eq = true;
                    }
                }

            }

            if (a[2] != c)
            {
                right = turndice(a, c);
            }
            else
            {
                right = a[3];
            }
            
            return right;
        }


        static int turndice(int[] d, int e)
        {
            bool forward_eq = false;
            while (forward_eq == false)
            {
                int tmp = d[2];
                d[2] = d[4];
                d[4] = d[5];
                d[5] = d[3];
                d[3] = tmp;

                if (d[2] == e) forward_eq = true;
            }

            return d[3];
        }


        static int[] getdice()
        {
            int[] dice = new int[7];


            string[] input = Console.ReadLine().Split(' ');
            for (int i=1; i<=6; i++)
            {
                dice[i] = int.Parse(input[i-1]);
            }

            return dice;
        }
    }
}