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:
def roll(l, command):
    '''
    return rolled list

    l : string list
    command: string
    '''
    res = []
    i = -1
    if command =='N':
        res = [l[i+2], l[i+6], l[i+3], l[i+4], l[i+1], l[i+5]]
    if command =='S':
        res = [l[i+5], l[i+1], l[i+3], l[i+4], l[i+6], l[i+2]]
    if command =='E':
        res = [l[i+4], l[i+2], l[i+1], l[i+6], l[i+5], l[i+3]]
    if command =='W':
        res = [l[i+3], l[i+2], l[i+6], l[i+1], l[i+5], l[i+4]]

    return res

def spin(l):
    '''
    return right sppined list

    l : string list
    '''

    i = -1
    res = [l[i+1], l[i+3], l[i+5], l[i+2], l[i+4], l[i+6]]

    return res

def check_all(upper, front, faces):
    orig_faces = faces[:]

    #change upper face
    for i in range(6):
        if 1 <= i <= 3:
            faces = roll(faces, "N")
        elif i == 4:
            pass
            faces = orig_faces[:]
            faces = roll(faces, "W")
        elif i == 5:
            faces = orig_faces[:]
            faces = roll(faces, "E")


        # spin 4 times
        for _ in range(4): #spin 3 times
            faces = spin(faces)
            if (faces[0] == upper and (faces[1] == front)):
                return faces[2]

if __name__ == "__main__":
    faces = input().split()
    n = int(input())

    for _ in range(n):

        question = list(input().split())
        answer = check_all(question[0], question[1], faces)

        print(answer)

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, s1, s2, s3, s4, s5, s6):
        self.s1 = s1
        self.s2 = s2
        self.s3 = s3
        self.s4 = s4
        self.s5 = s5
        self.s6 = s6
    def east(self):
        prev_s6 = self.s6
        self.s6 = self.s3
        self.s3 = self.s1
        self.s1 = self.s4
        self.s4 = prev_s6
    def west(self):
        prev_s6 = self.s6
        self.s6 = self.s4
        self.s4 = self.s1
        self.s1 = self.s3
        self.s3 = prev_s6
    def north(self):
        prev_s6 = self.s6
        self.s6 = self.s5
        self.s5 = self.s1
        self.s1 = self.s2
        self.s2 = prev_s6
    def south(self):
        prev_s6 = self.s6
        self.s6 = self.s2
        self.s2 = self.s1
        self.s1 = self.s5
        self.s5 = prev_s6
    def rotate(self):
        prev_s2 = self.s2
        self.s2 = self.s4
        self.s4 = self.s5
        self.s5 = self.s3
        self.s3 = prev_s2
    def top(self):
        return self.s1
    def front(self):
        return self.s2
    def right(self):
        return self.s3

s1, s2, s3, s4, s5, s6 = map(int, input().split())
dice = Dice(s1, s2, s3, s4, s5, s6)
q = int(input())
for i in range(q):
    t, f = map(int, input().split())
    flag = False
    for j in range(6):
        if j % 2 == 0:
            dice.north()
        else:
            dice.west()

        for k in range(4):
            dice.rotate()
            if dice.top() == t and dice.front() == f:
                flag = True
                break
        if flag:
            break
    print(dice.right())

Submission status: Accepted

Solution code:
def R(dice,U,S):
  def f(x): return str(dice.index(x))
  PAT=["12431","03520","01540","04510","02530","13421"]
  it=f(U)+f(S)
  for i in range(6):
    if PAT[i].find(it)>=0: return dice[i]

dice = map(int,raw_input().split())
for _ in [0]*int(raw_input()):
  U,S=map(int,raw_input().split())
  print R(dice,U,S)

Submission status: Accepted

Solution code:
IDX_TABLE=\
{1:[4,2,3,5],
 2:[1,4,6,3],
 3:[1,2,6,5],
 4:[1,5,6,2],
 5:[4,1,3,6],
 6:[3,2,4,5]}

nums = ["dummy"] + [int(x) for x in raw_input().split(" ")]
n = int(raw_input())

for i in range(n):
    up, front = tuple([int(x) for x in raw_input().split(" ")])
    up_idx = nums.index(up)
    front_idx = nums.index(front)
    ring_list = IDX_TABLE[up_idx]
    ring_idx = ring_list.index(front_idx)

    print nums[ring_list[(ring_idx+1) % 4]]

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, data):
        self.data = data
    
    def move(self, direction):
        if direction == 'E':
            self.data[0], self.data[3], self.data[5], self.data[2] = \
                self.data[3], self.data[5], self.data[2], self.data[0]
        elif direction == 'N':
            self.data[0], self.data[4], self.data[5], self.data[1] = \
                self.data[1], self.data[0], self.data[4], self.data[5]
        elif direction == 'S':
            self.data[0], self.data[1], self.data[5], self.data[4] = \
                self.data[4], self.data[0], self.data[1], self.data[5]
        elif direction == 'W':
            self.data[0], self.data[2], self.data[5], self.data[3] = \
                self.data[2], self.data[5], self.data[3], self.data[0]
        elif direction == 'R':
            self.data[1], self.data[2], self.data[4], self.data[3] = \
                self.data[3], self.data[1], self.data[2], self.data[4]
        elif direction == 'L':
            self.data[1], self.data[2], self.data[4], self.data[3] = \
                self.data[2], self.data[4], self.data[3], self.data[1]

    def moveTopTo(self, target):
        for a in range(4):
            if self.data[0] == target:
                break
            self.move('W')
        if self.data[4] == target:
            self.move('S')
        elif self.data[1] == target:
            self.move('N')
    
    def moveFrontTo(self, target):
        for a in range(4):
            if self.data[1] == target:
                break
            self.move('R')

    def getTop(self):
        return self.data[0]
    
    def getRight(self):
        return self.data[2]

dice = Dice(input().split())
c = int(input())

for a in range(c):
    (t, f) = input().split()
    dice.moveTopTo(t)
    dice.moveFrontTo(f)
    print(dice.getRight())

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, data):
        self.data = data

    def move(self, direction):
        if direction == 'E':
            self.data[0],self.data[3], self.data[5], self.data[2] = \
                self.data[3],self.data[5], self.data[2], self.data[0] 
        elif direction == 'N':
            self.data[0],self.data[1], self.data[5], self.data[4] = \
                self.data[1],self.data[5], self.data[4], self.data[0]
        elif direction == 'S':
            self.data[0],self.data[1], self.data[5], self.data[4] = \
                self.data[4],self.data[0], self.data[1], self.data[5]
        elif direction == 'W':
            self.data[0],self.data[2], self.data[5], self.data[3] = \
                self.data[2],self.data[5], self.data[3], self.data[0]
        elif direction == 'R':
            self.data[1],self.data[2], self.data[4], self.data[3] = \
                self.data[2],self.data[4], self.data[3], self.data[1]


    def getTop(self):
        return self.data[0]

    def getRight(self, top, front):
        if self.data[0] != top:
            for i in range(4):
                if self.data[0] == top:
                    break
                self.move('E')
            for i in range(4):
                if self.data[0] == top:
                    break
                self.move('N')

        if self.data[1] != front:
            for i in range(3):
                self.move('R')
                if self.data[1] == front and self.data[0] == top:
                    break
        return self.data[2]

dice = Dice(input().split())
hoge = int(input())
fuga = []
for i in range(hoge):
    (x,y) = [j for j in input().split()]
    fuga += [dice.getRight(x,y)]
for i in fuga:
    print(i)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, data):
        self.data = data

    def move(self, direction):
        if direction == 'E':
            self.data[0],self.data[3],self.data[5],self.data[2] = \
                    self.data[3],self.data[5],self.data[2],self.data[0]
        elif direction == 'N':
            self.data[0],self.data[4],self.data[5],self.data[1] = \
                    self.data[1],self.data[0],self.data[4],self.data[5]
        elif direction == 'S':
            self.data[0],self.data[1],self.data[5],self.data[4] = \
                    self.data[4],self.data[0],self.data[1],self.data[5]
        elif direction == 'W':
             self.data[0],self.data[2],self.data[5],self.data[3] = \
                    self.data[2],self.data[5],self.data[3],self.data[0]
        elif direction == 'LEFT':
            self.data[2],self.data[4],self.data[3],self.data[1] = \
                    self.data[1], self.data[2],self.data[4],self.data[3]


    def getTop(self):
        return self.data[0]

    def getRight(self):
        return self.data[2]

    def moveTopTo(self, target):
        for i in range(4):
            if self.data[0] == target:
                break
            self.move('W')

            if self.data[1] == target:
                self.move('S')
            elif self.data[4] == target:
                self.move('N')

    def moveFrontTo(self, target):
        for i in range(4):
            if self.data[1] == target:
                break
            self.move('LEFT')

dice = Dice(input().split())
count = int(input())
result = []

for i in range(count):
    (top, front) = [i for i in input().split()]
    dice.moveTopTo(top)
    dice.moveFrontTo(front)
    result.append(dice.getRight())

for s in result:
    print(s)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, data):
        self.data = data

    def move(self, direction):
        if direction == 'E':
            self.data[0],self.data[3],self.data[5],self.data[2] = \
                    self.data[3],self.data[5],self.data[2],self.data[0]
        elif direction == 'N':
            self.data[0],self.data[4],self.data[5],self.data[1] = \
                    self.data[1],self.data[0],self.data[4],self.data[5]
        elif direction == 'S':
            self.data[0],self.data[1],self.data[5],self.data[4] = \
                    self.data[4],self.data[0],self.data[1],self.data[5]
        elif direction == 'W':
             self.data[0],self.data[2],self.data[5],self.data[3] = \
                    self.data[2],self.data[5],self.data[3],self.data[0]
        elif direction == 'LEFT':
            self.data[2],self.data[4],self.data[3],self.data[1] = \
                    self.data[1], self.data[2],self.data[4],self.data[3]


    def getTop(self):
        return self.data[0]

    def getRight(self):
        return self.data[2]

    def moveTopTo(self, target):
        for i in range(4):
            if self.data[0] == target:
                break
            self.move('W')

            if self.data[1] == target:
                self.move('S')
            elif self.data[4] == target:
                self.move('N')

    def moveFrontTo(self, target):
        for i in range(4):
            if self.data[1] == target:
                break
            self.move('LEFT')

dice = Dice(input().split())
count = int(input())
result = []

for i in range(count):
    (top, front) = [i for i in input().split()]
    dice.moveTopTo(top)
    dice.moveFrontTo(front)
    result.append(dice.getRight())

[print(i) for i in result]

Submission status: Accepted

Solution code:
import sys

class Dice(object):
    def __init__(self, dice):
        self.__dice = dice

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

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

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

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

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

    def __str__(self):
        return str(self.__dice)


dice = Dice([int(i) for i in sys.stdin.readline().split()])
n = int(sys.stdin.readline())
for i in range(n):
    (face1, face2) = [int(a) for a in sys.stdin.readline().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:
class Dice:
    
    def __init__(self,ls):
        self.a = ls[1-1] # top
        self.b = ls[2-1] # front
        self.c = ls[3-1] # right
        self.d = ls[4-1] # left
        self.e = ls[5-1] # back
        self.f = ls[6-1] # bottom
    
    def E(self):
        self.a, self.c, self.f, self.d = self.d, self.a, self.c, self.f
    def W(self):
        self.a, self.c, self.f, self.d = self.c, self.f, self.d, self.a
    def S(self):
        self.a, self.b, self.f, self.e = self.e, self.a, self.b, self.f
    def N(self):
        self.a, self.b, self.f, self.e = self.b, self.f, self.e, self.a   
                        
import random
ls = map(int,raw_input().split())
q = int(raw_input())
for i in range(q):
    dice = Dice(ls)
    a,b = map(int,raw_input().split())
    while dice.a != a or dice.b != b:
        i = random.choice([1,2,3,4])
        if i == 1: dice.E()
        elif i == 2: dice.W()
        elif i == 3 : dice.S()
        elif i == 4 : dice.N()
    print dice.c

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, nums):
        self.top, self.front, self.right, self.left, self.back, self.bottom = nums

    def roll(self, directions):
        if directions == 'E':
            self.top, self.right, self.left, self.bottom = self.left, self.top, self.bottom, self.right
            return

        if directions == 'N':
            self.top, self.front, self.back, self.bottom = self.front, self.bottom, self.top, self.back
            return

        if directions == 'S':
            self.top, self.front, self.back, self.bottom = self.back, self.top, self.bottom, self.front
            return

        if directions == 'W':
            self.top, self.right, self.left, self.bottom = self.right, self.bottom, self.top, self.left
            return

        self.roll(directions[0])
        self.roll(directions[1:])

if __name__ == '__main__':
    nums = [int(i) for i in input().split()]
    d = Dice(nums)
    q = int(input())
    for i in range(q):
        top, front = [int(x) for x in input().split()]

        if(top == d.front):
            d.roll('N')
        elif(top == d.right):
            d.roll('W')
        elif(top == d.left):
            d.roll('E')
        elif(top == d.back):
            d.roll('S')
        elif(top == d.bottom):
            d.roll('NN')

        if(front == d.right):
            d.roll('WSE')
        elif(front == d.left):
            d.roll('ESW')
        elif(front == d.back):
            d.roll('SENWSE')

        print(d.right)

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, nums):
        self.top, self.front, self.right, self.left, self.back, self.bottom = nums

    def roll(self, directions):
        if directions == 'E':
            self.top, self.right, self.left, self.bottom = self.left, self.top, self.bottom, self.right
            return

        if directions == 'N':
            self.top, self.front, self.back, self.bottom = self.front, self.bottom, self.top, self.back
            return

        if directions == 'S':
            self.top, self.front, self.back, self.bottom = self.back, self.top, self.bottom, self.front
            return

        if directions == 'W':
            self.top, self.right, self.left, self.bottom = self.right, self.bottom, self.top, self.left
            return

        self.roll(directions[0])
        self.roll(directions[1:])

if __name__ == '__main__':
    nums = [int(i) for i in input().split()]
    d = Dice(nums)
    q = int(input())
    for i in range(q):
        top, front = [int(x) for x in input().split()]

        if(front == d.top):
            d.roll('S')
        elif(front == d.right):
            d.roll('WS')
        elif(front == d.left):
            d.roll('ES')
        elif(front == d.back):
            d.roll('SS')
        elif(front == d.bottom):
            d.roll('N')

        if(top == d.right):
            d.roll('W')
        elif(top == d.left):
            d.roll('E')
        elif(top == d.bottom):
            d.roll('WW')

        print(d.right)

Submission status: Accepted

Solution code:
#coding:utf-8
#1_11_B 2015.4.20
class Dice:
    def __init__(self,ary):
        self.top = ary[0]
        self.south = ary[1]
        self.east = ary[2]
        self.west = ary[3]
        self.north = ary[4]
        self.bottom = ary[5]

    def roll_n(self):
        self.top , self.south , self.bottom , self.north = self.south , self.bottom , self.north , self.top

    def roll_e(self):
        self.top , self.west , self.bottom , self.east = self.west , self.bottom , self.east , self.top

    def roll(self):
        self.north , self.west , self.south , self.east = self.east , self.north , self.west , self.south

dice = Dice(input().split())
for i in range(int(input())):
    top,south = input().split()
    while dice.top != top or dice.south != south:
        dice.roll_n()
        if dice.top == top:
            while dice.south != south:
                dice.roll()
        elif dice.south == south:
            while dice.top != top:
                dice.roll_e()
    print(dice.east)

Submission status: Accepted

Solution code:
#coding:utf-8
#1_11_B 2015.4.20
class Dice:
    def __init__(self,ary):
        self.top = ary[0]
        self.south = ary[1]
        self.east = ary[2]
        self.west = ary[3]
        self.north = ary[4]
        self.bottom = ary[5]

    def roll_n(self):
        self.top , self.south , self.bottom , self.north = self.south , self.bottom , self.north , self.top

    def roll_e(self):
        self.top , self.west , self.bottom , self.east = self.west , self.bottom , self.east , self.top

    def twist(self):
        self.north , self.west , self.south , self.east = self.east , self.north , self.west , self.south

dice = Dice(input().split())
for i in range(int(input())):
    top,south = input().split()
    while dice.top != top or dice.south != south:
        dice.roll_n()
        if dice.top == top:
            while dice.south != south:
                dice.twist()
        elif dice.south == south:
            while dice.top != top:
                dice.roll_e()
    print(dice.east)

Submission status: Accepted

Solution code:
#coding:utf-8
#1_11_B 2015.4.20
class Dice:
    def __init__(self,ary):
        self.top , self.south , self.east , self.west , self.north ,self.bottom = ary

    def roll_n(self):
        self.top , self.south , self.bottom , self.north = self.south , self.bottom , self.north , self.top

    def roll_e(self):
        self.top , self.west , self.bottom , self.east = self.west , self.bottom , self.east , self.top

    def twist(self):
        self.north , self.west , self.south , self.east = self.east , self.north , self.west , self.south

dice = Dice(input().split())
for i in range(int(input())):
    top,south = input().split()
    while dice.top != top or dice.south != south:
        if dice.top == top:
            while dice.south != south:
                dice.twist()
        elif dice.south == south:
            while dice.top != top:
                dice.roll_e()
        else:
            dice.roll_n()
    print(dice.east)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
#start = time.clock()
l=map(int,raw_input().split())
n=int(raw_input())
ans=chk=0


def rolling(word,dice):
    if word=='S':
        dice[0],dice[1],dice[5],dice[4]=dice[4],dice[0],dice[1],dice[5]
    elif word=='N':
        dice[0],dice[1],dice[5],dice[4]=dice[1],dice[5],dice[4],dice[0]
    elif word=='W':
        dice[0],dice[2],dice[5],dice[3]=dice[2],dice[5],dice[3],dice[0]
    else:
        dice[0],dice[2],dice[5],dice[3]=dice[3],dice[0],dice[2],dice[5]
    return dice

def face(x,y,dice):
    if (x==dice[5] and y==dice[4])or(x==dice[4] and y==dice[0])or(x==dice[0] and y==dice[1])or(x==dice[1] and y==dice[5]):
        return dice[2]
    elif (x==dice[0] and y==dice[2])or(x==dice[2] and y==dice[5])or(x==dice[5] and y==dice[3])or(x==dice[3] and y==dice[0]):
        return dice[4]
    elif (y==dice[5] and x==dice[4])or(y==dice[4] and x==dice[0])or(y==dice[0] and x==dice[1])or(y==dice[1] and x==dice[5]):
        return dice[3]
    elif (y==dice[0] and x==dice[2])or(y==dice[2] and x==dice[5])or(y==dice[5] and x==dice[3])or(y==dice[3] and x==dice[0]):
        return dice[1]
    elif (x==dice[2] and y==dice[1])or(x==dice[1] and y==dice[3])or(x==dice[3] and y==dice[4])or(x==dice[4] and y==dice[2]):
        return dice[5]
    else:
        return dice[0]
        

for i in range(n):
    a,b=map(int,raw_input().split())
    print face(a,b,l)

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 raw_input( ).split( " " ) ]
dice = Dice( diceface )
q = int( raw_input( ) )
for i in range( q ):
	topnum, frontnum = [ int( val ) for val in raw_input( ).split( " " ) ]
	dice.rolltoTop( topnum )
	while dice.face[1] != frontnum:
		dice.roll( "M" )
		
	print( dice.face[2] )

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:
import random
class Dice:
    def __init__(self, top, front, right, left, back, bottom):
        self.top,   self.bottom = top, bottom
        self.right, self.left   = right, left
        self.front, self.back   = front, back

    def rot(self, d):
        if   d == "N":
            self.top, self.back, self.bottom, self.front = self.front, self.top, self.back, self.bottom
        elif d == "S":
            self.top, self.back, self.bottom, self.front = self.back, self.bottom, self.front, self.top
        elif d == "E":
            self.top, self.right, self.bottom, self.left = self.left, self.top, self.right, self.bottom
        elif d == "W":
            self.top, self.right, self.bottom, self.left = self.right, self.bottom, self.left, self.top

ref = {}
ls = map(int, raw_input().split())
dice = Dice(ls[0], ls[1], ls[2], ls[3], ls[4], ls[5])
for i in xrange(1000):
    d = "NSEW"[random.randint(0,3)]
    dice.rot(d)
    ref[(dice.top, dice.front)] = dice.right

for i in xrange(input()):
    a, b = map(int, raw_input().split())
    print ref[(a, b)]

Submission status: Accepted

Solution code:
# coding=utf-8


class Dice(object):

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

    def _rotateS(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s5, s1, s3, s4, s6, s2]

    def _rotateN(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s2, s6, s3, s4, s1, s5]

    def _rotateE(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s4, s2, s1, s6, s5, s3]

    def _rotateW(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s3, s2, s6, s1, s5, s4]

    def rotate(self, r):
        if r == 'S':
            self._rotateS()
        elif r == 'N':
            self._rotateN()
        elif r == 'E':
            self._rotateE()
        elif r == 'W':
            self._rotateW()
        else:
            print 'Cannot rotate into ' + r + 'direction.'

    def _spinPos(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s1, s4, s2, s5, s3, s6]

    def _spinNeg(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s1, s3, s5, s2, s4, s6]

    def getTopLabel(self):
        return self.label[0]

    def match(self, top, front):
        iTop = self.label.index(top) + 1
        if iTop == 1:
            pass
        elif iTop == 2:
            self._rotateN()
        elif iTop == 3:
            self._rotateW()
        elif iTop == 4:
            self._rotateE()
        elif iTop == 5:
            self._rotateS()
        else:
            self._rotateS()
            self._rotateS()

        iFront = self.label.index(front) + 1
        if iFront == 2:
            pass
        elif iFront == 3:
            self._spinNeg()
        elif iFront == 4:
            self._spinPos()
        elif iFront == 5:
            self._spinPos()
            self._spinPos()


if __name__ == '__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.label[2]

Submission status: Accepted

Solution code:
# coding=utf-8


class Dice(object):

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

    def _rotateS(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s5, s1, s3, s4, s6, s2]

    def _rotateN(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s2, s6, s3, s4, s1, s5]

    def _rotateE(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s4, s2, s1, s6, s5, s3]

    def _rotateW(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s3, s2, s6, s1, s5, s4]

    def _spinPos(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s1, s4, s2, s5, s3, s6]

    def _spinNeg(self):
        s1, s2, s3, s4, s5, s6 = self.label
        self.label = [s1, s3, s5, s2, s4, s6]

    def rotate(self, r):
        if r == 'S':
            self._rotateS()
        elif r == 'N':
            self._rotateN()
        elif r == 'E':
            self._rotateE()
        elif r == 'W':
            self._rotateW()
        elif r == 'SP':
            self._spinPos()
        elif r == 'SN':
            self._spinNeg()
        elif r == '2S':
            self._rotateS()
            self._rotateS()
        elif r == '2SP':
            self._spinPos()
            self._spinPos()

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

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

        iFront = self.label.index(front) + 1
        frontRot = {2: '', 3: 'SN', 4: 'SP', 5: '2SP'}
        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()