Problem:
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 (Python):
class Dice:
    def __init__(self, dice):
        self.dice = dice
        self.face = {'head':1, 'tail':4, 'left':3, 'right':2, 'above':0, 'below':5}
    
    def roll(self, ch):
        dice = self.dice
        face = self.face
        if ch == 'S':
            tmp = dice[face['head']]
            dice[face['head']] = dice[face['above']]
            dice[face['above']] = dice[face['tail']]
            dice[face['tail']] = dice[face['below']]
            dice[face['below']] = tmp
        elif ch == 'N':
            tmp = dice[face['head']]
            dice[face['head']] = dice[face['below']]
            dice[face['below']] = dice[face['tail']]
            dice[face['tail']] = dice[face['above']]
            dice[face['above']] = tmp
        elif ch == 'W':
            tmp = dice[face['left']]
            dice[face['left']] = dice[face['above']]
            dice[face['above']] = dice[face['right']]
            dice[face['right']] = dice[face['below']]
            dice[face['below']] = tmp
        else:
            tmp = dice[face['left']]
            dice[face['left']] = dice[face['below']]
            dice[face['below']] = dice[face['right']]
            dice[face['right']] = dice[face['above']]
            dice[face['above']] = tmp

    def top(self):
        dice = self.dice
        face = self.face
        return dice[face['above']]
    
    def find_right(self, a, b):
        dice = self.dice
        face = self.face
        order = 'NNNNWNNNWNNNENNNENNNWNNN'
        for ch in order:
            self.roll(ch)
            if dice[face['above']] == a and dice[face['head']] == b:
                break
        return dice[face['right']]

dice = Dice([*map(int, input().split())])
q = int(input())
while q > 0:
    q -= 1
    a, b = map(int, input().split())
    print(dice.find_right(a, b))

Solution (Python):
roll_dict = dict(E = (3, 1, 0, 5, 4, 2), W = (2, 1, 5, 0, 4, 3), S = (4, 0, 2, 3, 5, 1), N = (1, 5, 2, 3, 0, 4))

dices = []
dices.append(list(map(int, input().split())))
q = int(input())

for i in "EWSN":
  dice = dices[0]
  new_dice = []
  for j in range(6):
    new_dice.append(dice[ roll_dict[i][j] ])
  dices.append(new_dice)

dice = dices[1]
new_dice = []
for i in range(6):
  new_dice.append(dice[ roll_dict["E"][i] ])
dices.append(new_dice)

for i in range(q):
  upper, front = map(int, input().split())
  for j in range(6):
    if dices[j][0] == upper:
      if dices[j][1] == front:
        print(dices[j][2])
      elif dices[j][2] == front:
        print(dices[j][4])
      elif dices[j][4] == front:
        print(dices[j][3])
      elif dices[j][3] == front:
        print(dices[j][1])

Solution (Python):



t, s, e, w, n, b = map(int, input().split())
p_num = int(input())


dice_dic = {}
dice_dic[t] = {s:e, e:n, n:w, w:s}
dice_dic[b] = {n:e, w:n, s:w, e:s}
dice_dic[s] = {e:t, t:w, w:b, b:e}
dice_dic[n] = {t:e, e:b, b:w, w:t}
dice_dic[e] = {s:b, b:n, n:t, t:s}
dice_dic[w] = {s:t, t:n, n:b, b:s}


for i in range(p_num):
    in_1, in_2 = map(int, input().split())
    print(dice_dic[in_1][in_2])

Solution (Python):
dice = input().rstrip().split(' ')
indication = "EEENEEENEEESEEESEEENEEENSWWSWWWNWWWNWWWSWWWSWWW"
indic = list(indication)
length = len(indic)
num = int(input())
for j in range(num):
  s = input().rstrip().split(' ')
  top = int(s[0])
  front = int(s[1])
  for i in range(length):
    newdice = [0] * 6
    if str(indic[i]) == "N":
      newdice[0] = dice[1]
      newdice[1] = dice[5]
      newdice[2] = dice[2]
      newdice[3] = dice[3]
      newdice[4] = dice[0]
      newdice[5] = dice[4]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;
    elif str(indic[i]) == "S":
      newdice[0] = dice[4]
      newdice[1] = dice[0]
      newdice[2] = dice[2]
      newdice[3] = dice[3]
      newdice[4] = dice[5]
      newdice[5] = dice[1]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;
    elif str(indic[i]) == "E":
      newdice[0] = dice[3]
      newdice[1] = dice[1]
      newdice[2] = dice[0]
      newdice[3] = dice[5]
      newdice[4] = dice[4]
      newdice[5] = dice[2]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;
    elif str(indic[i]) == "W":
      newdice[0] = dice[2]
      newdice[1] = dice[1]
      newdice[2] = dice[5]
      newdice[3] = dice[0]
      newdice[4] = dice[4]
      newdice[5] = dice[3]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;

Solution (Python):
class dice():
    
    def __init__(self, nums):
        self.pos = []
        self.pos.extend(nums)
    
    def right(self, et, ef):
        
        

        t = self.pos.index(et)  
        f = self.pos.index(ef)  
        
        
        dr = [(1,2,4,3),(0,3,5,2),(0,1,5,4),(0,4,5,1),(0,2,5,3),(1,3,4,2)]
        r = (dr[t].index(f) + 1) % 4
        print(self.pos[dr[t][r]])

d = dice([int(x) for x in input().split()])
q = int(input())
for i in range(q):
    d.right(*map(int, input().split()))

Solution (Python):
class dice():
    def __init__(self,labels):
        self.l=labels

    def zen(self,a,b):
        x1=self.l[0]
        x2=self.l[1]
        x3=self.l[2]
        x4=self.l[3]
        x5=self.l[4]
        x6=self.l[5]
        if (a,b)==(x1,x2) or (a,b)==(x2,x6) or (a,b)==(x6,x5) or (a,b)==(x5,x1):
            print(x3)
        elif (a,b)==(x3,x2) or (a,b)==(x2,x4) or (a,b)==(x4,x5) or (a,b)==(x5,x3):
            print(x6)
        elif (a,b)==(x6,x2) or (a,b)==(x2,x1) or (a,b)==(x1,x5) or (a,b)==(x5,x6):
            print(x4)
        elif (a,b)==(x3,x1) or (a,b)==(x1,x4) or (a,b)==(x4,x6) or (a,b)==(x6,x3):
            print(x2)
        elif (a,b)==(x3,x6) or (a,b)==(x6,x4) or (a,b)==(x4,x1) or (a,b)==(x1,x3):
            print(x5)
        elif (a,b)==(x4,x2) or (a,b)==(x2,x3) or (a,b)==(x3,x5) or (a,b)==(x5,x4):
            print(x1)

dice=dice([int(i) for i in input().split()])
n=int(input())
for i in range(n):
    a,b=map(int,input().split())
    dice.zen(a,b)

Solution (Python):
from copy import deepcopy

class Dice():

    def __init__(self):
        self.E = [4,2,1,6,5,3]
        self.N = [2,6,3,4,1,5]
        self.S = [5,1,3,4,6,2]
        self.W = [3,2,6,1,5,4]
        self.order = "NNNNWNNNWNNNENNNENNNWNNN"

    def num_input(self):
        self.number = list(map(int,input().split()))

    def rotate(self,order):
        tmp = deepcopy(self.number)
        if order == "E":
            for i in range(6):
                self.number[i] = tmp[self.E[i]-1]
        elif order == "N":
            for i in range(6):
                self.number[i] = tmp[self.N[i]-1]
        elif order == "S":
            for i in range(6):
                self.number[i] = tmp[self.S[i]-1]
        elif order == "W":
            for i in range(6):
                self.number[i] = tmp[self.W[i]-1]
    
    def sidenum(self,x,y):
        for i in self.order:
            if self.number[1] == y and self.number[0] == x:
                print(self.number[2])
                break
            else:
                self.rotate(i)

if __name__ == "__main__":
    dice = Dice()
    dice.num_input()
    q = int(input())
    for i in range(q):
        x,y = map(int,input().split())
        dice.sidenum(x,y)

Solution (Python):


class dice:
    def __init__(self,dice):
        self.dice = dice

    def roll_n(self):
        d = self.dice
        self.dice = [d[1],d[5],d[2],d[3],d[0],d[4]]

    def roll_e(self):
        d = self.dice
        self.dice = [d[3],d[1],d[0],d[5],d[4],d[2]]

    def roll_s(self):
        d = self.dice
        self.dice = [d[4],d[0],d[2],d[3],d[5],d[1]]
    
    def roll_w(self):
        d = self.dice
        self.dice = [d[2],d[1],d[5],d[0],d[4],d[3]]

    def top(self):
        return self.dice[0]

    def roll(self,command):
        if command=='N':self.roll_n()
        if command=='E':self.roll_e()
        if command=='S':self.roll_s()
        if command=='W':self.roll_w()

def main():
    com1 = 'SSSWSSS'
    com2 = 'WWWW'
    d = dice(list(map(int,input().split())))
    q = int(input())
    for _ in range(q):
        a,b = map(int,input().split())
        for c in com1:
            if b==d.dice[1]:break
            d.roll(c)
        for c in com2:
            if a==d.dice[0]:print (d.dice[2])
            d.roll(c)

if __name__ == '__main__':
    main()

Solution (Python):
n=list(map(int,input().split()))
p=['2354','3146','2651','1562','1364','2453']
for _ in range(int(input())):
    t,m=map(int,input().split())
    t=n.index(t)
    m=str(n.index(m)+1)
    print(n[int(p[t][(p[t].index(m)+1)%4])-1])

Solution (Python):
class dice:
    state={}
    writtenval={}
    def __init__(self, state_=None,writtenval_=None):
        self.writtenval = writtenval_
        

        self.state={}
        if state_ != None:
            for key in state_.keys():
                self.state[key]=writtenval.index(state_[key])
            for keyset in [["now","behind"], ["N","S"], ["S","N"], ["W","E"], ["E","W"], ["behind","now"]]:
                if not keyset[0] in self.state.keys():
                    if keyset[1] in self.state.keys():
                        self.state[keyset[0]]=7-self.state[keyset[1]]
            if len(self.state.keys()) == 4 :
                state=self.state
                keys=self.state.keys()
                
                if not "N" in keys or not "S" in keys:
                    if state["now"] == 1:
                        if state["E"] == 2:
                            state["N"]=3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 2
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"] == 2:
                        if state["E"] == 3:
                            state["N"]=1
                            state["S"]=7-state["N"]
                        elif state["E"] == 1:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 3
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==3:
                        if state["E"] == 6:
                            state["N"]=2
                            state["S"]=7-state["N"]
                        elif state["E"] == 2:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        elif state["E"] == 1:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==4:
                        if state["E"] == 1:
                            state["N"]=2
                            state["S"]=7-state["N"]
                        elif state["E"] == 2:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==5:
                        if state["E"] == 1:
                            state["N"]=4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==6:
                        if state["E"] == 2:
                            state["N"]=3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 2
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    else:
                        print("miss")
                elif not "W" in keys or not "E" in keys:
                    if state["now"] == 1:
                        if state["N"] == 5:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 2:
                        if state["N"] == 1:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 3:
                        if state["N"] == 1:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 4:
                        if state["N"] == 1:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 5:
                        if state["N"] == 1:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 6:
                        if state["N"] == 2:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                elif not "now" in keys or not "behind" in keys:
                    if state["N"] == 1:
                        if state["W"] == 2:
                            state["now"]=3
                            state["E"]=7-state["now"]
                        elif state["W"] == 5:
                            state["now"]=4
                            state["E"]=7-state["now"]
                        elif state["W"] == 4:
                            state["now"]=2
                            state["E"]=7-state["now"]
                        elif state["W"] == 3:
                            state["now"]=5
                            state["E"]=7-state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 2:
                        if state["W"] == 3:
                            state["now"]=1
                            state["E"]=7-state["now"]
                        elif state["W"] == 6:
                            state["now"]=3
                            state["E"]=7-state["now"]
                        elif state["W"] == 4:
                            state["now"]=6
                            state["E"]=7-state["now"]
                        elif state["W"] == 1:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 3:
                        if state["W"] == 5:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 2:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 4:
                        if state["W"] == 2:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 5:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 5:
                        if state["W"] == 4:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=3
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 3:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 6:
                        if state["W"] == 3:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 5:
                            state["now"]=3
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 2:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 4:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    else:
                        print("miss")
            else:
                print("miss")
        else:
            self.state={"now":6,"N":5,"S":2,"W":4,"E":3,"behind":1}
        self.writtenval=writtenval_

    def chState(self):
        state=self.state
        for order in input():
            if order == "N":
                newstate = {"now": state["N"],
                            "N": state["behind"],
                            "S": state["now"],
                            "W": state["W"],
                            "E":state["E"],
                            "behind":state["S"]
                            }
                state=newstate
            elif order == "S":
                newstate = {"now": state["S"],
                            "N": state["now"],
                            "S": state["behind"],
                            "W": state["W"],
                            "E":state["E"],
                            "behind":state["N"]
                            }
                state=newstate
            elif order == "W":
                newstate = {"now": state["W"],
                            "N": state["N"],
                            "S": state["S"],
                            "W": state["behind"],
                            "E":state["now"],
                            "behind":state["E"]
                            }
                state=newstate
            elif order == "E":
                newstate = {"now": state["E"],
                            "N": state["N"],
                            "S": state["S"],
                            "W": state["now"],
                            "E":state["behind"],
                            "behind":state["W"]
                            }
                state = newstate
        self.state=state

writtenval=[-1]+[int(item) for item in input().split()]
loop=int(input())
for _ in range(loop):
    [behind,N]=[int(item) for item in input().split()]
    dice_ = dice({"N":N,"behind":behind},writtenval)
    print(dice_.writtenval[dice_.state["E"]])

Solution (Python):
dice = ['23542', '14631', '12651', '15621', '13641', '24532']

d = input().split()
q = int(input())

for _ in range(q):
    num = input().split()
    order = str(d.index(num[0]) + 1) + str(d.index(num[1]) + 1)
    [print(d[i]) for i in range(6) if order in dice[i]]

Solution (Python):
class Dice():

    def __init__(self, nums):
        self.__indexTop__ = 1
        self.__indexFront__ = 2
        self.__indexRight__ = 3
        self.__indexLeft__ = 4
        self.__indexBack__ = 5
        self.__indexBottom__ = 6
        self.__sides__ = {1:nums[0], 2:nums[1], 3:nums[2], 4:nums[3], 5:nums[4], 6:nums[5]}
    
    def getTopSideNum(self):
        return self.__sides__[self.__indexTop__]
    
    def getFrontSideNum(self):
        return self.__sides__[self.__indexFront__]
        
    def getRightSideNum(self):
        return self.__sides__[self.__indexRight__]
    
    def roll(self, direction):
        initTop = self.__indexTop__
        initFront = self.__indexFront__
        initRight = self.__indexRight__
        initLeft = self.__indexLeft__
        initBack = self.__indexBack__
        initBottom = self.__indexBottom__
        if direction == 'N':
            self.__indexTop__ = initFront
            self.__indexFront__ = initBottom
            self.__indexRight__ = initRight
            self.__indexLeft__ = initLeft
            self.__indexBack__ = initTop
            self.__indexBottom__ = initBack
        elif direction == 'E':
            self.__indexTop__ = initLeft
            self.__indexFront__ = initFront
            self.__indexRight__ = initTop
            self.__indexLeft__ = initBottom
            self.__indexBack__ = initBack
            self.__indexBottom__ = initRight
        elif direction == 'S':
            self.__indexTop__ = initBack
            self.__indexFront__ = initTop
            self.__indexRight__ = initRight
            self.__indexLeft__ = initLeft
            self.__indexBack__ = initBottom
            self.__indexBottom__ = initFront
        elif direction == 'W':
            self.__indexTop__ = initRight
            self.__indexFront__ = initFront
            self.__indexRight__ = initBottom
            self.__indexLeft__ = initTop
            self.__indexBack__ = initBack
            self.__indexBottom__ = initLeft
    
    def pivot(self, direction):
        initTop = self.__indexTop__
        initFront = self.__indexFront__
        initRight = self.__indexRight__
        initLeft = self.__indexLeft__
        initBack = self.__indexBack__
        initBottom = self.__indexBottom__
        if direction == 'C':
            self.__indexTop__ = initTop
            self.__indexFront__ = initRight
            self.__indexRight__ = initBack
            self.__indexLeft__ = initFront
            self.__indexBack__ = initLeft
            self.__indexBottom__ = initBottom
        elif direction == 'CC':
            self.__indexTop__ = initTop
            self.__indexFront__ = initLeft
            self.__indexRight__ = initFront
            self.__indexLeft__ = initBack
            self.__indexBack__ = initRight
            self.__indexBottom__ = initBottom

die = Dice([int(i) for i in input().split()])
q = int(input())
for i in range(q):
    top, front = (int(i) for i in input().split())
    while True:
        if die.getTopSideNum() == top:
            break
        die.roll('N')
        if die.getTopSideNum() == top:
            break
        die.roll('W')
    while True:
        if die.getFrontSideNum() == front:
            break
        die.pivot('C')
    print(die.getRightSideNum())

Solution (Python):
class Dice():
    
    def __init__(self,d1,d2,d3,d4,d5,d6):
        self.d1=d1
        self.d2=d2
        self.d3=d3
        self.d4=d4
        self.d5=d5
        self.d6=d6
    
    def roll(self,c):
        if c=='N':
            self.d1,self.d5,self.d6,self.d2=self.d2,self.d1,self.d5,self.d6
        elif c=='S':
            self.d1,self.d5,self.d6,self.d2=self.d5,self.d6,self.d2,self.d1
        elif c=='E':
            self.d1,self.d3,self.d6,self.d4=self.d4,self.d1,self.d3,self.d6
        else:
            self.d1,self.d3,self.d6,self.d4=self.d3,self.d6,self.d4,self.d1
    
a,b,c,d,e,f=map(int,input().split())
D=Dice(a,b,c,d,e,f)
n=int(input())
for i in range(n):
    up,forward=map(int,input().split())
    if up==D.d3:
        D.roll('W')
    elif up==D.d6:
        D.roll('E')
        D.roll('E')
    elif up==D.d4:
        D.roll('E')
    elif up==D.d2:
        D.roll('N')
    elif up==D.d5:
        D.roll('S')
    if forward==D.d2:
        print(D.d3)
    elif forward==D.d3:
        print(D.d5)
    elif forward==D.d5:
        print(D.d4)
    else:
        print(D.d2)

Solution (Python):
a,b,c,d,e,f=map(int,input().split())
d2={1:a,2:b,3:c,4:d,5:e,6:f}
d1={a:1,b:2,c:3,d:4,e:5,f:6}
a,b,c,d,e,f=1,2,3,4,5,6
ans=[[b,c,e,d,b],[a,d,f,c,a],[a,b,f,e,a],[a,e,f,b,a],[a,c,f,d,a],[b,d,e,c,b]]
for _ in range(int(input())):
    top,front=map(int,input().split())
    top,front=d1[top],d1[front]
    top-=1
    for i in range(len(ans[top])):
        if front==ans[top][i]:
            print(d2[ans[top][i+1]])
            break

Solution (Python):
class Dice:
    def __init__(self):
        self.u=1
        self.w=2
        self.s=3
        self.e=4
        self.n=5
        self.d=6
        self.dic={"W":0,"S":1,"E":2,"N":3}
    
    def __init__(self,u,w,s,e,n,d):
        self.u=u
        self.w=w
        self.s=s
        self.e=e
        self.n=n
        self.d=d
        self.dic={"W":0,"S":1,"E":2,"N":3}

    def rot(self,way):
        if isinstance(way,str):
            way=self.dic[way]

        if(way==0):
            c=self.u
            self.u=self.e
            self.e=self.d
            self.d=self.w
            self.w=c
        elif way==1:
            c=self.u
            self.u=self.n
            self.n=self.d
            self.d=self.s
            self.s=c
        elif way==2:
            c=self.u
            self.u=self.w
            self.w=self.d
            self.d=self.e
            self.e=c
        else :
            c=self.u
            self.u=self.s
            self.s=self.d
            self.d=self.n
            self.n=c

import random
u,s,e,w,n,d=map(int,input().split())
dice=Dice(u,w,s,e,n,d)
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    while True:
        dice.rot(random.randint(0,3))
        if(dice.u==a and dice.s==b):
            print(dice.e)
            break