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;
//using System.Collections.Generic;

class Test
{
    static void Main()
    {
        Dice dice1 = new Dice(Console.ReadLine());
        int q = int.Parse(Console.ReadLine());

        for (int i = 0; i < q; i++)
        {
            string[] f = Console.ReadLine().Split();
            string f1 = f[0];
            string f2 = f[1];

            if (f1 == dice1.d2) dice1.North();
            else if (f1 == dice1.d3) dice1.West();
            else if (f1 == dice1.d4) dice1.East();
            else if (f1 == dice1.d5) dice1.South();
            else if (f1 == dice1.d6) { dice1.South(); dice1.South(); }
        
            while (dice1.d2 != f2)
            {
                dice1.Spin();
            }

            Console.WriteLine(dice1.d3);
        }
    }
}

class Dice
{
    string[] ss ;
    public string d1, d2, d3, d4, d5, d6;

    public Dice(string s)
    {
        this.ss = s.Split();
        d1 = ss[0];
        d2 = ss[1];
        d3 = ss[2];
        d4 = ss[3];
        d5 = ss[4];
        d6 = ss[5];
    }

    string tmp;

    public void North()
    {
        tmp = d1;
        d1 = d2;
        d2 = d6;
        d6 = d5;
        d5 = tmp;
    }

    public void East()
    {
        tmp = d1;
        d1 = d4;
        d4 = d6;
        d6 = d3;
        d3 = tmp;
    }

    public void West()
    {
        tmp = d1;
        d1 = d3;
        d3 = d6;
        d6 = d4;
        d4 = tmp;
    }

    public void South()
    {
        tmp = d1;
        d1 = d5;
        d5 = d6;
        d6 = d2;
        d2 = tmp;
    }

    public void Spin()
    {
        tmp = d2;
        d2 = d4;
        d4 = d5;
        d5 = d3;
        d3 = tmp;
    }
}