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;
namespace _001_FindingMissingCards
{
    class Dice
    {
        private int[] num = new int[7];
        private int[] reverse = new int[100];

        public Dice(int[]given)
        {
            num[1] = given[0];
            num[2] = given[1];
            num[3] = given[2];
            num[4] = given[3];
            num[5] = given[4];
            num[6] = given[5];

            reverse[given[0]] = 1;
            reverse[given[1]] = 2;
            reverse[given[2]] = 3;
            reverse[given[3]] = 4;
            reverse[given[4]] = 5;
            reverse[given[5]] = 6;
        }

        private int rightNum(int numa, int numb)
        {
            int x = 0;

            if ((numa == 2 && numb == 3) ||
                (numa == 3 && numb == 5) ||
                (numa == 4 && numb == 2) ||
                (numa == 5 && numb == 4))
            {
                x = 1;
            }
            else if (
                (numa == 1 && numb == 4) ||
                (numa == 3 && numb == 1) ||
                (numa == 4 && numb == 6) ||
                (numa == 6 && numb == 3))
            {
                x = 2;
            }
            else if (
                (numa == 1 && numb == 2) ||
                (numa == 2 && numb == 6) ||
                (numa == 5 && numb == 1) ||
                (numa == 6 && numb == 5))
            {
                x = 3;
            }
            else if (
                (numa == 1 && numb == 5) ||
                (numa == 2 && numb == 1) ||
                (numa == 5 && numb == 6) ||
                (numa == 6 && numb == 2))
            {
                x = 4;
            }
            else if (
                (numa == 1 && numb == 3) ||
                (numa == 3 && numb == 6) ||
                (numa == 4 && numb == 1) ||
                (numa == 6 && numb == 4))
            {
                x = 5;
            }
            else if (
                (numa == 2 && numb == 4) ||
                (numa == 3 && numb == 2) ||
                (numa == 4 && numb == 5) ||
                (numa == 5 && numb == 3))
            {
                x = 6;
            }

            return x;
        }


        public int setTopFront(int a, int b)
        {

            return num[rightNum(reverse[a],reverse[b])];
        }



    }

    class Program
    {
        static void Main()
        {
            var tmp1 = Console.ReadLine().Split(' ');
            var sample1 = new int[tmp1.Length];
            for (int i = 0; i < tmp1.Length; i++)
            {
                sample1[i] = Convert.ToInt32(tmp1[i]);
            }
            Dice a = new Dice(sample1);

            var numOfComm = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < numOfComm; i++)
            {
                var commands = Console.ReadLine().Split(' ');
                Console.WriteLine(a.setTopFront(Convert.ToInt32(commands[0]),Convert.ToInt32(commands[1])));
            }

        }
    }
}