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 Python3 (judge verdict: Accepted)

class Dice:
    def __init__(self, top, front, right, left, rear, bottom):
        self.top = top
        self.front = front
        self.right = right
        self.left = left
        self.rear = rear
        self.bottom = bottom
    def print(self):
        '''print top of dice'''
        print(self.top)
    def north(self):
        self.top, self.front, self.bottom, self.rear = \
            self.front, self.bottom, self.rear, self.top
    def south(self):
        self.top, self.front, self.bottom, self.rear = \
            self.rear, self.top, self.front, self.bottom
    def east(self):
        self.top, self.right, self.bottom, self.left = \
            self.left, self.top, self.right, self.bottom
    def west(self):
        self.top, self.right, self.bottom, self.left = \
            self.right, self.bottom, self.left, self.top
    def find_right(self, top, front):
        if top == self.top:
            pass
        elif top == self.front:
            self.north()
        elif top == self.rear:
            self.south()
        elif top == self.right:
            self.west()
        elif top == self.left:
            self.east()
        elif top == self.bottom:
            self.north()
            self.north()
        else:
            return -1
        if front == self.front:
            return self.right
        elif front == self.right:
            return self.rear
        elif front == self.rear:
            return self.left
        elif front == self.left:
            return self.front
        else:
            return -1

d = list(map(int, input().split()))
dice = Dice(d[0], d[1], d[2], d[3], d[4], d[5])
q = int(input())

for i in range(q):
    top, front = map(int, input().split())
    right = dice.find_right(top, front)
    print(right)