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 static System.Math;

class program{
    public static void Main(){
        var nums = Console.ReadLine().Split(' ').Select(_ => int.Parse(_)).ToList();
        var dice = new Dice(nums);
        var q = int.Parse(Console.ReadLine());
        for(int i=0; i<q; i++){
            var TS = Console.ReadLine().Split(' ').Select(_ => int.Parse(_)).ToList();
            Console.WriteLine(dice.getRightNum(TS[0],TS[1]));
        }
    }
}

class Dice{
    public List<Face> faces = new List<Face>(6);

    public Dice(List<int> nums){
        faces.Add(new Face(nums[0],'T'));
        faces.Add(new Face(nums[1],'S'));
        faces.Add(new Face(nums[2],'E'));
        faces.Add(new Face(nums[3],'W'));
        faces.Add(new Face(nums[4],'N'));
        faces.Add(new Face(nums[5],'B'));
    }

    public void Rotate(char dir){
        if(dir == 'N'){
            var tmpnum = getFace('T').num;
            getFace('T').num = getFace('S').num;
            getFace('S').num = getFace('B').num;
            getFace('B').num = getFace('N').num;
            getFace('N').num = tmpnum;
        }
        if(dir == 'W'){
            var tmpnum = getFace('T').num;
            getFace('T').num = getFace('E').num;
            getFace('E').num = getFace('B').num;
            getFace('B').num = getFace('W').num;
            getFace('W').num = tmpnum;
        }
        if(dir == 'S'){
            var tmpnum = getFace('T').num;
            getFace('T').num = getFace('N').num;
            getFace('N').num = getFace('B').num;
            getFace('B').num = getFace('S').num;
            getFace('S').num = tmpnum;
        }
        if(dir == 'E'){
            var tmpnum = getFace('T').num;
            getFace('T').num = getFace('W').num;
            getFace('W').num = getFace('B').num;
            getFace('B').num = getFace('E').num;
            getFace('E').num = tmpnum;
        }
    }

    public int getRightNum(int top, int south){
        var s = faces.First(_ => _.num == south);

        if(s.dir == 'N'){
            Rotate('N');
            Rotate('N');
        }
        if(s.dir == 'T'){
            Rotate('S');
        }
        if(s.dir == 'E'){
            Rotate('W');
            Rotate('S');
        }
        if(s.dir == 'W'){
            Rotate('E');
            Rotate('S');
        }
        if(s.dir == 'B'){
            Rotate('N');
        }

        var t = faces.First(_ => _.num == top);

        if(t.dir == 'W'){
            Rotate('E');
        }
        if(t.dir == 'B'){
            Rotate('E');
            Rotate('E');
        }
        if(t.dir == 'E'){
            Rotate('W');
        }

        return getFace('E').num;
    }

    public Face getFace(char d){
        return faces.First(_ => _.dir == d);
    }
}

class Face{
    public int num;
    public char dir;

    public Face(int n, char d){
        num = n;
        dir = d;
    }
}