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.IO;
using System.Linq;
using System.Text;

namespace _20170409_AOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArr = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            int q = int.Parse(Console.ReadLine());
            
            Dice dice = new Dice(iArr);
            for (int i = 0; i < q; i++)
            {
                int[] fArr = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
                dice.SetTopFront(fArr[0], fArr[1]);
                Console.WriteLine(dice.Right);
            }

            Console.ReadLine();
        }
    }

    class Dice
    {
        public enum Dir
        {
            N,
            S,
            W,
            E,
            L,
            R
        }


        int[] iArr = new int[6];
        int[,] tenkai;
        public Dice(int[] arr)
        {
            int len = Math.Min(iArr.Length, arr.Length);
            for (int i = 0; i < len; i++)
            {
                iArr[i] = arr[i];
            }

            tenkai = CreateTenkai();
        }

        public int Top { get { return iArr[tenkai[1, 1]]; } }
        public int Bottom { get { return iArr[tenkai[1, 3]]; } }
        public int Front { get { return iArr[tenkai[2, 1]]; } }
        public int Back { get { return iArr[tenkai[0, 1]]; } }
        public int Left { get { return iArr[tenkai[1, 0]]; } }
        public int Right { get { return iArr[tenkai[1, 2]]; } }

        public void SetTopFront(int _top, int _front)
        {
            for (int i = 0; i < 3; i++)
            {
                if (_top == Top)
                {
                    break;
                }
                Move(Dir.N);
                if (_top == Top)
                {
                    break;
                }
                Move(Dir.W);
            }

            for (int i = 0; i < 3; i++)
            {
                if (_front == Front)
                {
                    break;
                }
                Move(Dir.L);
            }
        }

        public void Move(char c)
        {
            switch (c)
            {
                case 'N': Move(Dir.N); break;
                case 'S': Move(Dir.S); break;
                case 'W': Move(Dir.W); break;
                case 'E': Move(Dir.E); break;
                case 'L': Move(Dir.L); break;
                case 'R': Move(Dir.R); break;
            }
        }

        public void Move(Dir dir)
        {
            switch(dir)
            {
                case Dir.N:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[2, 1];
                        tenkai[2, 1] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[0, 1];
                        tenkai[0, 1] = tmp;
                    }
                    break;
                case Dir.S:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[0, 1];
                        tenkai[0, 1] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[2, 1];
                        tenkai[2, 1] = tmp;
                    }
                    break;
                case Dir.W:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[1, 2];
                        tenkai[1, 2] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[1, 0];
                        tenkai[1, 0] = tmp;
                    }
                    break;
                case Dir.E:
                    {
                        int tmp = tenkai[1, 1];
                        tenkai[1, 1] = tenkai[1, 0];
                        tenkai[1, 0] = tenkai[1, 3];
                        tenkai[1, 3] = tenkai[1, 2];
                        tenkai[1, 2] = tmp;
                    }
                    break;
                case Dir.L:
                    {
                        int tmp = tenkai[2, 1];
                        tenkai[2, 1] = tenkai[1, 2];
                        tenkai[1, 2] = tenkai[0, 1];
                        tenkai[0, 1] = tenkai[1, 0];
                        tenkai[1, 0] = tmp;
                    }
                    break;
                case Dir.R:
                    {
                        int tmp = tenkai[2, 1];
                        tenkai[2, 1] = tenkai[1, 0];
                        tenkai[1, 0] = tenkai[0, 1];
                        tenkai[0, 1] = tenkai[1, 2];
                        tenkai[1, 0] = tmp;
                    }
                    break;
            }
        }

        private int[,] CreateTenkai()
        {
            int[,] arr = new int[,]{
                { -1,  4, -1, -1 },
                {  3,  0,  2,  5 },
                { -1,  1, -1, -1 },
            };

            return arr;
        }
    }
}