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

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

Solution (Python):
class Dice(object):
    """
    6??¢???????????????
    ?????¢???top, ?????¢???bottom???????????\??????4??¢??????????????? north, south, east, west ??¨?????¶???
    """
    def __init__(self, faces):
        
        
        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):
        
        
        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
        

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

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

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

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

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

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

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

Solution (Python):

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

    
    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

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

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

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

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

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

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

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

Solution (Python):
class Dice:
    def __init__(self,l1,l2,l3,l4,l5,l6):
        self.l1 = l1
        self.l2 = l2
        self.l3 = l3
        self.l4 = l4
        self.l5 = l5
        self.l6 = l6
        self.top = 1
        self.front = 2
        self.right = 3

    def get_up_front(self):
        return eval("("+"self." + 'l' + str(self.top)+","+"self." + 'l' + str(self.front)+")")

    def get_right(self):
        return eval("self." + 'l' + str(self.right))

    def rot(self):
        self.front,self.right = self.right,7-self.front

    def move(self, order):
        if order == 'S':
            pretop = self.top
            self.top = 7 - self.front
            self.front = pretop
        elif order == 'E':
            pretop = self.top
            self.top = 7 - self.right
            self.right = pretop
        elif order == 'N':
            pretop = self.top
            self.top = self.front
            self.front = 7 - pretop
        elif order == 'W':
            pretop = self.top
            self.top = self.right
            self.right = 7 - pretop

l = list(map(int,input().split()))
q = int(input())
d = Dice(l[0],l[1],l[2],l[3],l[4],l[5])

for i in range(q):
    up,front = list(map(int,input().split()))
    while (up,front) != d.get_up_front():
        for j in range(4):
            d.move('S')
            if up == d.get_up_front()[0]:
                break
        if up != d.get_up_front()[0]:
            for l in range(4):
                d.move('E')
                if up == d.get_up_front()[0]:
                    break
        for k in range(4):
            d.rot()
            if front == d.get_up_front()[1]:
                break
    print (d.get_right())

Solution (Python):
def move(up,bottom,right,left,front,back,direction):
    if direction == "N":
        return (front,back,right,left,bottom,up)
    elif direction == "S":
        return (back,front,right,left,up,bottom)
    elif direction == "E":
        return (left,right,up,bottom,front,back)
    elif direction == "W":
        return (right,left,bottom,up,front,back)
    
def check(up,bottom,right,left,front,back,up_current,front_current):
    if front_current == front and up_current == up:
        print(right)     
    elif front_current == front:
        if left == up_current:
            print(up)
        elif bottom == up_current:
            print(left)
        elif right == up_current:
            print(bottom)
    elif up_current == up:
        if right == front_current:
            print(back)
        elif back == front_current:
            print(left)
        elif left == front_current:
            print(front)
    else:
      
        up,bottom,right,left,front,back = move(up,bottom,right,left,front,back,"S")
        check(up,bottom,right,left,front,back,up_current,front_current)
                
                
                
                
up,front,right,left,back,bottom = input().split()
n = int(input())

for i in range(n):
    up_current,front_current = input().split()
    check(up,bottom,right,left,front,back,up_current,front_current)

Solution (Python):


import sys


class Die:
    def __init__(self, pips):
        self.pips = pips

    def move_die(self, direction):
        tmp = int()
        if direction == 'N':
            tmp = self.pips[0]
            self.pips[0] = self.pips[1]
            self.pips[1] = self.pips[5]
            self.pips[5] = self.pips[4]
            self.pips[4] = tmp
            del tmp
        elif direction == 'S':
            tmp = self.pips[0]
            self.pips[0] = self.pips[4]
            self.pips[4] = self.pips[5]
            self.pips[5] = self.pips[1]
            self.pips[1] = tmp
            del tmp
        elif direction == 'E':
            tmp = self.pips[0]
            self.pips[0] = self.pips[3]
            self.pips[3] = self.pips[5]
            self.pips[5] = self.pips[2]
            self.pips[2] = tmp
            del tmp
        elif direction == 'W':
            tmp = self.pips[0]
            self.pips[0] = self.pips[2]
            self.pips[2] = self.pips[5]
            self.pips[5] = self.pips[3]
            self.pips[3] = tmp
            del tmp

    def spin_die_clockwise(self):
        tmp = self.pips[1]
        self.pips[1] = self.pips[2]
        self.pips[2] = self.pips[4]
        self.pips[4] = self.pips[3]
        self.pips[3] = tmp
        del tmp

    def get_upside(self):
        return self.pips[0]

    def get_frontside(self):
        return self.pips[1]

    def get_rightside(self):
        return self.pips[2]


def init_die():
    pips = [int(pip) for pip in sys.stdin.readline().strip('\n').split()]
    die = Die(pips)
    return die


def roll_die(die):
    directions = list(sys.stdin.readline().strip('\n'))

    for direction in directions:
        die.move_die(direction)

    return die


def main():
    die = init_die()
    q = int(sys.stdin.readline().strip('\n'))

    for question in range(q):
        q_pips = [int(pip) for pip in sys.stdin.readline().strip('\n').split()]

        if q_pips[0] == die.pips[0]:
            pass
        elif q_pips[0] == die.pips[1]:
            die.move_die('N')
        elif q_pips[0] == die.pips[2]:
            die.move_die('W')
        elif q_pips[0] == die.pips[3]:
            die.move_die('E')
        elif q_pips[0] == die.pips[4]:
            die.move_die('S')
        elif q_pips[0] == die.pips[5]:
            die.move_die('N')
            die.move_die('N')

        while die.get_frontside() != q_pips[1]:
            die.spin_die_clockwise()

        print(die.get_rightside())


if __name__ == '__main__':
    main()