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 Converter:
    d = {
        1: { 2: 3, 3: 5, 4: 2, 5: 4, },
        2: { 1: 4, 3: 1, 4: 6, 6: 3, },
        3: { 1: 2, 2: 6, 5: 1, 6: 5, },
        4: { 1: 5, 2: 1, 5: 6, 6: 2, },
        5: { 1: 3, 3: 6, 4: 1, 6: 4, },
        6: { 2: 4, 3: 2, 4: 5, 5: 3, }
        }

    def __init__(self, s):
        l = s.split()
        self.tbl = dict(zip(l, range(1,7)))
        self.pip = dict(zip(range(1,7), l))

    def __call__(self, s):
        l, r = s.split()
        print(self.pip[self.d[self.tbl[l]][self.tbl[r]]])

cv = Converter(input())
n = int(input())
for _ in range(n):
    cv(input())

Solution (Python):
class Dice2():

    def __init__(self, v): 
        self.view = v
        self.dTFR = {}
        for i, j, k in ((ii, (ii++2)%6, (ii+4)%6) for ii in  range(6)):
            self.dTFR[self.view[i]]=[self.view[v] for v in (j, k, 5-j, 5-k, j)]


d2, q = Dice2(list(map(int, input().split()))), int(input())
for i in range(q):
    t, f = map(int, input().split())
    print(d2.dTFR[t][d2.dTFR[t].index(f)+1] )

Solution (Python):
class Dice:
    def __init__(self):
        self.dice=[1,2,3,4,5,6] 
    def set(self,l):
        self.dice=l
    def roll(self, s):
        import copy
        mat=((1,4,3,2),(5,0,1,1),(2,2,0,5),(3,3,5,0),(0,5,4,4),(4,1,2,3))
        l=copy.deepcopy(self.dice)
        if s == 'N': c = 0
        if s == 'S': c = 1
        if s == 'E': c = 2
        if s == 'W': c = 3
        for i in range(6):
            self.dice[i]=l[mat[i][c]]
    def get(self):
        return self.dice

import random
d=Dice()
d.set(list(map(int,input().split())))
for i in range(int(input())):
    u,f=map(int,input().split())
    while True:
        d.roll('NSEW'[random.randrange(4)])
        if d.get()[0]==u and d.get()[1]==f:
            print(d.get()[2])
            break

Solution (Python):
class Dice(object):
    
    def __init__(self, line):
        self.top = 1
        self.bottom = 6
        self.south = 2
        self.east = 3
        self.west = 4
        self.north = 5
        self.convert = [int(s) for s in line.split()]
    
    def move(self, direction):
        if 'N' == direction:
            self.top, self.north, self.bottom, self.south = self.south, self.top, self.north, self.bottom
        elif 'S' == direction:
            self.top, self.north, self.bottom, self.south = self.north, self.bottom, self.south, self.top
        elif 'W' == direction:
            self.top, self.east, self.bottom, self.west = self.east, self.bottom, self.west, self.top
        elif 'E' == direction:
            self.top, self.east, self.bottom, self.west = self.west, self.top, self.east, self.bottom
    
    def search(self, line):
        top, south = [int(s) for s in line.split()]
        for direction in 'NNNNWNNNN':
            self.move(direction)
            if self.convert[self.south - 1] == south:
                break
        for direction in 'WWWW':
            self.move(direction)
            if self.convert[self.top - 1] == top:
                break
        return self.result()
        
    def result(self):
        return self.convert[self.east - 1]
    
dice = Dice(input())
for i in range(int(input())):
    print(dice.search(input()))

Solution (Python):
dice = {
    '1': {'F':'2', 'R':'4', 'B':'5', 'L':'3'},
    '2': {'F':'6', 'R':'4', 'B':'1', 'L':'3'},
    '3': {'F':'6', 'R':'2', 'B':'1', 'L':'5'},
    '4': {'F':'6', 'R':'5', 'B':'1', 'L':'2'},
    '5': {'F':'6', 'R':'3', 'B':'1', 'L':'4'},
    '6': {'F':'5', 'R':'4', 'B':'2', 'L':'3'}
}

faces = list(map(int, input().split()))
q = int(input())

rightSide = {'F':'L','R':'F','B':'R','L':'B'}

for i in range(q):
    top, front = list(map(int, input().split()))
    topIdx = str(faces.index(top) + 1)
    frontIdx = str(faces.index(front) + 1)
    for k,v in dice[topIdx].items():
        if v[0] == str(frontIdx):
            faceIdx = dice[topIdx][rightSide[k]]
            print(faces[int(faceIdx)-1])
            break

Solution (Python):
class Dice:
    dice = {'N':2, 'E':4, 'S':5, 'W':3}
    currTop = 1

    def top(self):
        return self.currTop

    def rot(self, direction):
        newTop = self.dice[direction]
        currTop = self.currTop
        self.currTop = newTop
        if direction == 'N':
            self.dice['N'] = 7 - currTop
            self.dice['S'] = currTop
        elif direction == 'S':
            self.dice['N'] = currTop
            self.dice['S'] = 7 - currTop
        elif direction == 'E':
            self.dice['E'] = 7 - currTop
            self.dice['W'] = currTop
        elif direction == 'W':
            self.dice['E'] = currTop
            self.dice['W'] = 7 - currTop

    def yaw(self, direction):
        newDice = {}
        if direction == 'E':
            newDice['N'] = self.dice['E']
            newDice['E'] = self.dice['S']
            newDice['S'] = self.dice['W']
            newDice['W'] = self.dice['N']
        if direction == 'W':
            newDice['N'] = self.dice['W']
            newDice['E'] = self.dice['N']
            newDice['S'] = self.dice['E']
            newDice['W'] = self.dice['S']
        self.dice = newDice

faces = list(map(int, input().split()))
q = int(input())
d = Dice()

for i in range(q):
    top, front = list(map(int, input().split()))
    topIdx = faces.index(top) + 1
    frontIdx = faces.index(front) + 1

    if d.top() != topIdx:
        
        key = [k for k,v in d.dice.items() if topIdx == v]
        if len(key) == 0:
            d.rot('N')
            key = [k for k,v in d.dice.items() if topIdx == v]
        d.rot(key[0])

    
    while True:
        if d.dice['N'] == frontIdx:
            break
        d.yaw('E')

    print(faces[d.dice['W']-1])

Solution (Python):
class Dice:
    def __init__(self):
        self.dice = {'N':2, 'E':4, 'S':5, 'W':3}
        self.currTop = 1

    def top(self):
        return self.currTop

    def rot(self, direction):
        newTop = self.dice[direction]
        currTop = self.currTop
        self.currTop = newTop
        if direction == 'N':
            self.dice['N'] = 7 - currTop
            self.dice['S'] = currTop
        elif direction == 'S':
            self.dice['N'] = currTop
            self.dice['S'] = 7 - currTop
        elif direction == 'E':
            self.dice['E'] = 7 - currTop
            self.dice['W'] = currTop
        elif direction == 'W':
            self.dice['E'] = currTop
            self.dice['W'] = 7 - currTop

    def yaw(self, direction):
        newDice = {}
        if direction == 'E':
            newDice['N'] = self.dice['E']
            newDice['E'] = self.dice['S']
            newDice['S'] = self.dice['W']
            newDice['W'] = self.dice['N']
        if direction == 'W':
            newDice['N'] = self.dice['W']
            newDice['E'] = self.dice['N']
            newDice['S'] = self.dice['E']
            newDice['W'] = self.dice['S']
        self.dice = newDice

faces = list(map(int, input().split()))
q = int(input())
d = Dice()

for i in range(q):
    top, front = list(map(int, input().split()))
    topIdx = faces.index(top) + 1
    frontIdx = faces.index(front) + 1

    if d.top() != topIdx:
        
        key = [k for k,v in d.dice.items() if topIdx == v]
        if len(key) == 0:
            d.rot('N')
            key = [k for k,v in d.dice.items() if topIdx == v]
        d.rot(key[0])

    
    while True:
        if d.dice['N'] == frontIdx:
            break
        d.yaw('E')

    print(faces[d.dice['W']-1])

Solution (Python):


import random

class Dice:

    def __init__(self, diceList):
        self.d1, self.d2, self.d3, self.d4, self.d5, self.d6 = diceList

    def diceRoll(self, r):
        if(r == 'N'):
            self.d1, self.d2, self.d5, self.d6 = self.d2, self.d6, self.d1, self.d5

        elif(r == 'E'):
            self.d1, self.d3, self.d4, self.d6 = self.d4, self.d1, self.d6, self.d3

        elif(r == 'W'):
            self.d1, self.d3, self.d4, self.d6 = self.d3, self.d6, self.d1, self.d4

        elif(r == 'S'):
            self.d1, self.d2, self.d5, self.d6 = self.d5, self.d1, self.d6, self.d2


def main():

    orderList = ['N', 'E', 'W', 'S']
    inputDiceList = list(map(int, input().split()))
    inputNum = int(input())
    d = Dice(inputDiceList)

    for i in range(inputNum):
        x, y = map(int, input().split())
        while True:
            if(d.d1 == x and d.d2 == y):
                print(d.d3)
                break
            else:
                r = random.randint(0, 3)
                d.diceRoll(orderList[r])

if __name__ == '__main__':
    main()

Solution (Python):
def main_1_11_A():
    values = list(map(int, input().split()))
    query = input()

    dice = Dice(values)
    for d in query:
        dice.roll(d)


def main_1_11_B():
    values = list(map(int, input().split()))
    n = int(input())

    for _ in range(n):
        t_value, s_value = map(int, input().split())

        print(_B(values, t_value, s_value))


def _B(values, t_value, s_value):
    """
    ????????¢?????????????????¨??????
    ??´??¢???????????¢?????????????????§???????????????????????¨?????????
    ??????????????????????????¢???????¢??????????
    """
    def rot_n(dice, n):
        for _ in range(n):
            dice.roll("N").roll("E").roll("S")
        return dice

    def make_dices():
        
        return [
            Dice(values),
            Dice(values).roll("N"),
            Dice(values).roll("N").roll("N"),
            Dice(values).roll("N").roll("N").roll("N"),
            Dice(values).roll("E"),
            Dice(values).roll("W"),
        ]

    dices = (
        make_dices() +
        list(map(lambda dice: rot_n(dice, 1), make_dices())) +
        list(map(lambda dice: rot_n(dice, 2), make_dices())) +
        list(map(lambda dice: rot_n(dice, 3), make_dices()))
    )
    for dice in dices:
        
        for d in ("E" * 4 + "W" * 4 + "S" * 4 + "N" * 4):
            dice.roll(d)
            
            if (dice.top_value, dice.south_value) == (t_value, s_value):
                return dice.east_value


class Dice:

    def __init__(self, values):
        self.values = values
        
        self.t = 1
        self.b = 6
        self.w = 4
        self.e = 3
        self.n = 5
        self.s = 2

    def __repr__(self):
        labels = {
            "t": self.t,
            "b": self.b,
            "w": self.w,
            "e": self.e,
            "n": self.n,
            "s": self.s,
        }
        return "<%s (%s, %s)>" % (self.__class__, self.values, labels)

    def roll(self, direction):
        if direction == "E":
            after_lables = self.t, self.b, self.e, self.w
            self.e, self.w, self.b, self.t = after_lables
        elif direction == "W":
            after_lables = self.t, self.b, self.e, self.w
            self.w, self.e, self.t, self.b = after_lables
        elif direction == "S":
            after_lables = self.t, self.b, self.n, self.s
            self.s, self.n, self.t, self.b = after_lables
        elif direction == "N":
            after_lables = self.t, self.b, self.n, self.s
            self.n, self.s, self.b, self.t = after_lables

        return self

    @property
    def top_value(self):
        return self.values[self.t - 1]

    @property
    def south_value(self):
        return self.values[self.s - 1]

    @property
    def east_value(self):
        return self.values[self.e - 1]

if __name__ == "__main__":
    
    main_1_11_B()

Solution (Python):
dice = {
	1: [2, 3, 5, 4],
	2: [6, 3, 1, 4],
	3: [2, 6, 5, 1],
	4: [2, 1, 5, 6],
	5: [1, 3, 6, 4],
	6: [2, 4, 5, 3]
}
tags = []

def GetRight(top, front):
	numList = dice[top]
	for i in range(4):
		if numList[i] == front:
			return numList[(i+1)%4]

def SetDice():
	global tags
	tags = input().split(" ")
	
def DoQuestion():
	nums = input().split(" ")
	top = tags.index(nums[0])+1
	front = tags.index(nums[1])+1
	right = GetRight(top, front)
	print(tags[right-1])
	
SetDice()
q = int(input())
for i in range(q):
	DoQuestion()

Solution (Python):
d=input().split()
for _ in range(int(input())):
 t,f=[d.index(s) for s in input().split()]
 n={0:(1,2,4,3,1),1:(0,3,5,2,0),2:(0,1,5,4,0),3:(1,0,4,5,1),4:(0,2,5,3,0),5:(1,3,4,2,1)}[int(t)]
 print(d[n[n.index(int(f))+1]])

Solution (Python):
import random

class Dice():
    
    def __init__(self, *n):
       self.rolls = n

    def spin(self, order):
        if order == "N":
            self.rolls = [
                self.rolls[1],
                self.rolls[5],
                self.rolls[2],
                self.rolls[3],
                self.rolls[0],
                self.rolls[4]
            ]
        if order == "E":
            self.rolls = [
                self.rolls[3],
                self.rolls[1],
                self.rolls[0],
                self.rolls[5],
                self.rolls[4],
                self.rolls[2]
            ]
        if order == "S":
            self.rolls = [
                self.rolls[4],
                self.rolls[0],
                self.rolls[2],
                self.rolls[3],
                self.rolls[5],
                self.rolls[1]
            ]
        if order == "W":
            self.rolls = [
                self.rolls[2],
                self.rolls[1],
                self.rolls[5],
                self.rolls[0],
                self.rolls[4],
                self.rolls[3]
            ]
        return self.rolls[0]

d = Dice(*[int(i) for i in input().split()])
directions = ['N','E','S','W']
for _ in range(int(input())):
    a,b = [int(i) for i in input().split()]
    while a != d.rolls[0] or d.rolls[1] != b:
        d.spin(directions[random.randint(0,3)])
    print(d.rolls[2])

Solution (Python):
dice = list(map(int, input().split()))
mask = {
    'N': (1, 5, 2, 3, 0, 4),
    'R': (0, 2, 4, 1, 3, 5)
    }


for i in range(int(input())):
    a, b = map(int, input().split())

    lim = 0
    while dice[0] != a:
        dice = [dice[x] for x in mask['N']]
        lim += 1
        if lim == 4:
            dice = [dice[x] for x in mask['R']]
            lim = 0
    while dice[1] != b:
        dice = [dice[x] for x in mask['R']]
    print(dice[2])

Solution (Python):
class dice:
    men = [0] * 7
    def __init__(self, li):
        self.men = [0] + li
    def move0(self, a, b, c, d):
        self.men[a], self.men[b], self.men[c], self.men[d] = self.men[b], self.men[c], self.men[d], self.men[a]
    def move(self, h):
        if h == "N":
            self.move0(1, 2, 6, 5)
        elif h == "S":
            self.move0(1, 5, 6, 2)
        elif h == "W":
            self.move0(1, 3, 6, 4)
        elif h == "E":
            self.move0(1, 4, 6, 3)
    def move2(self, a, b):
        if self.men[1] != a:
            for i, ss in [(2, "N"), (3, "W"), (4, "E"), (5, "S"), (6, "NN")]:
                if self.men[i] == a:
                    for s in ss:
                        self.move(s)
                    break
        if self.men[2] != b:
            if b == self.men[3]:
                self.move0(2, 3, 5, 4)
            elif b == self.men[4]:
                self.move0(2, 4, 5, 3)
            else:
                self.men[2], self.men[5], self.men[3], self.men[4] = self.men[5], self.men[2], self.men[4], self.men[3]
saikoro = dice(list(map(int, input().split())))
for _ in range(int(input())):
    saikoro.move2(*map(int, input().split()))
    print(saikoro.men[3])

Solution (Python):
class dice:
    men = [0] * 7
    def __init__(self, li):
        self.men = [0] + li
    def move0(self, a, b, c, d):
        self.men[a], self.men[b], self.men[c], self.men[d] = self.men[b], self.men[c], self.men[d], self.men[a]
    def move(self, h):
        if h == "N":
            self.move0(1, 2, 6, 5)
        elif h == "S":
            self.move0(1, 5, 6, 2)
        elif h == "W":
            self.move0(1, 3, 6, 4)
        elif h == "E":
            self.move0(1, 4, 6, 3)
        elif h == "O":
            self.men[1], self.men[6], self.men[2], self.men[5] = self.men[6], self.men[1], self.men[5], self.men[2]
        elif h == "L":
            self.move0(2, 3, 5, 4)
        elif h == "R":
            self.move0(2, 4, 5, 3)
        elif h == "B":
            self.men[3], self.men[4], self.men[2], self.men[5] = self.men[4], self.men[3], self.men[5], self.men[2]
    def move2(self, a, b):
        if self.men[1] != a:
            for i, s in [(2, "N"), (3, "W"), (4, "E"), (5, "S"), (6, "O")]:
                if self.men[i] == a:
                    self.move(s)
                    break
        if self.men[2] != b:
            if b == self.men[3]:
                self.move("L")
            elif b == self.men[4]:
                self.move("R")
            else:
                self.move("B")
saikoro = dice(list(map(int, input().split())))
for _ in range(int(input())):
    saikoro.move2(*map(int, input().split()))
    print(saikoro.men[3])

Solution (Python):
class dice:
    def __init__(self, li):
        self.men = [0] + li
    def move0(self, a, b, c, d):
        self.men[a], self.men[b], self.men[c], self.men[d] = self.men[b], self.men[c], self.men[d], self.men[a]
    def move(self, h):
        if h == "N":
            self.move0(1, 2, 6, 5)
        elif h == "S":
            self.move0(1, 5, 6, 2)
        elif h == "W":
            self.move0(1, 3, 6, 4)
        elif h == "E":
            self.move0(1, 4, 6, 3)
        elif h == "O":
            self.men[1], self.men[6], self.men[2], self.men[5] = self.men[6], self.men[1], self.men[5], self.men[2]
        elif h == "L":
            self.move0(2, 3, 5, 4)
        elif h == "R":
            self.move0(2, 4, 5, 3)
        elif h == "B":
            self.men[3], self.men[4], self.men[2], self.men[5] = self.men[4], self.men[3], self.men[5], self.men[2]
    def move2(self, a, b):
        if self.men[1] != a:
            for i, s in [(2, "N"), (3, "W"), (4, "E"), (5, "S"), (6, "O")]:
                if self.men[i] == a:
                    self.move(s)
                    break
        if self.men[2] != b:
            if b == self.men[3]:
                self.move("L")
            elif b == self.men[4]:
                self.move("R")
            else:
                self.move("B")
saikoro = dice(list(map(int, input().split())))
for _ in range(int(input())):
    saikoro.move2(*map(int, input().split()))
    print(saikoro.men[3])

Solution (Python):
data = list(map(int, input().split()))
n = int(input())
dice = ['12431', '03520', '01540', '04510', '02530', '13421']
for i in range(n):
    up, front = map(int, input().split())
    u = data.index(up)
    f = data.index(front)
    a = dice[u].find(str(f))
    print(data[int(dice[u][a+1])])

Solution (Python):
d=input().split()
for _ in range(int(input())):
 t,f=[int(d.index(s))for s in input().split()]
 n={0:(1,2,4,3,1),1:(0,3,5,2,0),2:(0,1,5,4,0),3:(1,0,4,5,1),4:(0,2,5,3,0),5:(1,3,4,2,1)}[t]
 print(d[n[n.index(f)+1]])

Solution (Python):
d=input().split()
for _ in range(int(input())):
 t,f=[int(d.index(s))for s in input().split()]
 n=[(1,2,4,3,1),(0,3,5,2,0),(0,1,5,4,0),(1,0,4,5,1),(0,2,5,3,0),(1,3,4,2,1)][t]
 print(d[n[n.index(f)+1]])

Solution (Python):
d=input().split()
for _ in range(int(input())):
 t,f=[d.index(s)for s in input().split()]
 n=[(1,2,4,3,1),(0,3,5,2,0),(0,1,5,4,0),(1,0,4,5,1),(0,2,5,3,0),(1,3,4,2,1)][t]
 print(d[n[n.index(f)+1]])

Solution (Python):
class dice:

    def __init__(self, numlist):
        self._men = {"T": numlist[0],
                     "B": numlist[5],
                     "N": numlist[4],
                     "S": numlist[1],
                     "W": numlist[3],
                     "E": numlist[2]}

    def roll(self, direction):
        t = dict(self._men)

        if direction == "N":
            self._men["T"] = t["S"]
            self._men["B"] = t["N"]
            self._men["N"] = t["T"]
            self._men["S"] = t["B"]
        elif direction == "S":
            self._men["T"] = t["N"]
            self._men["B"] = t["S"]
            self._men["S"] = t["T"]
            self._men["N"] = t["B"]
        elif direction == "W":
            self._men["T"] = t["E"]
            self._men["B"] = t["W"]
            self._men["W"] = t["T"]
            self._men["E"] = t["B"]
        elif direction == "E":
            self._men["T"] = t["W"]
            self._men["B"] = t["E"]
            self._men["W"] = t["B"]
            self._men["E"] = t["T"]


    def settopsouth(self, top, south):
        if self._men["T"] == top:
            pass
        elif self._men["S"] == top:
            self.roll("N")
        elif self._men["N"] == top:
            self.roll("S")
        elif self._men["W"] == top:
            self.roll("E")
        elif self._men["E"] == top:
            self.roll("W")
        elif self._men["B"] == top:
            self.roll("N")
            self.roll("N")

        if self._men["S"] == south:
            return self._men["E"]
        elif self._men["E"] == south:
            return self._men["N"]
        elif self._men["N"] == south:
            return self._men["W"]
        elif self._men["W"] == south:
            return self._men["S"]


    def gettop(self):
        return self._men["T"]

def decode():
    numlist = [int(x) for x in input().split()]
    n = int(input())
    qs = []
    for _ in range(n):
        al = [int(x) for x in input().split()]
        qs.append(al)

    return numlist, n, qs


if __name__ == '__main__':

    numlist, n, qs = decode()
    d1 = dice(numlist)
    for q in qs:
        print(d1.settopsouth(*q))

Solution (Python):
import random

class Dice:
    def __init__(self, hoge):
        self.num = hoge
        self.face = {'U': 0, 'W': 3, 'E': 2, 'N': 4, 'S': 1, 'B': 5} 
    
    def move(self, direct):
        previouse = {k: v for k, v in self.face.items()}
        if direct == 'E':
            self.face['U'] = previouse['W']
            self.face['W'] = previouse['B']
            self.face['E'] = previouse['U']
            self.face['B'] = previouse['E']
        elif direct == 'W':
            self.face['U'] = previouse['E']
            self.face['W'] = previouse['U']
            self.face['E'] = previouse['B']
            self.face['B'] = previouse['W']
        elif direct == 'N':
            self.face['U'] = previouse['S']
            self.face['N'] = previouse['U']
            self.face['S'] = previouse['B']
            self.face['B'] = previouse['N']
        elif direct == 'S':
            self.face['U'] = previouse['N']
            self.face['N'] = previouse['B']
            self.face['S'] = previouse['U']
            self.face['B'] = previouse['S']
    
    def get_num(self, place):
        return self.num[self.face[place]]
    
        
if __name__ == '__main__':
    dice = Dice([int(x) for x in input().split()])
    num_q = int(input())
    d = ['E', 'W', 'N', 'S']
    for _ in range(num_q):
        x, y = [int(x) for x in input().split()]
        while True:
            rand_num = random.randint(0, 3)
            dice.move(d[rand_num])
            if dice.get_num('U') == x and dice.get_num('S') == y:
                break
        print(dice.get_num('E'))

Solution (Python):
class Dice:
    def __init__(self, top, dside):
        self.top = top
        self.dside = dside
        self.rside = None
        self.num = [self.top, self.dside, 7-self.top, 7-self.dside]
    def rside_num(self):
        if 2 in self.num and 3 in self.num:
            if(self.num.index(3)==self.num.index(2)+1 or 
                self.num==[3, 5, 4, 2]):
                self.rside = 1
            else:
                self.rside = 6
        elif 1 in self.num and 4 in self.num:
            if(self.num.index(4)==self.num.index(1)+1 or 
                self.num==[4, 6, 3, 1]):
                self.rside = 2
            else:
                self.rside = 5
        else:
            if(self.num.index(2)==self.num.index(1)+1 or
                self.num==[2, 6, 5, 1]):
                self.rside = 3
            else:
                self.rside = 4

num = list(map(int, input().split()))
n = int(input())
for i in range(n):
    value = list(map(int, input().split()))
    dice = Dice(num.index(value[0])+1, num.index(value[1])+1)
    dice.rside_num()
    print(num[dice.rside-1])

Solution (Python):
jisho= {0:
    [[1,2],[3,1],[4,3],[2,4]],
     1:
    [[0,3],[3,5],[5,2],[2,0]],
    2:
    [[0,1],[1,5],[5,4],[4,0]],
    3:
    [[0,4],[4,5],[5,1],[1,0]],
    4:
    [[0,2],[2,5],[5,3],[3,0]],
    5:
    [[2,1],[1,3],[3,4],[4,2]]
       }
i_number = [int(x) for x in input().split()]
n = int(input())
for _ in range(n):
    q1,q2 = [int(x) for x in input().split()]
    for i,n in enumerate(i_number):
        if q1 == n:
            qq1 = i
        if q2 == n:
            qq2 = i
    for k,v in jisho.items():
        if [qq1,qq2] in v:
            print(i_number[k])

Solution (Python):



import sys

face = sys.stdin.readline().split()

right = [[None for front in range(6)] for top in range(6)]


right[0][1] = right[1][5] = right[5][4] = right[4][0] = 3 - 1
right[1][0] = right[5][1] = right[4][5] = right[0][4] = 4 - 1
right[0][2] = right[2][5] = right[5][3] = right[3][0] = 5 - 1
right[2][0] = right[5][2] = right[3][5] = right[0][3] = 2 - 1
right[3][1] = right[1][2] = right[2][4] = right[4][3] = 1 - 1
right[1][3] = right[2][1] = right[4][2] = right[3][4] = 6 - 1



q = int(sys.stdin.readline())
for i in range(q):
    top, front = sys.stdin.readline().split()
    r_index = right[face.index(top)][face.index(front)]
    print(face[r_index])

Solution (Python):
def f(d, D):
    i = 0
    while i < 6:
        if d == D[i]:
            return i
        else:
            i += 1

Q0 = [1,2,4,3,1]
Q1 = [0,3,5,2,0]
Q2 = [0,1,5,4,0]
ans = ''
D = list(map(int, input().split(' ')))
n = int(input())
i = 0
while i < n:
    d0, d1 = map(int, input().split(' '))
    L0 = f(d0, D)
    L1 = f(d1, D)
    if L0 == 0:
        q = 0
        while q < 4:
            if L1 == Q0[q]:
                ans += str(D[Q0[q + 1]]) + '\n'
                break
            q += 1
    elif L0 == 1:
        q = 0
        while q < 4:
            if L1 == Q1[q]:
                ans += str(D[Q1[q + 1]]) + '\n'
                break
            q += 1
    elif L0 == 2:
        q = 0
        while q < 4:
            if L1 == Q2[q]:
                ans += str(D[Q2[q + 1]]) + '\n'
                break
            q += 1
    elif L0 == 3:
        q = 0
        while q < 4:
            if L1 == Q2[4-q]:
                ans += str(D[Q2[4-q-1]]) + '\n'
                break
            q += 1
    elif L0 == 4:
        q = 0
        while q < 4:
            if L1 == Q1[4-q]:
                ans += str(D[Q1[4-q-1]]) + '\n'
                break
            q += 1
    elif L0 == 5:
        q = 0
        while q < 4:
            if L1 == Q0[4-q]:
                ans += str(D[Q0[4-q-1]]) + '\n'
                break
            q += 1
    i += 1
if ans != '':
    print(ans[:-1])

Solution (Python):
class Dice:
    """サイコロを操作するクラス"""
    def __init__(self, num):
        self.num = num
    def roll_north(self):
        tmp = self.num[:]
        self.num[0] = tmp[1]
        self.num[1] = tmp[5]
        self.num[4] = tmp[0]
        self.num[5] = tmp[4]
    def roll_south(self):
        tmp = self.num[:]
        self.num[0] = tmp[4]
        self.num[1] = tmp[0]
        self.num[4] = tmp[5]
        self.num[5] = tmp[1]
    def roll_east(self):
        tmp = self.num[:]
        self.num[0] = tmp[3]
        self.num[2] = tmp[0]
        self.num[3] = tmp[5]
        self.num[5] = tmp[2]
    def roll_west(self):
        tmp = self.num[:]
        self.num[0] = tmp[2]
        self.num[2] = tmp[5]
        self.num[3] = tmp[0]
        self.num[5] = tmp[3]
    def roll_right(self):
        tmp = self.num[:]
        self.num[1] = tmp[3]
        self.num[2] = tmp[1]
        self.num[3] = tmp[4]
        self.num[4] = tmp[2]
    def print_dice(self):
        print(self.num)
    def print_top(self):
        print(self.num[0])

num = list(map(int, input().split()))
dice = Dice(num)
for i in range(int(input())):
    top, front = map(int, input().split())
    while not(top == dice.num[0] or front == dice.num[1]):
        dice.roll_north()
    while top != dice.num[0]:
        dice.roll_east()
    while front != dice.num[1]:
        dice.roll_right()
    print(dice.num[2])

Solution (Python):
from enum import IntEnum


class Position(IntEnum):
    TOP = 0
    FORESIDE = 1
    RIGHTSIDE = 2
    BACKSIDE = 3
    LEFTSIDE = 4
    BOTTOM = 5


class Dice:
    def __init__(self, v1, v2, v3, v4, v5, v6):
        self.values = {v1: [v1, v2, v3, v5, v4, v6],
                       v2: [v2, v6, v3, v1, v4, v5],
                       v3: [v3, v6, v5, v1, v2, v4],
                       v4: [v4, v6, v2, v1, v5, v3],
                       v5: [v5, v6, v4, v1, v3, v2],
                       v6: [v6, v5, v3, v2, v4, v1]}
        self.set_top(v1)

    def set_top(self, v):
        if v not in self.values:
            raise ValueError()

        self.top = v
        self.status = self.values[v]

    def rotate(self):
        (self.status[Position.FORESIDE], self.status[Position.RIGHTSIDE],
         self.status[Position.BACKSIDE], self.status[Position.LEFTSIDE]) \
         = (self.status[Position.LEFTSIDE], self.status[Position.FORESIDE],
            self.status[Position.RIGHTSIDE], self.status[Position.BACKSIDE])

    def __getitem__(self, side):
        return self.status[side]


def run():
    sides = [int(v) for v in input().split()]
    dice = Dice(*sides)
    cnt = int(input())

    for i in range(cnt):
        v1, v2 = [int(v) for v in input().split()]
        dice.set_top(v1)
        for j in range(4):
            if v2 == dice[Position.FORESIDE]:
                break
            dice.rotate()
        else:
            raise ValueError()

        print('{}'.format(dice[Position.RIGHTSIDE]))


if __name__ == '__main__':
    run()

Solution (Python):
d = input().split()
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(int(input())):
  t, f = [d.index(s) for s in input().split()]
  print(d[p[t][f]])