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):



POSITIONS = ["top", "south", "east", "west", "north", "bottom"]


class Dice(object):

    def __init__(self, initial_faces):
        self.faces = {p: initial_faces[i] for i, p in enumerate(POSITIONS)}
        self.store_previous_faces()

    def store_previous_faces(self):
        self.previous_faces = self.faces.copy()

    def change_face(self, after, before):
        self.faces[after] = self.previous_faces[before]

    def change_faces(self, rotation):
        self.change_face(rotation[0], rotation[1])
        self.change_face(rotation[1], rotation[2])
        self.change_face(rotation[2], rotation[3])
        self.change_face(rotation[3], rotation[0])

    def roll(self, direction):
        self.store_previous_faces()
        if direction == "E":
            self.change_faces(["top", "west", "bottom", "east"])
        elif direction == "N":
            self.change_faces(["top", "south", "bottom", "north"])
        elif direction == "S":
            self.change_faces(["top", "north", "bottom", "south"])
        elif direction == "W":
            self.change_faces(["top", "east", "bottom", "west"])

    def rolls(self, directions):
        for d in directions:
            self.roll(d)


def main():
    dice = Dice(input().split())
    q = int(input())
    for x in range(q):
        [qtop, qsouth] = input().split()
        if qsouth == dice.faces["west"]:
            dice.roll("E")
        elif qsouth == dice.faces["east"]:
            dice.roll("W")
        while qsouth != dice.faces["south"]:
            dice.roll("N")
        while qtop != dice.faces["top"]:
            dice.roll("W")
        print(dice.faces["east"])

if __name__ == "__main__":
    main()

Solution (Python):
class Dice: 
    def __init__( self, nums ):
        self.face = nums
     
    def rolltoTop( self, dicenum ): 
        num = 0
        for i,val in enumerate( self.face ):
            if dicenum == val:
                num = i
                break
        if num == 1:
            self.roll( "N" )
        elif num == 2:
            self.roll( "W" )
        elif num == 3:
            self.roll( "E" )
        elif num == 4:
            self.roll( "S" )
        elif num == 5:
            self.roll( "NN" )
     
    def roll( self, actions ):
        for act in actions:
            t = 0
            if act == "E":
                t =   self.face[0]
                self.face[0] =   self.face[3]
                self.face[3] =   self.face[5]
                self.face[5] =   self.face[2]
                self.face[2] = t
            elif  act == "N":
                t =   self.face[0]
                self.face[0] =   self.face[1]
                self.face[1] =   self.face[5]
                self.face[5] =   self.face[4]
                self.face[4] = t
            elif  act == "S":
                t =  self.face[ 0]
                self.face[0] =  self.face[4]
                self.face[4] =  self.face[5]
                self.face[5] =  self.face[1]
                self.face[1] = t    
            elif  act == "W":
                t =  self.face[0]
                self.face[0] =  self.face[2]
                self.face[2] =  self.face[5]
                self.face[5] =  self.face[3]
                self.face[3] = t    
            elif  act == "M":
                t =  self.face[1]
                self.face[1] =  self.face[2]
                self.face[2] =  self.face[4]
                self.face[4] =  self.face[3]
                self.face[3] = t
 
diceface = [ int( val ) for val in input( ).split( " " ) ]
dice = Dice( diceface )
q = int( input( ) )
for i in range( q ):
    topnum, frontnum = [ int( val ) for val in input( ).split( " " ) ]
    dice.rolltoTop( topnum )
    while dice.face[1] != frontnum:
        dice.roll( "M" )
         
    print( dice.face[2] )

Solution (Python):
class Dice(object):

    def __init__(self, num):
        self.num = num

    def rotate_S(self):
        self.num = [self.num[4], self.num[0], self.num[2], self.num[3], self.num[5], self.num[1]]

    def rotate_N(self):
        self.num = [self.num[1], self.num[5], self.num[2], self.num[3], self.num[0], self.num[4]]

    def rotate_W(self):
        self.num = [self.num[2], self.num[1], self.num[5], self.num[0], self.num[4], self.num[3]]

    def rotate_E(self):
        self.num = [self.num[3], self.num[1], self.num[0], self.num[5], self.num[4], self.num[2]]

    def get_right_num(self, a, b):
        top_index = self.num.index(a) + 1
        front_index = self.num.index(b) + 1
        if [top_index, front_index] in [[2, 3], [3, 5], [5, 4], [4, 2]]:
            return self.num[0]
        elif [top_index, front_index] in [[1, 4], [4, 6], [6, 3], [3, 1]]:
            return self.num[1]
        elif [top_index, front_index] in [[1, 2], [2, 6], [6, 5], [5, 1]]:
            return self.num[2]
        elif [top_index, front_index] in [[1, 5], [5, 6], [6, 2], [2, 1]]:
            return self.num[3]
        elif [top_index, front_index] in [[1, 3], [3, 6], [6, 4], [4, 1]]:
            return self.num[4]
        elif [top_index, front_index] in [[2, 4], [4, 5], [5, 3], [3, 2]]:
            return self.num[5]

dice = Dice(map(int, raw_input().split()))
Q = input()
for q in range(Q):
    num_set = map(int, raw_input().split())
    print dice.get_right_num(num_set[0], num_set[1])

Solution (Python):
import copy

class Dice(object):
	def __init__(self,List):
		self.face=List
		
	def n_spin(self,a_list):
		List=copy.copy(a_list)
		temp=List[0]
		List[0]=List[1]
		List[1]=List[5]
		List[5]=List[4]
		List[4]=temp
		return List
		
 	def s_spin(self,a_list):
 		List=copy.copy(a_list)
 		temp=List[0]
 		List[0]=List[4]
 		List[4]=List[5]
		List[5]=List[1]
		List[1]=temp
		return List
		
	def e_spin(self,a_list):
		List=copy.copy(a_list)
		temp=List[0]
		List[0]=List[3]
		List[3]=List[5]
		List[5]=List[2]
		List[2]=temp
		return List
		
 	def w_spin(self,a_list):
 		List = copy.copy(a_list)
 		temp=List[0]
 		List[0]=List[2]
 		List[2]=List[5]
		List[5]=List[3]
		List[3]=temp
		return List

dice=Dice(map(int,raw_input().split()))
emer=copy.copy(dice.face)
face_storage=[]
face_storage.append(emer)
q=input()
for a in range(q):
	question=map(int,raw_input().split())
	for a in range(24):
		face_storage.append(dice.n_spin(face_storage[a]))
		face_storage.append(dice.s_spin(face_storage[a]))
		face_storage.append(dice.e_spin(face_storage[a]))
		face_storage.append(dice.w_spin(face_storage[a]))
	for c in face_storage:
		if c[0]==question[0] and c[1]==question[1]:
			print(c[2])
			break

Solution (Python):
class Dice:
    def __init__(self, num_list):
        self.faces = num_list

    def find_rf(self, top, front):
        tf = str(self.faces.index(top)) + str(self.faces.index(front))
        if tf in '12431':
            return self.faces[0]
        elif tf in '03520':
            return self.faces[1]
        elif tf in '01540':
            return self.faces[2]
        elif tf in '04510':
            return self.faces[3]
        elif tf in '02530':
            return self.faces[4]
        else:
            return self.faces[5]

num_list = tuple(map(int, input().split()))

dice = Dice(num_list)

q = int(input())

for i in range(q):
    top, front = map(int, input().split())
    right_face = dice.find_rf(top, front)
    print(right_face)

Solution (Python):
class Dice:
    def __init__(self, face_vals):
        self.faces = dict(zip(['top', 'front', 'right',
                               'left', 'back', 'bottom'], face_vals.split()))

    def roll(self, direction):
        if direction == 'N':
            self.faces['top'], self.faces['front'], self.faces['bottom'], \
            self.faces['back'] = self.faces['front'], self.faces['bottom'], \
            self.faces['back'], self.faces['top']
        elif direction == 'S':
            self.faces['top'], self.faces['front'], self.faces['bottom'], \
            self.faces['back'] = self.faces['back'], self.faces['top'], \
            self.faces['front'], self.faces['bottom']
        elif direction == 'E':
            self.faces['top'], self.faces['right'], self.faces['bottom'], \
            self.faces['left'] = self.faces['left'], self.faces['top'], \
            self.faces['right'], self.faces['bottom']
        else:
            self.faces['top'], self.faces['right'], self.faces['bottom'], \
            self.faces['left'] = self.faces['right'], self.faces['bottom'], \
            self.faces['left'], self.faces['top']

    def _revolve(self, direction):
        if direction == 'R':
            self.faces['front'], self.faces['left'], self.faces['back'], \
            self.faces['right'] = self.faces['right'], self.faces['front'], \
            self.faces['left'], self.faces['back']
        else:
            self.faces['front'], self.faces['left'], self.faces['back'], \
            self.faces['right'] = self.faces['left'], self.faces['back'], \
            self.faces['right'], self.faces['front']
            
    def show_right_face(self, top, front):
        if front == self.faces['back']:
            if top == self.faces['top']:
                self._revolve('R')
                self._revolve('R')
            else:
                self.roll('S')
                self.roll('S')
        elif front == self.faces['top']:
            self.roll('S')
        elif front == self.faces['right']:
            self._revolve('R')
        elif front == self.faces['left']:
            self._revolve('L')
        elif front == self.faces['bottom']:
            self.roll('N')
        if top == self.faces['bottom']:
            self.roll('E')
            self.roll('E')
        elif top == self.faces['right']:
            self.roll('W')
        elif top == self.faces['left']:
            self.roll('E')
        print(self.faces['right'])


import sys

face_vals = sys.stdin.readline()

dice = Dice(face_vals)

n = int(sys.stdin.readline())

for i in range(n):
    top, front = sys.stdin.readline().split()
    dice.show_right_face(top, front)

Solution (Python):
class Dice:
    def __init__(self, face_vals):
        self.faces = dict(zip(['top', 'front', 'right',
                               'left', 'back', 'bottom'], face_vals.split()))

    def roll(self, direction):
        if direction == 'N':
            self.faces['top'], self.faces['front'], self.faces['bottom'], \
            self.faces['back'] = self.faces['front'], self.faces['bottom'], \
            self.faces['back'], self.faces['top']
        elif direction == 'S':
            self.faces['top'], self.faces['front'], self.faces['bottom'], \
            self.faces['back'] = self.faces['back'], self.faces['top'], \
            self.faces['front'], self.faces['bottom']
        elif direction == 'E':
            self.faces['top'], self.faces['right'], self.faces['bottom'], \
            self.faces['left'] = self.faces['left'], self.faces['top'], \
            self.faces['right'], self.faces['bottom']
        else:
            self.faces['top'], self.faces['right'], self.faces['bottom'], \
            self.faces['left'] = self.faces['right'], self.faces['bottom'], \
            self.faces['left'], self.faces['top']

    def _revolve(self, direction):
        if direction == 'R':
            self.faces['front'], self.faces['left'], self.faces['back'], \
            self.faces['right'] = self.faces['right'], self.faces['front'], \
            self.faces['left'], self.faces['back']
        else:
            self.faces['front'], self.faces['left'], self.faces['back'], \
            self.faces['right'] = self.faces['left'], self.faces['back'], \
            self.faces['right'], self.faces['front']
            
    def show_right_face(self, top, front):
        if front == self.faces['back']:
            if top == self.faces['top']:
                self._revolve('R')
                self._revolve('R')
            else:
                self.roll('S')
                self.roll('S')
        elif front == self.faces['top']:
            self.roll('S')
        elif front == self.faces['right']:
            self._revolve('R')
        elif front == self.faces['left']:
            self._revolve('L')
        elif front == self.faces['bottom']:
            self.roll('N')
        if top == self.faces['bottom']:
            self.roll('E')
            self.roll('E')
        elif top == self.faces['right']:
            self.roll('W')
        elif top == self.faces['left']:
            self.roll('E')
        print(self.faces['right'])


import sys

face_vals = sys.stdin.readline()

dice = Dice(face_vals)

n = int(sys.stdin.readline())

for i in range(n):
    top, front = sys.stdin.readline().split()
    dice.show_right_face(top, front)

Solution (Python):
class Dice:
    def __init__(self, face_vals):
        self.faces = dict(zip(['top', 'front', 'right',
                               'left', 'back', 'bottom'], face_vals.split()))

    def roll(self, direction):
        if direction == 'N':
            self.faces['top'], self.faces['front'], self.faces['bottom'], \
            self.faces['back'] = self.faces['front'], self.faces['bottom'], \
            self.faces['back'], self.faces['top']
        elif direction == 'S':
            self.faces['top'], self.faces['front'], self.faces['bottom'], \
            self.faces['back'] = self.faces['back'], self.faces['top'], \
            self.faces['front'], self.faces['bottom']
        elif direction == 'E':
            self.faces['top'], self.faces['right'], self.faces['bottom'], \
            self.faces['left'] = self.faces['left'], self.faces['top'], \
            self.faces['right'], self.faces['bottom']
        else:
            self.faces['top'], self.faces['right'], self.faces['bottom'], \
            self.faces['left'] = self.faces['right'], self.faces['bottom'], \
            self.faces['left'], self.faces['top']

    def _revolve(self, direction):
        if direction == 'R':
            self.faces['front'], self.faces['left'], self.faces['back'], \
            self.faces['right'] = self.faces['right'], self.faces['front'], \
            self.faces['left'], self.faces['back']
        else:
            self.faces['front'], self.faces['left'], self.faces['back'], \
            self.faces['right'] = self.faces['left'], self.faces['back'], \
            self.faces['right'], self.faces['front']
            
    def show_right_face(self, top, front):
        if front == self.faces['back']:
            if top == self.faces['top']:
                self._revolve('R')
                self._revolve('R')
            else:
                self.roll('S')
                self.roll('S')
        elif front == self.faces['top']:
            self.roll('S')
        elif front == self.faces['right']:
            self._revolve('R')
        elif front == self.faces['left']:
            self._revolve('L')
        elif front == self.faces['bottom']:
            self.roll('N')
        if top == self.faces['bottom']:
            self.roll('E')
            self.roll('E')
        elif top == self.faces['right']:
            self.roll('W')
        elif top == self.faces['left']:
            self.roll('E')
        print(self.faces['right'])


import sys

face_vals = sys.stdin.readline()

dice = Dice(face_vals)

n = int(sys.stdin.readline())

for i in range(n):
    top, front = sys.stdin.readline().split()
    dice.show_right_face(top, front)

Solution (Python):
class Dice(object):
    def __init__(self, top, south, east, west, north, bottom):
        self.top = top
        self.south = south
        self.east = east
        self.west = west
        self.north = north
        self.bottom = bottom

    def get_face(self, face):
        if face == 'top':
            return self.top
        elif face == 'south':
            return self.south
        elif face == 'east':
            return self.east
        elif face == 'west':
            return self.west
        elif face == 'north':
            return self.north
        elif face == 'bottom':
            return self.bottom

    def rotate(self, directions):
        for direction in directions:
            if direction == 'S':
                self.prev_top = self.top
                self.top = self.north
                self.north = self.bottom
                self.bottom = self.south
                self.south = self.prev_top
            elif direction == 'E':
                self.prev_top = self.top
                self.top = self.west
                self.west = self.bottom
                self.bottom = self.east
                self.east = self.prev_top
            elif direction == 'W':
                self.prev_top = self.top
                self.top = self.east
                self.east = self.bottom
                self.bottom = self.west
                self.west = self.prev_top
            elif direction == 'N':
                self.prev_top = self.top
                self.top = self.south
                self.south = self.bottom
                self.bottom = self.north
                self.north = self.prev_top

dice = Dice(*map(int,input().split()))
n = int(input())
for _ in range(n):
  top, south = map(int, input().split())
  
  for direction in 'NNNEEE':
    if dice.get_face('top') == top:
        break
    dice.rotate(direction)

  
  for directions in ['NES']*3:
    if dice.get_face('south') == south:
        break
    for direction in directions:
        dice.rotate(direction)
  print(dice.get_face('east'))

Solution (Python):



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]]




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
        
    elif top in werolls and front in werolls:
        topps = werolls.index(top)
        frontps = werolls.index(front)
        tmprolls = nsrolls
        listmode = 1
        
    elif top in nsrolls2 and front in nsrolls2:
        topps = nsrolls2.index(top)
        frontps = nsrolls2.index(front)
        tmprolls = werolls2
        
        listmode = 1
    elif top in werolls2 and front in werolls2:
        topps = werolls2.index(top)
        frontps = werolls2.index(front)
        tmprolls = nsrolls2
        listmode = 0
        

    
    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]

Solution (Python):
d, q = list(map(int, input().split())), int(input())
dd = {v: k for k, v in enumerate(d)}
dms = ((1, 2, 4, 3), (0, 3, 5, 2), (0, 1, 5, 4), (0, 4, 5, 1), (0, 2, 5, 3), (1, 3, 4, 2))

while q:
    t, f = map(int, input().split())
    dm = dms[dd[t]]
    print(d[dm[(dm.index(dd[f]) + 1) % 4]])
    q -= 1

Solution (Python):
import copy
class Dice(object):
  def __init__(self, List):
    self.face = List

  def n_spin(self, a_List):
    List = copy.copy(a_List)
    temp = List[0]
    List[0] = List[1]
    List[1] = List[5]
    List[5] = List[4]
    List[4] = temp
    return List

  def s_spin(self, a_List):
    List = copy.copy(a_List)
    temp = List[0]
    List[0] = List[4]
    List[4] = List[5]
    List[5] = List[1]
    List[1] = temp
    return List

  def e_spin(self, a_List):
    List = copy.copy(a_List)
    temp = List[0]
    List[0] = List[3]
    List[3] = List[5]
    List[5] = List[2]
    List[2] = temp
    return List

  def w_spin(self, a_List):
    List = copy.copy(a_List)
    temp = List[0]
    List[0] = List[2]
    List[2] = List[5]
    List[5] = List[3]
    List[3] = temp
    return List

dice = Dice(map(int, raw_input().split()))
emer = copy.copy(dice.face)
face_storage = []
face_storage.append(emer)
q = input()
for a in range(q):
  question = map(int, raw_input().split())
  for a in range(24):
    face_storage.append(dice.n_spin(face_storage[a]))
    face_storage.append(dice.s_spin(face_storage[a]))
    face_storage.append(dice.w_spin(face_storage[a]))
    face_storage.append(dice.e_spin(face_storage[a]))
  for c in face_storage:
    if c[0] == question[0] and c[1] == question[1]:
      print c[2]
      break

Solution (Python):
class Dice():
    def __init__(self, nums):
        self.nums = nums
        self.top, self.front, self.right = 0, 1, 2

    def move(self, op):
        for c in op:
            if c == 'N':
                self.top, self.front = self.front, 5 - self.top
            elif c == 'S':
                self.top, self.front = 5 - self.front, self.top
            elif c == 'E':
                self.top, self.right = 5 - self.right, self.top
            else:
                self.top, self.right = self.right, 5 - self.top

    def getRight(self, t, f):
        top, front = self.nums.index(t), self.nums.index(f)
        for c in "NNNNEEE":
            self.move(c)
            if top == self.top:
                break
        while True:
            self.move("ESW")
            if front == self.front:
                return self.nums[self.right]

dice = Dice([int(n) for n in input().split()])
n = int(input())
for _ in range(n):
    t, f = [int(x) for x in input().split()]
    print(dice.getRight(t, f))

Solution (Python):


class dice_class:
    def __init__(self, top, front, right, left, back, bottom):
        self.top    =   top
        self.front  =   front
        self.right  =   right
        self.left   =   left
        self.back   =   back
        self.bottom =   bottom

    def sut(self, t):  
        while True:
            if self.top == t:
                break
            else:
                dice.roll('E')
            if self.top == t:
                break
            else:
                dice.roll('N')

    def suf(self, f):   
        while True:
            if self.front == f:
                break
            else:
                dice.roll("SEN")

    def roll(self, s):
        for i in s:
            if i == 'E':
                self.rollE()
            elif i == 'N':
                self.rollN()
            elif i == 'S':
                self.rollS()
            elif i == 'W':
                self.rollW()

    def rollE(self):
        tmp         =   self.top
        self.top    =   self.left
        self.left   =   self.bottom
        self.bottom =   self.right
        self.right  =   tmp

    def rollN(self):
        tmp         =   self.top
        self.top    =   self.front
        self.front  =   self.bottom
        self.bottom =   self.back
        self.back   =   tmp

    def rollS(self):
        tmp         =   self.top
        self.top    =   self.back
        self.back   =   self.bottom
        self.bottom =   self.front
        self.front  =   tmp

    def rollW(self):
        tmp         =   self.top
        self.top    =   self.right
        self.right  =   self.bottom
        self.bottom =   self.left
        self.left   =   tmp

if __name__ == "__main__":
    a = map(int, raw_input().split())
    dice = dice_class(a[0], a[1], a[2], a[3], a[4], a[5])
    n = int(raw_input())
    for i in range(n):
        t, f = map(int, raw_input().split())
        dice.sut(t)
        dice.suf(f)
        print dice.right

Solution (Python):


class dice_class:
    def __init__(self, list):
        self.num = list

    def sut(self, top):  
        while True:
            if self.num[0] == top:
                break
            else:
                self.roll('E')
            if self.num[0] == top:
                break
            else:
                self.roll('N')

    def suf(self, front):   
        while True:
            if self.num[1] == front:
                break
            else:
                self.roll('SEN')

    def roll(self, s):
        for i in s:
            if i == 'E':
                self.rollE()
            elif i == 'N':
                self.rollN()
            elif i == 'S':
                self.rollS()
            elif i == 'W':
                self.rollW()

    def rollE(self):
        self.num = [self.num[3], self.num[1], self.num[0], self.num[5], self.num[4], self.num[2]]

    def rollN(self):
        self.num = [self.num[1], self.num[5], self.num[2], self.num[3], self.num[0], self.num[4]]

    def rollS(self):
        self.num = [self.num[4], self.num[0], self.num[2], self.num[3], self.num[5], self.num[1]]

    def rollW(self):
        self.num = [self.num[2], self.num[1], self.num[5], self.num[0], self.num[4], self.num[3]]


if __name__ == "__main__":
    dice = dice_class(map(int, raw_input().split()))
    n = int(raw_input())
    for i in range(n):
        top, front = map(int, raw_input().split())
        dice.sut(top)
        dice.suf(front)
        print dice.num[2]

Solution (Python):
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)

Solution (Python):
def move_dice(d, dir):
    if(dir == 'N'):
        d[0], d[1], d[4], d[5] = d[1], d[5], d[0], d[4]
    if(dir == 'S'):
        d[0], d[1], d[4], d[5] = d[4], d[0], d[5], d[1]
    if(dir == 'E'):
        d[0], d[2], d[3], d[5] = d[3], d[0], d[5], d[2]
    if(dir == 'W'):
        d[0], d[2], d[3], d[5] = d[2], d[5], d[0], d[3]

def rotate_right_dice(d):
    d[1], d[2], d[3], d[4] = d[3], d[1], d[4], d[2]

def to_up_dice(d, num):
    num_index = d.index(num)
    if(num_index==2 or num_index==3):
        move_dice(d, 'E')
    while(d[0] != num):
        move_dice(d, 'N')

def to_front_dice(d, num):
    while(d[1] != num):
        rotate_right_dice(d)

dice = list(map(int, input().split()))
q = int(input())
for _ in range(q):
    up, front = map(int, input().split())
    to_up_dice(dice, up)
    to_front_dice(dice, front)
    print(dice[2])

Solution (Python):
import random

class Dice:
    def __init__(self, labels):
        self.labels = labels

    def north(self):
        self.change([2, 6, 3, 4, 1, 5])

    def south(self):
        self.change([5, 1, 3, 4, 6, 2])

    def east(self):
        self.change([4, 2, 1, 6, 5, 3])

    def west(self):
        self.change([3, 2, 6, 1, 5, 4])

    def change(self, convert):
        result = []
        for i in range(6):
            result.append(self.labels[convert[i] - 1])

        self.labels = result


dice = Dice(list(map(int, input().split())))
for i in range(int(input())):
    up, front = map(int, input().split())

    while(True):
        direction = random.randint(0, 3)
        if direction == 0:
            dice.north()
        if direction == 1:
            dice.south()
        if direction == 2:
            dice.east()
        if direction == 3:
            dice.west()

        if dice.labels[0] == up and dice.labels[1] == front:
            print(dice.labels[2])
            break

Solution (Python):



class Dice:
    def __init__(self):
        self.top = 1
        self.front = 2
        self.right = 3
        self.left = 4
        self.back = 5
        self.bottom = 6
    
    def turnSouth(self):
        temp = self.top
        self.top = self.back
        self.back = self.bottom
        self.bottom = self.front
        self.front = temp
        return self
    
    def turnEast(self):
        temp = self.top
        self.top = self.left
        self.left = self.bottom
        self.bottom = self.right
        self.right = temp
        return self
    
    def turnWest(self):
        temp = self.top
        self.top = self.right
        self.right = self.bottom
        self.bottom = self.left
        self.left = temp
        return self

    
    def turnNorth(self):
        temp = self.top
        self.top = self.front
        self.front = self.bottom
        self.bottom = self.back
        self.back = temp
        return self
    
    def turnRight(self):
        temp = self.front
        self.front = self.right
        self.right = self.back
        self.back = self.left
        self.left = temp
        return self
    
    def turnRight(self):
        temp = self.front
        self.front = self.left
        self.left = self.back
        self.back = self.right
        self.right = temp
        return self


myDice = Dice()

myDice.top, myDice.front, myDice.right, myDice.left, myDice.back, myDice.bottom = input().split()

q = int(input())


for i in range(0, q):
    
    question = list(input())
    question = "".join(question).split()
    
    for j in range(0, 3):
        if question[0] == myDice.top:
            break
        myDice.turnNorth()
    
    for j in range(0, 3):
        if question[0] == myDice.top:
            break
        myDice.turnEast()
    
    for j in range(0, 3):
        if question[1] == myDice.front:
            break
        myDice.turnRight()
    print("{0}".format(myDice.right))