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.Linq;

class Problem {
    class Dice {
        public int Top    { get; private set; }
        public int South  { get; private set; }
        public int East   { get; private set; }
        public int West   { get; private set; }
        public int North  { get; private set; }
        public int Bottom { get; private set; }
        public Dice(int[] nums) {
            Top    = nums[0];
            South  = nums[1];
            East   = nums[2];
            West   = nums[3];
            North  = nums[4];
            Bottom = nums[5];
        }
        public void Roll(string coms) {
            foreach (var com in coms) {
                var tmp = Top;
                switch (com) {
                    case 'E': Top = West;  West  = Bottom; Bottom = East;  East  = tmp; break;
                    case 'N': Top = South; South = Bottom; Bottom = North; North = tmp; break;
                    case 'S': Top = North; North = Bottom; Bottom = South; South = tmp; break;
                    case 'W': Top = East;  East  = Bottom; Bottom = West;  West  = tmp; break;
                }
            }
        }
        public void RollY(string coms) {
            foreach (var com in coms) {
                var tmp = South;
                switch (com) {
                    case 'R': South = West; West = North; North = East; East = tmp; break;
                    case 'L': South = East; East = North; North = West; West = tmp; break;
                }
            }
        }
        public char Position(int num) {
            return num == Top   ? 'T' :
                   num == South ? 'S' :
                   num == East  ? 'E' :
                   num == West  ? 'W' :
                   num == North ? 'N' : 'B';
        }
    }
    
    public static void Main() {
        var d = new Dice(Console.ReadLine().Split().Select(int.Parse).ToArray());
        var n = int.Parse(Console.ReadLine());
        for (var i = 0; i < n; i++) {
            var dat = Console.ReadLine().Split().Select(int.Parse).ToArray();
            switch (d.Position(dat[0])) {
                case 'T': break;
                case 'S': d.Roll("N"); break;
                case 'E': d.Roll("W"); break;
                case 'W': d.Roll("E"); break;
                case 'N': d.Roll("S"); break;
                case 'B': d.Roll("NN"); break;
            }
            switch (d.Position(dat[1])) {
                case 'S': break;
                case 'E': d.RollY("L"); break;
                case 'W': d.RollY("R"); break;
                case 'N': d.RollY("RR"); break;
            }
            Console.WriteLine(d.East);
        }
    }
}