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)

d=list(map(int,input().split()))
q=int(input())

def Check(Dice,P):
    if P == [3,1] or P ==[4,3] or P ==[2,4] or P ==[1,2]:
        a=0
    elif P == [2,0] or P ==[5,2] or P ==[3,5] or P ==[0,3]:
        a=1
    elif P == [0,1] or P ==[1,5] or P ==[5,4] or P ==[4,0]:
        a=2
    elif P == [1,0] or P ==[5,1] or P ==[4,5] or P ==[0,4]:
        a=3
    elif P == [0,2] or P ==[2,5] or P ==[5,3] or P ==[3,0]:
        a=4
    elif P == [1,3] or P ==[3,4] or P ==[4,2] or P ==[2,1]:
        a=5
    return a

for i in range(q):
    l1=list(map(int,input().split()))
    l2=[d.index(l1[0]),d.index(l1[1])]
    print(d[Check(d,l2)])