Dice II: Construct a dice from a given sequence of integers as in Dice I. Given integers on the top and front faces after rolling, write a program to print the integer on the right side face. Input includes six integers in ascending order, followed by the number of questions. Each question consists of two integers representing the top and front faces. Output the integer on the right side face for each question. Constraints: Integers assigned to a face range from 0 to 100, all integers are different, and there are 1 to 24 questions. Example input: "1 2 3 4 5 6" and 3, followed by questions "6 5", "1 3", and "3 2". Example output: "3", "5", "6".

# Buggy version:
import random


class Cube:
    def __init__(self, u, s, e, w, n, d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d

    def rotate(self, dic):
        if dic == "N":
            tmp = self.u
            self.u = self.s
            self.s = self.d
            self.d = self.n
            self.n = tmp
        elif dic == "E":
            tmp = self.u
            self.u = self.w
            self.w = self.d
            self.d = self.e
            self.e = tmp
        elif dic == "W":
            tmp = self.u
            self.u = self.e
            self.e = self.d
            self.d = self.w
            self.w = tmp
        else:
            tmp = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = tmp


def main():
    u, s, e, w, n, d = map(int, input().split())
    cube = Cube(u, s, e, w, n, d)
    q = int(input())
    for i in range(q):
        upper, front = map(int, input().split())
        while True:
            cube.rotate(random.choice("NWES"))
            if upper == cube.u and front == cube.s:
                print(cube.e)
                break


if __name__ == '__main__':
    main(


# Fixed version:
import random


class Cube:
    def __init__(self, u, s, e, w, n, d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d

    def rotate(self, dic):
        if dic == "N":
            tmp = self.u
            self.u = self.s
            self.s = self.d
            self.d = self.n
            self.n = tmp
        elif dic == "E":
            tmp = self.u
            self.u = self.w
            self.w = self.d
            self.d = self.e
            self.e = tmp
        elif dic == "W":
            tmp = self.u
            self.u = self.e
            self.e = self.d
            self.d = self.w
            self.w = tmp
        else:
            tmp = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = tmp


def main():
    u, s, e, w, n, d = map(int, input().split())
    cube = Cube(u, s, e, w, n, d)
    q = int(input())
    for i in range(q):
        upper, front = map(int, input().split())
        while True:
            cube.rotate(random.choice("NWES"))
            if upper == cube.u and front == cube.s:
                print(cube.e)
                break


if __name__ == '__main__':
    main()

# Buggy version:
mask = [range(6), (1, 5, 2, 3, 0, 4), (2, 1, 5, 0, 4,3),(3, 1, 0, 5, 4, 2), (4, 0, 2, 3, 5, 1)]
mask += [mask[1][i] for i in mask[1]]

TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM = mask[0]

def set_top(dice, top):
    return [dice[i] for i in mask[top]]

def set_front(dice, front):
    twist = (0, 3, 1, 4, 2, 5)
    while dice[FRONT] != front:
        dice = [dice[i] for i in twist]
    return dice
        
label = input().split()
q = int(input())
for _ in range(q):
    top, front = map(label.index, input().split())
    dice = set_top(mask[0], top)
    dice = set_front(dice, front)
    print(label[dice[RIGHT]])

# Fixed version:
mask = [[i for i in range(6)], (1, 5, 2, 3, 0, 4), (2, 1, 5, 0, 4,3),(3, 1, 0, 5, 4, 2), (4, 0, 2, 3, 5, 1)]
mask += [[mask[1][i] for i in mask[1]]]

TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM = mask[0]

def set_top(dice, top):
    return [dice[i] for i in mask[top]]

def set_front(dice, front):
    twist = (0, 3, 1, 4, 2, 5)
    while dice[FRONT] != front:
        dice = [dice[i] for i in twist]
    return dice
        
label = input().split()
q = int(input())
for _ in range(q):
    top, front = map(label.index, input().split())
    dice = set_top(mask[0], top)
    dice = set_front(dice, front)
    print(label[dice[RIGHT]])

# Buggy version:
def whatIsRight(u,f):	
    r = 0
    if u == 1:
        if f == 2:
            r = 3
        elif f == 3:
            r = 5
        elif f == 4:
            r = 2
        elif f == 5:
            r = 4
        return r
	
    if u == 2:
        if f == 6:
            r = 3
        elif f == 3:
            r = 1
        elif f == 1:
            r = 4
        elif f == 4:
            r = 6
        return r

    if u == 3:
        if f == 5:
            r = 1
        elif f == 1:
            r = 2
        elif f == 2:
            r = 6
        elif f == 6:
            r = 3
        return r
			
    if u == 4:
        if f == 6:
            r = 2
        elif f == 2:
            r = 1
        elif f == 1:
            r = 5
        elif f == 5:
            r = 6 
        return r 
			
    if u == 5:
        if f == 3:
            r = 6
        elif f == 6:
            r = 4
        elif f == 4:
            r = 1
        elif f == 1:
            r = 3
	return r
		
    if u == 6:
        if f == 5:
            r = 3
        elif f == 3:
            r = 2
        elif f == 2:
            r = 4
        elif f == 4:
            r = 5
        return r
			
n = list(map(int,input().split()))
N = int(input())

for i in range(N):
    m = list(map(int,input().split()))
    num = []

    for j in range(len(m)):
        for k in range(len(n)):
            if m[j] == n[k]:
                num.append(k+1)
                break
	
    print(n[whatIsRight(num[0],num[1]) -1])

# Fixed version:
def whatIsRight(u,f):	
    r = 0
    if u == 1:
        if f == 2:
            r = 3
        elif f == 3:
            r = 5
        elif f == 4:
            r = 2
        elif f == 5:
            r = 4
	
    elif u == 2:
        if f == 6:
            r = 3
        elif f == 3:
            r = 1
        elif f == 1:
            r = 4
        elif f == 4:
            r = 6

    elif u == 3:
        if f == 5:
            r = 1
        elif f == 1:
            r = 2
        elif f == 2:
            r = 6
        elif f == 6:
            r = 5
			
    elif u == 4:
        if f == 6:
            r = 2
        elif f == 2:
            r = 1
        elif f == 1:
            r = 5
        elif f == 5:
            r = 6 
			
    elif u == 5:
        if f == 3:
            r = 6
        elif f == 6:
            r = 4
        elif f == 4:
            r = 1
        elif f == 1:
            r = 3
		
    elif u == 6:
        if f == 5:
            r = 3
        elif f == 3:
            r = 2
        elif f == 2:
            r = 4
        elif f == 4:
            r = 5
    return r
			
n = list(map(int,input().split()))
N = int(input())

for i in range(N):
    m = list(map(int,input().split()))
    num = []

    for j in range(len(m)):
        for k in range(len(n)):
            if m[j] == n[k]:
                num.append(k+1)
                break
	
    print(n[whatIsRight(num[0],num[1]) -1])

# Buggy version:
pattern = {
           "12651":3,
           "15621":4,
           "13641":5,
           "14631":2,
           "23542":1,
           "24532":6,
          }

label = input().split()
l2d = {label[i]:str(i+1) for i in range(6)}
print(l2d)

qn = int(input())
for i in range(qn):
    top, front = input().split()
    tf = l2d[top]+l2d[front]

    for p in pattern:
        if tf in p:
            print(label[pattern[p]-1])

# Fixed version:
pattern = {
           "12651":3,
           "15621":4,
           "13641":5,
           "14631":2,
           "23542":1,
           "24532":6,
          }

label = input().split()
l2d = {label[i]:str(i+1) for i in range(6)}

qn = int(input())
for i in range(qn):
    top, front = input().split()
    tf = l2d[top]+l2d[front]

    for p in pattern:
        if tf in p:
            print(label[pattern[p]-1])

# Buggy version:
rot = ((1,2,4,3),(2,0,3,5),(0,1,5,4))
f = input().split()

for i in range(int(input())):
    t, s = map(f.index, input().split())
    for j in rot:
        if (t in j)*(s in j):
            continue
        d = rot.index(j)
        if (j.index(t) - j.index(s)) %4 == 1:
            d -= 5
    print(f[abs(d)])

# Fixed version:
R = ((1,2,4,3),(2,0,3,5),(0,1,5,4))
f = input().split()

for i in range(int(input())):
    t, s = map(f.index, input().split())
    for j in R:
        if not (t in j)*(s in j):
            continue
        d = R.index(j)
        if (j.index(t) - j.index(s)) %4 == 1:
            d -= 5
    print(f[abs(d)])

# Buggy version:
class dice:
    def __init__(self, pip):
        self.pip = pip
        
    def move(self,dir):
        if str(dir) == "E":
            self.pip[0],self.pip[2],self.pip[3],self.pip[5] = self.pip[3],self.pip[0],self.pip[5],self.pip[2]
        elif str(dir) == "W":
            self.pip[0],self.pip[2],self.pip[3],self.pip[5] = self.pip[2],self.pip[5],self.pip[0],self.pip[3]
        elif str(dir) == "N":
            self.pip[0],self.pip[1],self.pip[4],self.pip[5] = self.pip[1],self.pip[5],self.pip[0],self.pip[4]
        elif str(dir) == "S":
            self.pip[0],self.pip[1],self.pip[4],self.pip[5] = self.pip[4],self.pip[0],self.pip[5],self.pip[1]

d = dice(list(map(int,input().split())))
n = int(input())

for i in range(n):
    
    top, front = map(int,input().split())
    
    for op in "EEENEEENEEESEEESEEENEEEN"
        d.move(op)
        if d.pip[0] == top and d.pip[1] == front:
            break
    
    print(d.pip[2])

# Fixed version:
class dice:
    def __init__(self, pip):
        self.pip = pip
        
    def move(self,dir):
        if str(dir) == "E":
            self.pip[0],self.pip[2],self.pip[3],self.pip[5] = self.pip[3],self.pip[0],self.pip[5],self.pip[2]
        elif str(dir) == "W":
            self.pip[0],self.pip[2],self.pip[3],self.pip[5] = self.pip[2],self.pip[5],self.pip[0],self.pip[3]
        elif str(dir) == "N":
            self.pip[0],self.pip[1],self.pip[4],self.pip[5] = self.pip[1],self.pip[5],self.pip[0],self.pip[4]
        elif str(dir) == "S":
            self.pip[0],self.pip[1],self.pip[4],self.pip[5] = self.pip[4],self.pip[0],self.pip[5],self.pip[1]

d = dice(list(map(int,input().split())))
n = int(input())

for i in range(n):
    
    top, front = map(int,input().split())
    
    for op in "EEENEEENEEESEEESEEENEEEN":
        if d.pip[0] == top and d.pip[1] == front:
            break
        d.move(op)    
    print(d.pip[2])

# Buggy version:
class Dice:
    def __init__(self):
        self.front = '1'
        self.back = '6'
        self.top = '5'
        self.bottom = '2'
        self. right = '3'
        self.left = '4'
    def turnRight(self):
        temp = self.top
        self.top = self.left
        self.left = self.bottom
        self.bottom = self.right
        self.right = temp
        return self.top
    def turnLeft(self):
        temp = self.top
        self.top = self.right
        self.right = self.bottom
        self.bottom = self.left
        self.left = temp
        return self.top
    def turnFront(self):
        temp = self.top
        self.top = self.back
        self.back = self.bottom
        self.bottom = self.front
        self.front = temp
        return self.top
    def turnBack(self):
        temp = self.top
        self.top = self.front
        self.front = self.bottom
        self.bottom = self.back
        self.back = temp
        return self.top
 
mydice = Dice()
mydice.top, mydice.front, mydice.right, mydice.left, mydice.back, mydice.bottom = input().split()
 
n = int(input())
for q in range(n):
    top, front = input().split()
 
    for rotate in range(3):
        mydice.turnFront()
        if mydice.top == front:
            break
        mydice.turnRight()
        if mydice.top == front:
            break
    mydice.turnBack()
    while top != mydice.top:
        mydice.turnRight()
    print(mydice.right)

# Fixed version:
class Dice:
    def __init__(self):
        self.front = '1'
        self.back = '6'
        self.top = '5'
        self.bottom = '2'
        self. right = '3'
        self.left = '4'
    def turnRight(self):
        temp = self.top
        self.top = self.left
        self.left = self.bottom
        self.bottom = self.right
        self.right = temp
        return self.top
    def turnLeft(self):
        temp = self.top
        self.top = self.right
        self.right = self.bottom
        self.bottom = self.left
        self.left = temp
        return self.top
    def turnFront(self):
        temp = self.top
        self.top = self.back
        self.back = self.bottom
        self.bottom = self.front
        self.front = temp
        return self.top
    def turnBack(self):
        temp = self.top
        self.top = self.front
        self.front = self.bottom
        self.bottom = self.back
        self.back = temp
        return self.top
 
mydice = Dice()
mydice.top, mydice.front, mydice.right, mydice.left, mydice.back, mydice.bottom = input().split()
 
n = int(input())
for q in range(n):
    top, front = input().split()
 
    for rotate in range(3):
        mydice.turnFront()
        if mydice.top == front:
            break
        mydice.turnRight()
        if mydice.top == front:
            break
    mydice.turnFront()
    while top != mydice.top:
        mydice.turnRight()
    print(mydice.right)

# Buggy version:
class Dice2(Dice1):

    def __init__(self, v): # v [Top, Front, Right, Left, Back, Bottom ]
        super().__init__(v)

        self.dTFR = {}
        for i, j, k in ((ii, (ii++2)%6, (ii+4)%6) for ii in  range(6)):
            self.dTFR[self.view[i]]=[self.view[v] for v in (j, k, 5-j, 5-k, j)]


d2, q = Dice2(list(map(int, input().split()))), int(input())
for i in range(q):
    t, f = map(int, input().split())
    print(d2.dTFR[t][d2.dTFR[t].index(f)+1] )

# Fixed version:
class Dice2():

    def __init__(self, v): # v [Top, Front, Right, Left, Back, Bottom ]
        self.view = v
        self.dTFR = {}
        for i, j, k in ((ii, (ii++2)%6, (ii+4)%6) for ii in  range(6)):
            self.dTFR[self.view[i]]=[self.view[v] for v in (j, k, 5-j, 5-k, j)]


d2, q = Dice2(list(map(int, input().split()))), int(input())
for i in range(q):
    t, f = map(int, input().split())
    print(d2.dTFR[t][d2.dTFR[t].index(f)+1] )

# Buggy version:
# -*- coding: utf-8 -*-


class Dice:
    def __init__(self, n):
        self.upper    = n[0]
        self.backward = n[1]
        self.right    = n[2]
        self.left     = n[3]
        self.ahead    = n[4]
        self.bottom   = n[5]

    def roll_north(self):
        self.upper, self.ahead, self.bottom, self.backward = self.backward, self.upper, self.ahead, self.bottom

    def roll_south(self):
        self.upper, self.ahead, self.bottom, self.backward = self.ahead, self.bottom, self.backward, self.upper

    def roll_east(self):
        self.upper, self.right, self.bottom, self.left = self.left, self.upper, self.right, self.bottom

    def roll_west(self):
        self.upper, self.right, self.bottom, self.left = self.right, self.bottom, self.left, self.upper

    def roll_side(self):
        self.ahead, self.right, self.backward, self.left = self.left, self.ahead, self.right, self.backward


dice_info = Dice(list(map(int, input().split())))
q = int(input())

for i in range(q):
    upper, ahead = map(int, input().split())

    while not (upper == dice_info.upper or ahead == dice_info.ahead):
        dice_info.roll_north()
        dice_info.roll_side()

    while upper != dice_info.upper:
        dice_info.roll_east()

    while ahead != dice_info.ahead:
        dice_info.roll_side()

    print(dice_info.left)


# Fixed version:
# -*- coding: utf-8 -*-


class Dice:
    def __init__(self, n):
        self.upper    = n[0]
        self.backward = n[1]
        self.right    = n[2]
        self.left     = n[3]
        self.ahead    = n[4]
        self.bottom   = n[5]

    def roll_north(self):
        self.upper, self.ahead, self.bottom, self.backward = self.backward, self.upper, self.ahead, self.bottom

    def roll_south(self):
        self.upper, self.ahead, self.bottom, self.backward = self.ahead, self.bottom, self.backward, self.upper

    def roll_east(self):
        self.upper, self.right, self.bottom, self.left = self.left, self.upper, self.right, self.bottom

    def roll_west(self):
        self.upper, self.right, self.bottom, self.left = self.right, self.bottom, self.left, self.upper

    def roll_side(self):
        self.ahead, self.right, self.backward, self.left = self.left, self.ahead, self.right, self.backward


dice_info = Dice(list(map(int, input().split())))
q = int(input())

for i in range(q):
    upper, ahead = map(int, input().split())

    if upper is dice_info.left:
        dice_info.roll_east()
        
    elif upper is dice_info.right:
        dice_info.roll_west()

    while not (upper == dice_info.upper or ahead == dice_info.ahead):
        dice_info.roll_north()

    while upper != dice_info.upper:
        dice_info.roll_east()

    while ahead != dice_info.ahead:
        dice_info.roll_side()

    print(dice_info.left)

# Buggy version:
d=input().split()
n={0:(1,2,4,3,1),1:(0,3,5,2,0),2:(0,1,5,4,0),3:(1,0,4,5,1),4:(0,2,5,3,0),5:(1,3,4,2,1)}
for _ in range(int(input())):
 t,f=[d.index(s) for s in input().split()]
 n=n[int(t)]
 print(d[n[n.index(int(f))+1]])


# Fixed version:

d=input().split()
for _ in range(int(input())):
 t,f=[d.index(s)for s in input().split()]
 n=[(1,2,4,3,1),(0,3,5,2,0),(0,1,5,4,0),(1,0,4,5,1),(0,2,5,3,0),(1,3,4,2,1)][t]
 print(d[n[n.index(f)+1]])