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



Sample input:
1 2 3 4 5 6
3
6 5
1 3
3 2


Sample output:
3
5
6

Submission status: Accepted

Solution code:
# coding=utf-8


class Dice(object):

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

    def getLabel(self, i):
        return self.label[i - 1]

    def _rotateS(self):
        self.label = [self.getLabel(i) for i in [5, 1, 3, 4, 6, 2]]

    def _rotateN(self):
        self.label = [self.getLabel(i) for i in [2, 6, 3, 4, 1, 5]]

    def _rotateE(self):
        self.label = [self.getLabel(i) for i in [4, 2, 1, 6, 5, 3]]

    def _rotateW(self):
        self.label = [self.getLabel(i) for i in [3, 2, 6, 1, 5, 4]]

    def _spinPos(self):
        self.label = [self.getLabel(i) for i in [1, 4, 2, 5, 3, 6]]

    def _spinNeg(self):
        self.label = [self.getLabel(i) for i in [1, 3, 5, 2, 4, 6]]

    def rotate(self, rotates):
        for r in rotates:
            if r == 'S':
                self._rotateS()
            elif r == 'N':
                self._rotateN()
            elif r == 'E':
                self._rotateE()
            elif r == 'W':
                self._rotateW()
            elif r == 'T':
                self._spinPos()
            elif r == 'B':
                self._spinNeg()

    def match(self, top, front):
        iTop = self.label.index(top) + 1
        topRot = {1: '', 2: 'N', 3: 'W', 4: 'E', 5: 'S', 6: 'SS'}
        self.rotate(topRot[iTop])

        iFront = self.label.index(front) + 1
        frontRot = {2: '', 3: 'B', 4: 'T', 5: 'TT'}
        self.rotate(frontRot[iFront])


def main():
    d = Dice(map(int, raw_input().split()))
    n = input()
    for _ in xrange(n):
        top, front = map(int, raw_input().split())
        d.match(top, front)
        print d.getLabel(3)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
class Dice:

    def __init__(self, faces):
        self.faces = tuple(faces)

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

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

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

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

    def number(self, face_id):
        return self.faces[face_id - 1]


dice = Dice(list(map(int, input().split())))
n = int(input())
for i in range(n):
    (face1, face2) = map(int, input().split())
    if face2 == dice.number(3) or face2 == dice.number(4):
        dice.roll_west()
    while dice.number(2) != face2:
        dice.roll_north()
    while dice.number(1) != face1:
        dice.roll_west()
    print(dice.number(3))

Submission status: Accepted

Solution code:
axis_1 = '0154' * 2  # 2 -> 3
axis_2 = '0253' * 2  # 4 -> 1
axis_3 = '3124' * 2  # 0 -> 5

right = [axis_3, axis_2[::-1], axis_1, axis_1[::-1], axis_2, axis_3[::-1], ]
a = list(map(int, input().split()))
n = int(input())
for _ in range(n):
    top, front = map(a.index, map(int, input().split()))
    x = '%d%d' % (top, front)
    for i, axis in enumerate(right):
        if x in axis:
            print(a[i])
            break
    else:
        print('------------------')
        print(x)

Submission status: Accepted

Solution code:
import random

class Dice:
  def __init__(self, list = map(str, range(1, 7))):
    self.top = list[0]
    self.front = list[1]
    self.right = list[2]
    self.left = list[3]
    self.back = list[4]
    self.bottom = list[5]

  def print_all(self):
    print "top = " + self.top
    print "front = " + self.front
    print "right = " + self.right
    print "left = " + self.left
    print "back = " + self.back
    print "bottom = " + self.bottom

  def roll_N(self):
    temp = self.top
    self.top = self.front
    self.front = self.bottom
    self.bottom = self.back
    self.back = temp

  def roll_S(self):
    temp = self.top
    self.top = self.back
    self.back = self.bottom
    self.bottom = self.front
    self.front = temp

  def roll_W(self):
    temp = self.top
    self.top = self.right
    self.right = self.bottom
    self.bottom = self.left
    self.left = temp

  def roll_E(self):
    temp = self.top
    self.top = self.left
    self.left = self.bottom
    self.bottom = self.right
    self.right = temp

  def random_roll(self):
    ram = random.randint(1, 4)
    if ram == 1:
      self.roll_E()
    elif ram == 2:
      self.roll_N()
    elif ram == 3:
      self.roll_S()
    else:
      self.roll_W()

my_dice = Dice(raw_input().split(" "))
q = int(raw_input())

for i in xrange(q):
  input_dice = raw_input().split(" ")
  while True:
    if my_dice.top == input_dice[0] and my_dice.front == input_dice[1]:
      break
    else:
      my_dice.random_roll()
  print my_dice.right

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

Submission status: Accepted

Solution code:
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] )

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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())
  # find top face
  for direction in 'NNNEEE':
    if dice.get_face('top') == top:
        break
    dice.rotate(direction)

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

Submission status: Accepted

Solution code:
#-*- 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]

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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):  #set_up_top
        while True:
            if self.top == t:
                break
            else:
                dice.roll('E')
            if self.top == t:
                break
            else:
                dice.roll('N')

    def suf(self, f):   #set_up_front
        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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

    def sut(self, top):  #set_up_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):   #set_up_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]

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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