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;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            //入力読取。
            string strDice = Console.ReadLine();
            string strDate = Console.ReadLine();
            int intDate = int.Parse(strDate);
            List<int> faceList = new List<int>();
            for(int date = 0; date < intDate; date++){
                string strOutCome = Console.ReadLine();
                
                //サイコロのクラスにて処理。
                Dice insD = new Dice(strDice, strOutCome);
                int intFace = insD.GetSide();
                faceList.Add(intFace);
            }

            //出力
            foreach(int face in faceList){
                Console.WriteLine(face);
            }
            
        }
        
    }
    
    class Dice{
        
        private string strDice;
        private string strOutCome;
        private int[] dice;
        private int intSide;
        
        public Dice(string strDice, string strOutCome){
            this.strDice = strDice;
            this.strOutCome = strOutCome;
            SetDice();
        }
        
        private void SetDice(){
        
            //サイコロ生成。
            string[] strDiceArray = strDice.Split(' ');
            this.dice = new int[6];
            for(int index = 0; index < 6; index++){
                dice[index] = int.Parse(strDiceArray[index]);
            }
            
            //指定されたサイコロの出目。
            string[] outComeArray = strOutCome.Split(' ');
            int intTop = int.Parse(outComeArray[0]);
            int intFront = int.Parse(outComeArray[1]);
            
            //サイコロを転がす。
            string strOrder = "NNNNWNNNWNNNENNNENNNWNNN";
            char[] chOrder = strOrder.ToCharArray();
            for(int order = 0; order < chOrder.Length; order++){
                Roll(chOrder[order]);
                if(dice[0] == intTop && dice[1] == intFront){
                    this.intSide = dice[2];
                    break;
                }
            }
            
        }
        
        private void Roll(char chDirection){
            if(chDirection == 'N'){
                int intTemp = dice[0];
                dice[0] = dice[1];
                dice[1] = dice[5];
                dice[5] = dice[4];
                dice[4] = intTemp;
            }else if(chDirection == 'W'){
                int intTemp = dice[0];
                dice[0] = dice[2];
                dice[2] = dice[5];
                dice[5] = dice[3];
                dice[3] = intTemp;
            }else if(chDirection == 'E'){
                int intTemp = dice[0];
                dice[0] = dice[3];
                dice[3] = dice[5];
                dice[5] = dice[2];
                dice[2] = intTemp;
            }else{
                int intTemp = dice[0];
                dice[0] = dice[4];
                dice[4] = dice[5];
                dice[5] = dice[1];
                dice[1] = intTemp;
            }
        }
        
        public int GetTop(){
            return dice[0];
        }
        
        public int GetSide(){
            return intSide;
        }
        
        
    }

}