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;
namespace AOJ
{
    internal class Program
    {
        public static void Main(string[] args) { new Program().solve(); }
        void solve() {
            var D = new DiceClass();
            var inputs = Console.ReadLine().Split().Select(a => int.Parse(a)).ToArray();
            var orderNum = int.Parse(Console.ReadLine());
            D.SetDice(inputs);
            for (int i = 0; i < orderNum; ++i) {
                var af = Console.ReadLine().Split().Select(a => int.Parse(a)).ToArray();
                Console.WriteLine(D.Right(af[0], af[1]));
            }
        }
    }
    class DiceClass
    {
        //above figure is |0|
        //front figure is |1|
        //     N
        //    |0|
        //W |3|1|2|4| E
        //    |5|
        //     S
        public int[] Dice = new int[6];
        public int Above { get { return Dice[0]; } }
        public void SetDice(int[] a) {
            for (int i = 0; i < 6; ++i) Dice[i] = a[i];
        }
        public void SetBasicDice() {
            SetDice(Enumerable.Range(1, 6).ToArray());
        }
        private int[] genArr(params int[] a) {
            return new int[] { Dice[a[0]], Dice[a[1]], Dice[a[2]], Dice[a[3]], Dice[a[4]], Dice[a[5]] };
        }
        public void RollN() {
            SetDice(genArr(1, 5, 2, 3, 0, 4));
        }
        public void RollW() {
            SetDice(genArr(2, 1, 5, 0, 4, 3));
        }
        public void RollE() {
            SetDice(genArr(3, 1, 0, 5, 4, 2));
        }
        public void RollS() {
            SetDice(genArr(4, 0, 2, 3, 5, 1));
        }
        public void Show() {
            //     N
            //    |0|
            //W |3|1|2|4| E
            //    |5|       
            //     S
            Console.WriteLine("     N");
            Console.WriteLine("    |" + Dice[0] + "|");
            Console.WriteLine("W |" + Dice[3] + "|" + Dice[1] + "|" + Dice[2] + "|" + Dice[4] + "| E");
            Console.WriteLine("    |" + Dice[5] + "|");
            Console.WriteLine("     S");
        }
        public int Right(int above, int front) {
            Func<int, int, bool> f = (a, b) => Dice[a] == above && Dice[b] == front;
            if (f(1, 2) || f(2, 4) || f(4, 3) || f(3, 1)) return Dice[0];
            if (f(0, 3) || f(3, 5) || f(5, 2) || f(2, 0)) return Dice[1];
            if (f(0, 1) || f(1, 5) || f(5, 4) || f(4, 0)) return Dice[2];
            if (f(1, 0) || f(0, 4) || f(4, 5) || f(5, 1)) return Dice[3];
            if (f(0, 2) || f(2, 5) || f(5, 3) || f(3, 0)) return Dice[4];
            return Dice[5];            
        }




    }
}