Problem:
Dice II Construct a dice from a given sequence of integers in the same way as Dice I . You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I . Write a program to print an integer on the right side face.   Input In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given. In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively. Output For each question, print the integer on the right side face. Constraints  $0 \leq $ the integer assigned to a face $ \leq 100$  The integers are all different  $1 \leq q \leq 24$  Sample Input 1 2 3 4 5 6
3
6 5
1 3
3 2 Sample Output 3
5
6

Solution (Python):
class Dice:
    def __init__(self, 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())

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

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

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

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

Solution (Python):
class Dice:
    
    def __init__(self,ls):
        self.a = ls[1-1] 
        self.b = ls[2-1] 
        self.c = ls[3-1] 
        self.d = ls[4-1] 
        self.e = ls[5-1] 
        self.f = ls[6-1] 
    
    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

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

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

Solution (Python):


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)

Solution (Python):


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)

Solution (Python):


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)

Solution (Python):


import time
import sys
import io
import re
import math
import itertools



mod=1000000007

pi=3.141592653589
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]

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)

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

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

Solution (Python):



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]

Solution (Python):



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

Solution (Python):



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

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

Solution (Python):
axis_1 = '0154' * 2  
axis_2 = '0253' * 2  
axis_3 = '3124' * 2  

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)

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