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)

def right(a,b,a1,a2,a3,a4,a5,a6):
    b1=a6
    b2=a5
    b3=a4
    b4=a3
    b5=a2
    b6=a1
  
    if a>b:
        tmp_a=a
        a=b
        b=tmp_a
        
    if [a,b]==[a1,a2]:
        right_side=[a3,b3]
    elif [a,b]==[a1,a3]:
        right_side=[a5,b5]
    elif [a,b]==[a1,a4]:
        right_side=[a2,b2]
    elif [a,b]==[a1, a5]:
        right_side=[a4,b4]
    elif [a,b]==[a2, a3]:
        right_side=[a1,b1]
    elif [a, b]==[a2, a4]:
        right_side=[a6,b6]
    elif [a,b]==[a2,a6]:
        right_side=[a3,b3]
    elif [a,b]==[a3, a5]:
        right_side=[a1,b1]
    elif [a,b]==[a3, a6]:
        right_side=[a5,b5]
    elif [a, b]==[a4, a5]:
        right_side=[a6,b6]
    elif [a,b]==[a4,a6]:
        right_side=[a2,b2]
    elif [a,b]==[a5,a6]:
        right_side=[a4,b4]
  
    return right_side
    

initial=list( map(int,input().split()))
num_of_q=int(input())

for i in range(0, num_of_q):
    a=list(map(int, input().split()))
    flag=0
    if a[0]>a[1]:
        flag=1
    answer=right(*a,*initial)
    print(answer[flag])