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 Dice
    {
        private readonly int[,] adj = new int[6, 4] { { 1, 2, 4, 3 }, { 0, 3, 5, 2 }, { 0, 1, 5, 4 }, { 0, 4, 5, 1 }, { 0, 2, 5, 3 }, { 1, 3, 4, 2 } };
        private int top, front;
        private int[] vals = new int[6];
        public int Top { get { return vals[top]; } }
        public Dice(int[] values)
        {
            Array.Copy(values, vals, 6);
            top = 0;
            front = 1;
        }

        public void N()
        {
            int index = GetIndex(front, top);
            top = front;
            front = adj[front, (index + 2) % 4];
        }

        public void W()
        {
            int index = GetIndex(top, front);
            top = adj[top, (index + 1) % 4];
        }
        public void S()
        {
            int index = GetIndex(top, front);
            front = top;
            top = adj[top, (index + 2) % 4];
        }
        public void E()
        {
            int index = GetIndex(top, front);
            top = adj[top, (index + 3) % 4];
        }

        public int GetRight(int top, int front)
        {
            int topKey = GetKey(top);
            int frontKey = GetKey(front);
            return vals[adj[topKey, (GetIndex(topKey, frontKey) + 1) % 4]];
        }

        private int GetKey(int val)
        {
            for (int i = 0; i < 6; i++) if (vals[i] == val) return i;
            throw new Exception();
        }

        private int GetIndex(int top, int val)
        {
            for (int i = 0; i < 4; i++) if (adj[top, i] == val) return i;
            throw new Exception();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Dice dice = new Dice(Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse));
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
                Console.WriteLine(dice.GetRight(input[0], input[1]));
            }
        }
    }
}