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:
class Dice:
    def __init__(self, f):
        self.f = f[:]

    def roll(self, s):
        for f in s:
            n = self.f[:]
            if f == 'E':
                t = n[0]
                n[0] = n[3]
                n[3] = n[5]
                n[5] = n[2]
                n[2] = t
            elif f == 'W':
                t = n[0]
                n[0] = n[2]
                n[2] = n[5]
                n[5] = n[3]
                n[3] = t
            elif f == 'N':
                t = n[0]
                n[0] = n[1]
                n[1] = n[5]
                n[5] = n[4]
                n[4] = t
            else:
                t = n[0]
                n[0] = n[4]
                n[4] = n[5]
                n[5] = n[1]
                n[1] = t
            self.f = n

    def to_top(self, index):
        if index == 1:
            self.roll('N')
        elif index == 2:
            self.roll('W')
        elif index == 3:
            self.roll('E')
        elif index == 4:
            self.roll('S')
        elif index == 5:
            self.roll('NN')

    def side_roll(self):
        t  = self.f[1]
        self.f[1] = self.f[2]
        self.f[2] = self.f[4]
        self.f[4] = self.f[3]
        self.f[3] = t

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

    def front(self):
        return self.f[1]

    def r(self):
        return self.f[2]

df = list(input().split())
q = int(input())
for _ in range(q):
    d = Dice(df)
    t, f = input().split()
    ti = d.f.index(t)
    fi = d.f.index(f)
    d.to_top(ti)
    while d.front() != f:
        d.side_roll()
    print(d.r())

Submission status: Accepted

Solution code:
def whatIsRight(u,f):	
    r = 0
    if u == 1:
        if f == 2:
            r = 3
        elif f == 3:
            r = 5
        elif f == 4:
            r = 2
        elif f == 5:
            r = 4
	
    elif u == 2:
        if f == 6:
            r = 3
        elif f == 3:
            r = 1
        elif f == 1:
            r = 4
        elif f == 4:
            r = 6

    elif u == 3:
        if f == 5:
            r = 1
        elif f == 1:
            r = 2
        elif f == 2:
            r = 6
        elif f == 6:
            r = 5
			
    elif u == 4:
        if f == 6:
            r = 2
        elif f == 2:
            r = 1
        elif f == 1:
            r = 5
        elif f == 5:
            r = 6 
			
    elif u == 5:
        if f == 3:
            r = 6
        elif f == 6:
            r = 4
        elif f == 4:
            r = 1
        elif f == 1:
            r = 3
		
    elif u == 6:
        if f == 5:
            r = 3
        elif f == 3:
            r = 2
        elif f == 2:
            r = 4
        elif f == 4:
            r = 5
    return r
			
n = list(map(int,input().split()))
N = int(input())

for i in range(N):
    m = list(map(int,input().split()))
    num = []

    for j in range(len(m)):
        for k in range(len(n)):
            if m[j] == n[k]:
                num.append(k+1)
                break
	
    print(n[whatIsRight(num[0],num[1]) -1])

Submission status: Accepted

Solution code:
dice = map(int, raw_input().split())
n = int(raw_input())
temp = [0 for i in range(2)]

for i in range(n):
    hint = map(int, raw_input().split())
    
    for j in range(len(dice)):
        if hint[0] == dice[j]:
            temp[0] = j
        elif hint[1] == dice[j]:
            temp[1] = j
    
    if (temp[0] == 1 and temp[1] == 2) or (temp[0] == 2 and temp[1] == 4) or (temp[0] == 4 and temp[1] == 3) or (temp[0] == 3 and temp[1] == 1):
        print dice[0]
    elif (temp[0] == 0 and temp[1] == 3) or (temp[0] == 3 and temp[1] == 5) or (temp[0] == 5 and temp[1] == 2) or (temp[0] == 2 and temp[1] == 0):
        print dice[1]
    elif (temp[0] == 0 and temp[1] == 1) or (temp[0] == 1 and temp[1] == 5) or (temp[0] == 5 and temp[1] == 4) or (temp[0] == 4 and temp[1] == 0):
        print dice[2]
    elif (temp[0] == 0 and temp[1] == 4) or (temp[0] == 4 and temp[1] == 5) or (temp[0] == 5 and temp[1] == 1) or (temp[0] == 1 and temp[1] == 0):
        print dice[3]
    elif (temp[0] == 0 and temp[1] == 2) or (temp[0] == 2 and temp[1] == 5) or (temp[0] == 5 and temp[1] == 3) or (temp[0] == 3 and temp[1] == 0):
        print dice[4]
    else:
        print dice[5]

Submission status: Accepted

Solution code:
class Dice(object):
    """Dice Class

    """

    def __init__(self, numbers):
        """

        Args:
            numbers:
        """
        self.numbers_inverse = {numbers[0]: 1, numbers[1]: 2, numbers[2]: 3, numbers[3]: 4, numbers[4]: 5,
                                numbers[5]: 6}
        self.numbers = {1: numbers[0], 2: numbers[1], 3: numbers[2], 4: numbers[3], 5: numbers[4], 6: numbers[5]}

        self.vertical = [self.numbers[1], self.numbers[2], self.numbers[6], self.numbers[5]]
        self.horizontal = [self.numbers[4], self.numbers[1], self.numbers[3], self.numbers[6]]

    def move_dice(self, s):
        """

        Args:
            s: move direction

        Returns:

        """
        if s == 'N':
            self.move_north()
        elif s == 'S':
            self.move_south()
        elif s == 'W':
            self.move_west()
        elif s == 'E':
            self.move_east()

    def move_south(self):
        """move this dice towered north
        """
        self.vertical = (self.vertical * 2)[3:7]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]

    def move_north(self):
        """move this dice towered south
        """
        self.vertical = (self.vertical * 2)[1:5]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]

    def move_east(self):
        """move this dice towered east
        """
        self.horizontal = (self.horizontal * 2)[3:7]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]

    def move_west(self):
        """move this dice towered west
        """
        self.horizontal = (self.horizontal * 2)[1:5]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]

    def get_top(self):
        return self.vertical[0]


dice = Dice([int(x) for x in raw_input().split()])
number_of_questions = int(raw_input())
counter = 0
while counter < number_of_questions:
    [top, front] = [int(x) for x in raw_input().split()]
    decision_list = "".join([str(x) for x in [dice.numbers_inverse[top], dice.numbers_inverse[front]]])
    if decision_list in "1265" * 2:
        print(dice.numbers[3])
    elif decision_list in "5621" * 2:
        print(dice.numbers[4])
    elif decision_list in "4235" * 2:
        print(dice.numbers[1])
    elif decision_list in "5324" * 2:
        print(dice.numbers[6])
    elif decision_list in "1364" * 2:
        print(dice.numbers[5])
    elif decision_list in "4631" * 2:
        print(dice.numbers[2])
    else:
        print("aho")
    counter += 1

Submission status: Accepted

Solution code:
class Dice(object):
    """Dice Class

    """

    def __init__(self, numbers):
        """

        Args:
            numbers:
        """
        self.numbers_inverse = {numbers[0]: 1, numbers[1]: 2, numbers[2]: 3, numbers[3]: 4, numbers[4]: 5,
                                numbers[5]: 6}
        self.numbers = {1: numbers[0], 2: numbers[1], 3: numbers[2], 4: numbers[3], 5: numbers[4], 6: numbers[5]}

        self.vertical = [self.numbers[1], self.numbers[2], self.numbers[6], self.numbers[5]]
        self.horizontal = [self.numbers[4], self.numbers[1], self.numbers[3], self.numbers[6]]

    def roll_dice(self, str):
        """

        Args:
            str: move direction

        Returns:

        """
        for s in str:
            if s == 'N':
                self.move_north()
            elif s == 'S':
                self.move_south()
            elif s == 'W':
                self.move_west()
            elif s == 'E':
                self.move_east()

    def set_top(self, value):
        """
        Args:
            value: target_value
        """
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("S")
            counter += 1
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("W")
            counter += 1

    def set_front(self, value):
        """
        Args:
            value: target value
        """
        counter = 0
        while counter < 4:
            if self.vertical[1] == value:
                self.map_values()
                return
            else:
                self.roll_dice("SWN")
            counter += 1

    def move_south(self):
        """move this dice towered north
        """
        self.vertical = (self.vertical * 2)[3:7]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]

    def move_north(self):
        """move this dice towered south
        """
        self.vertical = (self.vertical * 2)[1:5]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]

    def move_east(self):
        """move this dice towered east
        """
        self.horizontal = (self.horizontal * 2)[3:7]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]

    def move_west(self):
        """move this dice towered west
        """
        self.horizontal = (self.horizontal * 2)[1:5]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]

    def map_values(self):
        self.numbers[1] = self.vertical[0]
        self.numbers[2] = self.vertical[1]
        self.numbers[3] = self.horizontal[2]
        self.numbers[4] = self.horizontal[0]
        self.numbers[5] = self.horizontal[3]
        self.numbers[6] = self.vertical[3]

    def get_top(self):
        return self.vertical[0]


dice = Dice([int(x) for x in raw_input().split()])
number_of_questions = int(raw_input())
counter = 0
while counter < number_of_questions:
    [top, front] = [int(x) for x in raw_input().split()]
    dice.set_top(top)
    dice.set_front(front)
    print(dice.numbers[3])

    counter += 1

Submission status: Accepted

Solution code:
class Dice(object):
    """
    6??¢???????????????
    ?????¢???top, ?????¢???bottom???????????\??????4??¢??????????????? north, south, east, west ??¨?????¶???
    """
    def __init__(self, faces):
        # faces???????????????????????¨?????????????????????????????????
        # ???????????? top, south, east, west, north, bottom ????????§????´????????????????????????¨??????????????????????????§???top??????????????£????????????
        self.top = faces[0]
        self.south = faces[1]
        self.east = faces[2]
        self.west = faces[3]
        self.north = faces[4]
        self.bottom = faces[5]

    def rotate(self, dir):
        # ???????????????dir??????????????????????????¢?????????
        # ????????????N, S, E, W???????????????????????????(1??????)??§????????????
        if dir == 'N':
            temp = self.top
            self.top = self.south
            self.south = self.bottom
            self.bottom = self.north
            self.north = temp
        elif dir == 'S':
            temp = self.top
            self.top = self.north
            self.north = self.bottom
            self.bottom = self.south
            self.south = temp
        elif dir == 'E':
            temp = self.top
            self.top = self.west
            self.west = self.bottom
            self.bottom = self.east
            self.east = temp
        elif dir == 'W':
            temp = self.top
            self.top = self.east
            self.east = self.bottom
            self.bottom = self.west
            self.west = temp
        # self.print()

    def get_top_value(self):
        return self.top

    def get_east_value(self):
        return self.east

    def print(self):
        """ ???????????°??¨ """
        print('=' * 64)
        print('top: {0}'.format(self.top))
        print('south: {0}'.format(self.south))
        print('bottom: {0}'.format(self.bottom))
        print('north: {0}'.format(self.north))
        print('east: {0}'.format(self.east))
        print('west: {0}'.format(self.west))
        print('=' * 64)

    def spin(self, count=1):
        """???????????????top??¨bottom?????????????????????????????????????????????????????????????¨??????????90????????¢?????????
        ?????????????????§???1?????????????????????????????§?????§?????°??????????????????
        """
        for i in range(count):
            temp = self.south
            self.south = self.east
            self.east = self.north
            self.north = self.west
            self.west = temp

    def place(self, upper, front):
        # ?????¢(top)??¨??¨??¢(south)?????????????????????upper, front??¨???????????????????????????????????¢?????????
        # ????????? top == upper??¨????????????????????¢?????????
        if upper == self.top:
            pass
        elif upper == self.east:
            self.rotate('W')
        elif upper == self.west:
            self.rotate('E')
        elif upper == self.north:
            self.rotate('S')
        elif upper == self.south:
            self.rotate('N')
        else:
            self.rotate('N')
            self.rotate('N')
        # ????????????south == front ??¨????????????????????¢?????????
        while front != self.south:
            self.spin()


if __name__ == '__main__':
    # ??????????????\???
    dice_values = [int(x) for x in input().split(' ')]
    question_count = int(input())
    queries = []
    for i in range(question_count):
        data = [int(x) for x in input().split(' ')]
        queries.append(data)

    # ????????????????????????????????¢?????????
    d = Dice(dice_values)
    for r in queries:
        d.place(r[0], r[1])

        # ?????¢??????????¢????????????????
        print('{0}'.format(d.get_east_value()))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
dice = {v: k for k, v in enumerate(list(map(int, input().split())))}
adjacent = {k: v for k, v in enumerate(sorted(dice.keys()))}
q = int(input())
p = [(-1,2,4,1,3,-1),(3,-1,0,5,-1,2),(1,5,-1,-1,0,4),(4,0,-1,-1,5,1),(2,-1,5,0,-1,3),(-1,3,1,4,2,-1)]
for _ in range(q):
    top, front = map(int, input().split())
    x = dice[top]
    y = dice[front]
    print(adjacent[p[x][y]])

Submission status: Accepted

Solution code:
class Dice:
    rmap = [[2,3,5,4,2],
            [1,4,6,3,1],
            [1,2,6,5,1],
            [5,6,2,1,5],
            [3,6,4,1,3],
            [4,5,3,2,4] ]

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

    def lookup_right_side(self,u,s):
        index_upside = self.num.index(u)
        map_arr = self.rmap[index_upside]
        map_arr_dice_val = [ self.num[i-1] for i in map_arr ]
        index_next_map = map_arr_dice_val.index(s) + 1
        index_rightside = map_arr[index_next_map]
        return self.num[index_rightside-1]

spots = map(int,raw_input().split())
n = input()
questions = [ map(int,raw_input().split()) for _ in range(n) ]

dice = Dice(spots)
for i in questions:
    print dice.lookup_right_side(i[0],i[1])

Submission status: Accepted

Solution code:
d=list(map(int,input().split()))
q=int(input())

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

	def roll(self,com):
		a1,a2,a3,a4,a5,a6=self.d
		if com=="E":
			self.d = [a4,a2,a1,a6,a5,a3]
		elif com=="W":
			self.d = [a3,a2,a6,a1,a5,a4]
		elif com=="S":
			self.d = [a5,a1,a3,a4,a6,a2]
		elif com=="N":
			self.d = [a2,a6,a3,a4,a1,a5]

dice1=dice(d)
l=[]
for i in range(q):
	l.append(list(map(int,input().split())))

c=["E","W","S","N"]
import random
for i in l:
	while 1:
		if dice1.d[0]==i[0] and dice1.d[1]==i[1]:
			break 
		else:
			dice1.roll(c[random.randint(1,2)])
	print(dice1.d[2])

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self,num):
        self.f=num[0]
        self.s=num[1]
        self.e=num[2]
        self.w=num[3]
        self.n=num[4]
        self.b=num[5]
    def create(self,order):
        if order=='N':
            return [self.s,self.b,self.e,self.w,self.f,self.n]
        elif order=='S':
            return [self.n,self.f,self.e,self.w,self.b,self.s]

        elif order=='E':
            return [self.w,self.s,self.f,self.b,self.n,self.e]
        else:
            return [self.e,self.s,self.b,self.f,self.n,self.w]

    @classmethod
    def Dice_create(cls,num):
            return cls(num)
num=list(map(int,input().split()))
q=eval(input())
Q=[]
dice=Dice(num)
T={0:'N',1:'E',2:'S',3:'W'}
for i in range(q):
    Q.append(list(map(int,input().split())))
for i in range(q):
        while True:
            t=[dice.s,dice.w,dice.n,dice.e]
            if dice.f!=Q[i][0]:
                if dice.b!=Q[i][0]:#??´??¢????????¢?????\?????????
                    dice=Dice.Dice_create(dice.create(T[t.index(Q[i][0])]))
                else:#?????¢????????¢?????\?????????
                    dice=Dice.Dice_create(dice.create('N'))
            else:
                print(t[t.index(Q[i][1])-1])#?????¢?????£???????????¢?????\?????????
                break

Submission status: Accepted

Solution code:
pattern = {
           "12651":3,
           "15621":4,
           "13641":5,
           "14631":2,
           "23542":1,
           "24532":6,
          }

label = input().split()
l2d = {label[i]:str(i+1) for i in range(6)}

qn = int(input())
for i in range(qn):
    top, front = input().split()
    tf = l2d[top]+l2d[front]

    for p in pattern:
        if tf in p:
            print(label[pattern[p]-1])

Submission status: Accepted

Solution code:

class Dice:
    def __init__(self, label):
        self.d = label

    def roll(self, direction):
        if direction == "N":
            self.d[0], self.d[1], self.d[5], self.d[4] = self.d[1], self.d[5], self.d[4], self.d[0]
        elif direction == "S":
            self.d[0], self.d[1], self.d[5], self.d[4] = self.d[4], self.d[0], self.d[1], self.d[5]
        elif direction == "E":
            self.d[0], self.d[2], self.d[5], self.d[3] = self.d[3], self.d[0], self.d[2], self.d[5]
        elif direction == "W":
            self.d[0], self.d[2], self.d[5], self.d[3] = self.d[2], self.d[5], self.d[3], self.d[0]
        else:
            pass

label = [int(i) for i in input().split()]
q = int(input())

dice1 = Dice(label)

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

    # ??¨????????¶???(4 * 6)???????????????????????¢??????
    cmd = "N" * 4 + ("W" + "N" * 4) * 3 + "NE" + "N" * 4 + "EE" + "N" * 4 

    for i in cmd:
        dice1.roll(i)

        if dice1.d[1] == front and dice1.d[0] == top:
            print(dice1.d[2])
            break

Submission status: Accepted

Solution code:
A,B,C = (0,1,5,4),(1,2,4,3),(2,0,3,5)
D = (-1,3)

f = input().split()

for i in range(int(input())):
    t, s = map(f.index, input().split())
    if t in A and s in A:
        print(f[2]) if A.index(t) - A.index(s) in D else print(f[3])
    if t in B and s in B:
        print(f[0]) if B.index(t) - B.index(s) in D else print(f[5])
    if t in C and s in C:
        print(f[1]) if C.index(t) - C.index(s) in D else print(f[4])

Submission status: Accepted

Solution code:
rot = ((1,2,4,3),(2,0,3,5),(0,1,5,4))
f = input().split()

for i in range(int(input())):
    t, s = map(f.index, input().split())
    for j in rot:
        if t not in j or s not in j:
            continue
        d = rot.index(j)
        if (j.index(t) - j.index(s)) %4 == 1:
            d -= 5
    print(f[abs(d)])

Submission status: Accepted

Solution code:
rot = ((1,2,4,3),(2,0,3,5),(0,1,5,4))
f = input().split()

for i in range(int(input())):
    t, s = map(f.index, input().split())
    for j in rot:
        if not (t in j)*(s in j):
            continue
        d = rot.index(j)
        if (j.index(t) - j.index(s)) %4 == 1:
            d -= 5
    print(f[abs(d)])

Submission status: Accepted

Solution code:
R = ((1,2,4,3),(2,0,3,5),(0,1,5,4))
f = input().split()

for i in range(int(input())):
    t, s = map(f.index, input().split())
    for j in R:
        if not (t in j)*(s in j):
            continue
        d = R.index(j)
        if (j.index(t) - j.index(s)) %4 == 1:
            d -= 5
    print(f[abs(d)])

Submission status: Accepted

Solution code:
n = [int(i) for i in input().split()]
count = int(input())
num = []

class Dice(object):
    def __init__(self,num):
        self.one = num[0]
        self.two = num[1]
        self.three = num[2]
        self.four = num[3]
        self.five = num[4]
        self.six = num[5]

    def right(self,top,forward):
        if top == self.one:
            if forward == self.two:
                return self.three
            elif forward == self.three:
                return self.five
            elif forward == self.five:
                return self.four
            elif forward == self.four:
                return self.two
        elif top == self.two:
            if forward == self.six:
                return self.three
            elif forward == self.three:
                return self.one
            elif forward == self.one:
                return self.four
            elif forward == self.four:
                return self.six
        elif top == self.three:
            if forward == self.two:
                return self.six
            elif forward == self.six:
                return self.five
            elif forward == self.five:
                return self.one
            elif forward == self.one:
                return self.two
        elif top == self.four:
            if forward == self.two:
                return self.one
            elif forward == self.one:
                return self.five
            elif forward == self.five:
                return self.six
            elif forward == self.six:
                return self.two
        elif top == self.five:
            if forward == self.one:
                return self.three
            elif forward == self.three:
                return self.six
            elif forward == self.six:
                return self.four
            elif forward == self.four:
                return self.one
        elif top == self.six:
            if forward == self.two:
                return self.four
            elif forward == self.four:
                return self.five
            elif forward == self.five:
                return self.three
            elif forward == self.three:
                return self.two


dice = Dice(n)
for i in range(count):
    num.append([int(i) for i in input().split()])

for i in range(count):
    print(dice.right(num[i][0],num[i][1]))

Submission status: Accepted

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

    def throw(self, direction):
        if direction == "E":
            pmen = men[:]
            men[0] = pmen[3]
            men[1] = pmen[1]
            men[2] = pmen[0]
            men[3] = pmen[5]
            men[4] = pmen[4]
            men[5] = pmen[2]
        elif direction == "W":
            pmen = men[:]
            men[0] = pmen[2]
            men[1] = pmen[1]
            men[2] = pmen[5]
            men[3] = pmen[0]
            men[4] = pmen[4]
            men[5] = pmen[3]
        elif direction == "S":
            pmen = men[:]
            men[0] = pmen[4]
            men[1] = pmen[0]
            men[2] = pmen[2]
            men[3] = pmen[3]
            men[4] = pmen[5]
            men[5] = pmen[1]
        elif direction == "N":
            pmen = men[:]
            men[0] = pmen[1]
            men[1] = pmen[5]
            men[2] = pmen[2]
            men[3] = pmen[3]
            men[4] = pmen[0]
            men[5] = pmen[4]


    def printUe(self):
        print (self.men)[0]

    def printMigi(self):
        print (self.men)[2]

    def printMen(self):
        print self.men


men = map(int, (raw_input()).split(" "))
d = Dice(men)

q = int(raw_input())

for i in range(q):
    Q = map(int, (raw_input()).split(" "))
    if(d.men[2] == Q[1] or d.men[3] == Q[1]):
        d.throw("E")

    while(d.men[1] != Q[1]):
        d.throw("S")

    while(d.men[0] != Q[0]):
        d.throw("E")

    d.printMigi()

Submission status: Accepted

Solution code:
class Dice:

    __top = 0
    __front = 1
    __right = 2
    __left = 3
    __back = 4
    __bottom = 5

    def __init__(self, a, b, c, d, e, f):
        self.__dice = [a,b,c,d,e,f]

    def S(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__back]
        self.__dice[Dice.__front]  = dice_before[Dice.__top]
        self.__dice[Dice.__right]  = dice_before[Dice.__right]
        self.__dice[Dice.__left]   = dice_before[Dice.__left]
        self.__dice[Dice.__back]   = dice_before[Dice.__bottom]
        self.__dice[Dice.__bottom] = dice_before[Dice.__front]

    def E(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__left]
        self.__dice[Dice.__front]  = dice_before[Dice.__front]
        self.__dice[Dice.__right]  = dice_before[Dice.__top]
        self.__dice[Dice.__left]   = dice_before[Dice.__bottom]
        self.__dice[Dice.__back]   = dice_before[Dice.__back]
        self.__dice[Dice.__bottom] = dice_before[Dice.__right]

    def W(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__right]
        self.__dice[Dice.__front]  = dice_before[Dice.__front]
        self.__dice[Dice.__right]  = dice_before[Dice.__bottom]
        self.__dice[Dice.__left]   = dice_before[Dice.__top]
        self.__dice[Dice.__back]   = dice_before[Dice.__back]
        self.__dice[Dice.__bottom] = dice_before[Dice.__left]

    def N(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__front]
        self.__dice[Dice.__front]  = dice_before[Dice.__bottom]
        self.__dice[Dice.__right]  = dice_before[Dice.__right]
        self.__dice[Dice.__left]   = dice_before[Dice.__left]
        self.__dice[Dice.__back]   = dice_before[Dice.__top]
        self.__dice[Dice.__bottom] = dice_before[Dice.__back]

    def turn(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__top]
        self.__dice[Dice.__front]  = dice_before[Dice.__right]
        self.__dice[Dice.__right]  = dice_before[Dice.__back]
        self.__dice[Dice.__left]   = dice_before[Dice.__front]
        self.__dice[Dice.__back]   = dice_before[Dice.__left]
        self.__dice[Dice.__bottom] = dice_before[Dice.__bottom]

    def top(self):
        return(self.__dice[Dice.__top])

    def front(self):
        return(self.__dice[Dice.__front])

    def right(self):
        return(self.__dice[Dice.__right])

    def left(self):
        return(self.__dice[Dice.__left])

    def back(self):
        return(self.__dice[Dice.__back])

    def bottom(self):
        return(self.__dice[Dice.__bottom])

    def print(self):
        print('[top   ] :', self.__dice[Dice.__top])
        print('[front ] :', self.__dice[Dice.__front])
        print('[right ] :', self.__dice[Dice.__right])
        print('[left  ] :', self.__dice[Dice.__left])
        print('[back  ] :', self.__dice[Dice.__back])
        print('[bottom] :', self.__dice[Dice.__bottom])


def S(dice, top):
    for t in range(4):
        if dice.top() == top:
            return dice
        dice.S()
    return dice

def turn(dice, front):
    for t in range(4):
        if dice.front() == front:
            return dice
        dice.turn()
    return dice

a,b,c,d,e,f = input().split()
q = int(input())

for i in range(q):
    dice = Dice(a,b,c,d,e,f)
    top,front = input().split()

    for to in range(2):
        dice = S(dice, top)
        # dice.print()
        if dice.top() == top:
            break
        else:
            dice.turn()

    for fr in range(4):
        dice = turn(dice, front)
        if dice.front() == front:
            print(dice.right())
            break