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;

struct Dise
{
    public int D1 { set; get; }
    public int D2 { set; get; }
    public int D3 { set; get; }
    public int D4 { set; get; }
    public int D5 { set; get; }
    public int D6 { set; get; }
    public char[] Order { set; get; }
}


class Program
{
    static void Rotate(Dise[] dise , char order)
    {
        int a, b;
        switch (order)
        {
            case 'N':
                a = dise[0].D1;
                b = dise[0].D6;
                dise[0].D1 = dise[0].D2;
                dise[0].D2 = b;
                dise[0].D6 = dise[0].D5;
                dise[0].D5 = a;
                break;
            case 'E':
                a = dise[0].D1;
                b = dise[0].D6;
                dise[0].D6 = dise[0].D3;
                dise[0].D1 = dise[0].D4;
                dise[0].D3 = a;
                dise[0].D4 = b;
                break;
            case 'S':
                a = dise[0].D1;
                dise[0].D1 = dise[0].D5;
                dise[0].D5 = dise[0].D6;
                dise[0].D6 = dise[0].D2;
                dise[0].D2 = a;
                break;
            case 'W':
                a = dise[0].D4;
                b = dise[0].D3;
                dise[0].D4 = dise[0].D1;
                dise[0].D3 = dise[0].D6;
                dise[0].D6 = a;
                dise[0].D1 = b;
                break;
        }
    }
    
    static bool Confir(Dise[] dise, int front, List<int> result)
    {
        if (dise[0].D2 == front)
        {
            result.Add(dise[0].D3);
            return true;
        }
        else if (dise[0].D3 == front)
        {
            result.Add(dise[0].D5);
            return true;
        }
        else if (dise[0].D5 == front)
        {
            result.Add(dise[0].D4);
            return true;
        }
        else if (dise[0].D4 == front)
        {
            result.Add(dise[0].D2);
            return true;
        }
        else
        {
            return false;
        }
    }

    static void Main(string[] args)
    {       
        var dise = new Dise[1];   //サイコロの情報を持Aつ変数       

        string[] input = Console.ReadLine().Split(' ');  //サイコロの目

        dise[0].D1 = int.Parse(input[0]);
        dise[0].D2 = int.Parse(input[1]);
        dise[0].D3 = int.Parse(input[2]);
        dise[0].D4 = int.Parse(input[3]);
        dise[0].D5 = int.Parse(input[4]);
        dise[0].D6 = int.Parse(input[5]);

        int n = int.Parse(Console.ReadLine());
        var result = new List<int>();
        bool flag;

        for(int i = 0; i < n; i++)
        {
            flag = false;
            string[] str = Console.ReadLine().Split(' ');
            int above = int.Parse(str[0]);
            int front = int.Parse(str[1]);

            for(int j = 0; j < 3; j++)
            {
                if (dise[0].D1 == above)
                {
                    flag = Confir(dise, front, result);
                    break;
                }
                Rotate(dise, 'N');              
            }

            if (flag)
            {
                continue;
            }
            
            for(int j = 0; j < 3; j++)
            {
                if (dise[0].D1 == above)
                {
                    Confir(dise, front, result);
                    break;
                }
                Rotate(dise, 'W');                
            }           
        }

        foreach (var s in result)
        {
            Console.WriteLine(s);
        }
    }
}