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.


<image>


Constraints

* $0 \leq $ the integer assigned to a face $ \leq 100$
* The integers are all different
* $1 \leq q \leq 24$

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.

Example

Input

1 2 3 4 5 6
3
6 5
1 3
3 2


Output

3
5
6
#-*- coding:utf-8 -*-
#approach:
#????????????????????±??????N???S??????????????¨W???E??????????????¨????????????
rolls = raw_input().split()
n = input()
#?????????????£????
nsrolls = [rolls[0],rolls[1],rolls[5],rolls[4]]
werolls = [rolls[0],rolls[2],rolls[5],rolls[3]]
#?????????????£????
nsrolls2 = [rolls[1],rolls[2],rolls[4],rolls[3]]
werolls2 = [rolls[1],rolls[5],rolls[4],rolls[0]]
# print nsrolls
# print werolls
# print nsrolls2
# print werolls2
for x in xrange(n):
    top,front = raw_input().split()
    #??????????????????????????¨??????????????????????????????
    if top in nsrolls and front in nsrolls:
        topps = nsrolls.index(top)
        frontps = nsrolls.index(front)
        tmprolls = werolls
        listmode = 0
        # print "?????°????????????ns"
    elif top in werolls and front in werolls:
        topps = werolls.index(top)
        frontps = werolls.index(front)
        tmprolls = nsrolls
        listmode = 1
        # print "?????°????????????we"
    elif top in nsrolls2 and front in nsrolls2:
        topps = nsrolls2.index(top)
        frontps = nsrolls2.index(front)
        tmprolls = werolls2
        # print "?????°????????????ns2"
        listmode = 1
    elif top in werolls2 and front in werolls2:
        topps = werolls2.index(top)
        frontps = werolls2.index(front)
        tmprolls = nsrolls2
        listmode = 0
        # print "?????°????????????we2"

    #????????????????????????????????¨???????????£??????????????§??\????????¢?????´???????????§OK
    revflag = topps-frontps
    if listmode == 0:
        pass
        if revflag==3 or revflag==-1:
            print tmprolls[1]
        elif revflag==-3 or revflag==1:
            print tmprolls[3]
    else:
        if revflag==3 or revflag==-1:
            print tmprolls[3]
        elif revflag==-3 or revflag==1:
            print tmprolls[1]