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

enum direction { north, east, west, south }
class dice{
    public int number;
    public int me;
    public dice[] east;
    public dice[] north;
    public dice[] south;
    public dice[] west;
    public int[,] direction;
};


class Program
 {
    static void Main(string[] args)
    {
        string nyuryoku;
        dice[] face = new dice[7];
        string [] temp = new string[6];
        for (int i = 1; i < 7; i++)
        {
            face[i] = new dice();
            face[i].east = new dice[4];
            face[i].west = new dice[4];
            face[i].north = new dice[4];
            face[i].south = new dice[4];
            face[i].direction = new int[4,4];
            face[i].me = i;
        }

        face[1].east[0] = face[4];
        face[1].direction[0, 1] = 1;
        face[1].north[0] = face[2];
        face[1].direction[0, 0] = 0;
        face[1].west[0] = face[3];
        face[1].direction[0, 3] = 3;
        face[1].south[0] = face[5];
        face[1].direction[0, 2] = 2;

        face[1].east[1] = face[2];
        face[1].direction[1, 1] = 1;
        face[1].north[1] = face[3];
        face[1].direction[1, 0] = 0;
        face[1].west[1] = face[5];
        face[1].direction[1, 3] = 3;
        face[1].south[1] = face[4];
        face[1].direction[1, 2] = 2;

        face[1].east[2] = face[3];
        face[1].direction[2, 1] = 1;
        face[1].north[2] = face[5];
        face[1].direction[2, 0] = 0;
        face[1].west[2] = face[4];
        face[1].direction[2, 3] = 3;
        face[1].south[2] = face[2];
        face[1].direction[2, 2] = 2;

        face[1].east[3] = face[5];
        face[1].direction[3, 1] = 1;
        face[1].north[3] = face[4];
        face[1].direction[3, 0] = 0;
        face[1].west[3] = face[2];
        face[1].direction[3, 3] = 3;
        face[1].south[3] = face[3];
        face[1].direction[3, 2] = 2;
//--------------------------------------
        face[2].east[0] = face[4];
        face[2].direction[0, 1] = 0;
        face[2].north[0] = face[6];
        face[2].direction[0, 0] = 0;
        face[2].west[0] = face[3];
        face[2].direction[0, 3] = 0;
        face[2].south[0] = face[1];
        face[2].direction[0, 2] = 0;

        face[2].east[1] = face[6];
        face[2].direction[1, 1] = 1;
        face[2].north[1] = face[3];
        face[2].direction[1, 0] = 1;
        face[2].west[1] = face[1];
        face[2].direction[1, 3] = 1;
        face[2].south[1] = face[4];
        face[2].direction[1, 2] = 1;

        face[2].east[2] = face[3];
        face[2].direction[2, 1] = 2;
        face[2].north[2] = face[1];
        face[2].direction[2, 0] = 2;
        face[2].west[2] = face[4];
        face[2].direction[2, 3] = 2;
        face[2].south[2] = face[6];
        face[2].direction[2, 2] = 2;

        face[2].east[3] = face[1];
        face[2].direction[3, 1] = 3;
        face[2].north[3] = face[4];
        face[2].direction[3, 0] = 3;
        face[2].west[3] = face[6];
        face[2].direction[3, 3] = 3;
        face[2].south[3] = face[3];
        face[2].direction[3, 2] = 3;
//--------------------------------------
        face[3].east[0] = face[2];
        face[3].direction[0, 1] = 0;
        face[3].north[0] = face[6];
        face[3].direction[0, 0] = 3;
        face[3].west[0] = face[5];
        face[3].direction[0, 3] = 0;
        face[3].south[0] = face[1];
        face[3].direction[0, 2] = 1;

        face[3].east[1] = face[6];
        face[3].direction[1, 1] = 0;
        face[3].north[1] = face[5];
        face[3].direction[1, 0] = 1;
        face[3].west[1] = face[1];
        face[3].direction[1, 3] = 2;
        face[3].south[1] = face[2];
        face[3].direction[1, 2] = 1;

        face[3].east[2] = face[5];
        face[3].direction[2, 1] = 2;
        face[3].north[2] = face[1];
        face[3].direction[2, 0] = 3;
        face[3].west[2] = face[2];
        face[3].direction[2, 3] = 2;
        face[3].south[2] = face[6];
        face[3].direction[2, 2] = 1;

        face[3].east[3] = face[1];
        face[3].direction[3, 1] = 0;
        face[3].north[3] = face[2];
        face[3].direction[3, 0] = 3;
        face[3].west[3] = face[6];
        face[3].direction[3, 3] = 2;
        face[3].south[3] = face[5];
        face[3].direction[3, 2] = 3;
//-------------------------------------
        face[4].east[0] = face[5];
        face[4].direction[0, 1] = 0;
        face[4].north[0] = face[6];
        face[4].direction[0, 0] = 1;
        face[4].west[0] = face[2];
        face[4].direction[0, 3] = 0;
        face[4].south[0] = face[1];
        face[4].direction[0, 2] = 3;

        face[4].east[1] = face[6];
        face[4].direction[1, 1] = 2;
        face[4].north[1] = face[2];
        face[4].direction[1, 0] = 1;
        face[4].west[1] = face[1];
        face[4].direction[1, 3] = 0;
        face[4].south[1] = face[5];
        face[4].direction[1, 2] = 1;

        face[4].east[2] = face[2];
        face[4].direction[2, 1] = 2;
        face[4].north[2] = face[1];
        face[4].direction[2, 0] = 1;
        face[4].west[2] = face[5];
        face[4].direction[2, 3] = 2;
        face[4].south[2] = face[6];
        face[4].direction[2, 2] = 3;

        face[4].east[3] = face[1];
        face[4].direction[3, 1] = 2;
        face[4].north[3] = face[5];
        face[4].direction[3, 0] = 3;
        face[4].west[3] = face[6];
        face[4].direction[3, 3] = 0;
        face[4].south[3] = face[2];
        face[4].direction[3, 2] = 3;
//------------------------------------
        face[5].east[0] = face[3];
        face[5].direction[0, 1] = 0;
        face[5].north[0] = face[6];
        face[5].direction[0, 0] = 2;
        face[5].west[0] = face[4];
        face[5].direction[0, 3] = 0;
        face[5].south[0] = face[1];
        face[5].direction[0, 2] = 2;

        face[5].east[1] = face[6];
        face[5].direction[1, 1] = 3;
        face[5].north[1] = face[4];
        face[5].direction[1, 0] = 1;
        face[5].west[1] = face[1];
        face[5].direction[1, 3] = 3;
        face[5].south[1] = face[3];
        face[5].direction[1, 2] = 1;

        face[5].east[2] = face[4];
        face[5].direction[2, 1] = 2;
        face[5].north[2] = face[1];
        face[5].direction[2, 0] = 0;
        face[5].west[2] = face[3];
        face[5].direction[2, 3] = 2;
        face[5].south[2] = face[6];
        face[5].direction[2, 2] = 0;

        face[5].east[3] = face[1];
        face[5].direction[3, 1] = 1;
        face[5].north[3] = face[3];
        face[5].direction[3, 0] = 3;
        face[5].west[3] = face[6];
        face[5].direction[3, 3] = 1;
        face[5].south[3] = face[4];
        face[5].direction[3, 2] = 3;
//-----------------------------------
        face[6].east[0] = face[4];
        face[6].direction[0, 1] = 3;
        face[6].north[0] = face[5];
        face[6].direction[0, 0] = 2;
        face[6].west[0] = face[3];
        face[6].direction[0, 3] = 1;
        face[6].south[0] = face[2];
        face[6].direction[0, 2] = 0;

        face[6].east[1] = face[5];
        face[6].direction[1, 1] = 3;
        face[6].north[1] = face[3];
        face[6].direction[1, 0] = 2;
        face[6].west[1] = face[2];
        face[6].direction[1, 3] = 1;
        face[6].south[1] = face[4];
        face[6].direction[1, 2] = 0;

        face[6].east[2] = face[3];
        face[6].direction[2, 1] = 3;
        face[6].north[2] = face[2];
        face[6].direction[2, 0] = 2;
        face[6].west[2] = face[4];
        face[6].direction[2, 3] = 1;
        face[6].south[2] = face[5];
        face[6].direction[2, 2] = 0;

        face[6].east[3] = face[2];
        face[6].direction[3, 1] = 3;
        face[6].north[3] = face[4];
        face[6].direction[3, 0] = 2;
        face[6].west[3] = face[5];
        face[6].direction[3, 3] = 1;
        face[6].south[3] = face[3];
        face[6].direction[3, 2] = 0;

        dice inital = face[1];


        nyuryoku = Console.ReadLine();
        temp = nyuryoku.Split(' ');
        for (int i = 1; i < 7; i++)
            face[i].number = int.Parse(temp[i-1]);
     
        nyuryoku = Console.ReadLine();
        int number, front, top ;
        string[] input = new string[2];
        var result = new List<int>();
 
        number = int.Parse(nyuryoku);

        for (int i = 0; i < number; i++)
        {
            nyuryoku = Console.ReadLine();
            input = nyuryoku.Split(' ');
            top = int.Parse(input[0]);
            front = int.Parse(input[1]);
            for(int j = 1; j < 7 ;j++)
                if (face[j].number == top)
                {
                    top = face[j].me;
                    break;
                }
            for (int j = 1; j < 7; j++)
                if (face[j].number == front)
                {
                    front = face[j].me;
                    break;
                }




            if (face[top].north[0].me == front)
            {
                result.Add(face[top].west[0].number);
            }
            else if (face[top].north[1].me == front)
            {
                result.Add(face[top].west[1].number);
            }
            else if (face[top].north[2].me == front)
            {
                result.Add(face[top].west[2].number);
            }
            else if (face[top].north[3].me == front)
            {
                result.Add(face[top].west[3].number);
            }

        }
        for (int i=0; i < result.Count; i++)
            Console.WriteLine("{0}",result[i]);
 

    }
 }