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):
nums = list(map(int, input().split()))
cases = int(input())


class die:
    face = [0, 0, 0, 0, 0, 0]

    def __init__(self, nums):
        self.face = [nums[f] for f in [0, 3, 1, 2, 4, 5]]

    def roll(self, dirs):

        for di in dirs:
            c_face = list(self.face)
            if di == 'S':
                self.face = [c_face[f] for f in [4, 1, 0, 3, 5, 2]]
            elif di == 'N':
                self.face = [c_face[f] for f in [2, 1, 5, 3, 0, 4]]
            elif di == 'W':
                self.face = [c_face[f] for f in [3, 0, 2, 5, 4, 1]]
            elif di == 'E':
                self.face = [c_face[f] for f in [1, 5, 2, 0, 4, 3]]
            

        return list(self.face)


def four_roll(d, t, f):
    for r in 'SSSS':
        d.roll(r)
        if d.face[0] == t and d.face[2] == f:
            print(d.face[3])
            return True


for _ in range(cases):
    t, f = map(int, input().split())
    for md in (' ', 'W', 'E', 'NW', 'NE', 'WW'):
        d = die(nums)
        d.roll(md)
        four_roll(d, t, f)

Solution (Python):
import math

class Dice:
    def __init__(self, top, south, east, west, north, bottom):
        self.top = top
        self.south = south
        self.east = east
        self.west = west
        self.north = north
        self.bottom = bottom

    def roll_east(self):
        tmp = self.top
        self.top = self.west
        self.west = self.bottom
        self.bottom = self.east
        self.east = tmp

    def roll_north(self):
        tmp = self.top
        self.top = self.south
        self.south = self.bottom
        self.bottom = self.north
        self.north = tmp

    def roll_south(self):
        tmp = self.top
        self.top = self.north
        self.north = self.bottom
        self.bottom = self.south
        self.south = tmp

    def roll_west(self):
        tmp = self.top
        self.top = self.east
        self.east = self.bottom
        self.bottom = self.west
        self.west = tmp

    def spin_left(self):
        tmp = self.north
        self.north = self.east
        self.east = self.south
        self.south = self.west
        self.west = tmp

    def check(self, south):
        for x in range(4):
            if south == self.south:
                print(self.east)
                return
            self.spin_left()

    def top_check(self, top, south):
        if top == self.top:
            self.check(south)
        elif top == self.south:
            self.roll_north()
            self.check(south)
        elif top == self.east:
            self.roll_west()
            self.check(south)
        elif top == self.west:
            self.roll_east()
            self.check(south)
        elif top == self.north:
            self.roll_south()
            self.check(south)
        elif top == self.bottom:
            self.roll_south()
            self.roll_south()
            self.check(south)


a, b, c, d, e, f = map(int, input().split())
dice = Dice(a, b, c, d, e, f)
num = int(input())
for x in range(num):
    top, south = map(int, input().split())
    dice.top_check(top, south)

Solution (Python):
v = list(map(int, input().split()))
v.insert(0,-1)
n = int(input())
case = [[],
        [4,2,3,5], 
        [6,3,1,4], 
        [6,5,1,2], 
        [6,2,1,5], 
        [6,4,1,3], 
        [5,3,2,4]] 

for _ in range(n):
    topval, frontval = map(int, input().split())
    topkey, frontkey = v.index(topval), v.index(frontval)
    for i in range(4):
        if case[topkey][i] == frontkey:
            if i != 3:
                rightkey = case[topkey][i+1]
            else:
                rightkey = case[topkey][0]
    print(v[rightkey])

Solution (Python):
class Dice:
        def __init__(self, strList):
                self.label = strList[:]
                self.label[0] = int(self.label[0])
                self.label[1] = int(self.label[1])
                self.label[2] = int(self.label[2])
                self.label[3] = int(self.label[3])
                self.label[4] = int(self.label[4])
                self.label[5] = int(self.label[5])
        def reset(self, strList):
                self.label = strList[:]
                self.label[0] = int(self.label[0])
                self.label[1] = int(self.label[1])
                self.label[2] = int(self.label[2])
                self.label[3] = int(self.label[3])
                self.label[4] = int(self.label[4])
                self.label[5] = int(self.label[5])
        def rollE(self):
                temp = self.label[3]
                self.label[3] = self.label[5]
                self.label[5] = self.label[2]
                self.label[2] = self.label[0]
                self.label[0] = temp
        def rollN(self):
                temp = self.label[0]
                self.label[0] = self.label[1]
                self.label[1] = self.label[5]
                self.label[5] = self.label[4]
                self.label[4] = temp
        def rollA(self):
                temp = self.label[3]
                self.label[3] = self.label[4]
                self.label[4] = self.label[2]
                self.label[2] = self.label[1]
                self.label[1] = temp
        def printDice(self):
                print(self.label)
        def check(self, x, y):
                if x == self.label[0] and y == self.label[1]:
                        return True
                else:
                        return False

strDice = input()
diceList = strDice.split()
dice = Dice(diceList)

num = int(input())

for i in range(num):
        list = input().split()
        a = int(list[0])
        b = int(list[1])
        dice.reset(diceList)
        for j in range(6):
                for k in range(4):
                        dice.rollA()
                        if dice.check(a, b):
                                print(dice.label[2])
                if j % 2 == 0:
                        dice.rollE()
                else:
                        dice.rollN()

Solution (Python):
class dice:
    def __init__(self, first, second, third, fourth, fifth, sixth):
        self.up = first
        self.down = sixth
        self.S = second
        self.E = third
        self.N = fifth
        self.W = fourth
    def spinleft(self):
        temp = self.S
        self.S = self.W
        self.W = self.N
        self.N = self.E
        self.E = temp
    def movetoaim(self, u, S):
        
        if u == self.down:
            self = self.move("E")
            self = self.move("E")
        elif u != self.up:
            if u == self.S:
                self = self.move("N")
            elif u == self.E:
                self = self.move("W")
            elif u == self.N:
                self = self.move("S")
            else:
                self = self.move("E")
        
        if S == self.W:
            self.spinleft()
        elif S == self.N:
            self.spinleft()
            self.spinleft()
        elif S == self.E:
            for i in range(3):
                self.spinleft()
        return self
        
    def move(self, direction):
        if direction == "S":
            temp = self.up
            self.up = self.N
            self.N = self.down
            self.down = self.S
            self.S = temp 
            return self
        elif direction == "E":
            temp = self.up
            self.up = self.W
            self.W = self.down
            self.down = self.E
            self.E = temp
            return self
        elif direction == "N":
            temp = self.up
            self.up = self.S
            self.S = self.down
            self.down = self.N
            self.N = temp
            return self
        else:
            temp = self.up
            self.up = self.E
            self.E = self.down
            self.down = self.W
            self.W = temp
            return self

nums = list(map(int,input().split()))
dice1 = dice(nums[0], nums[1], nums[2], nums[3], nums[4], nums[5])
n = int(input())
for i in range(n):
    aim = list(map(int,input().split()))
    dice1 = dice1.movetoaim(aim[0], aim[1])
    print(str(dice1.E))

Solution (Python):
import random

def rotateDice(direction: str, dice: dict) -> dict:
    if direction == 'S':
        dice['S'], dice['F'], dice['N'], dice['B'] = dice['F'], dice['N'], dice['B'], dice['S']
    elif direction == 'E':
        dice['E'], dice['F'], dice['W'], dice['B'] = dice['F'], dice['W'], dice['B'], dice['E']
    elif direction == 'W':
        dice['W'], dice['F'], dice['E'], dice['B'] = dice['F'], dice['E'], dice['B'], dice['W']
    elif direction == 'N':
        dice['N'], dice['F'], dice['S'], dice['B'] = dice['F'], dice['S'], dice['B'], dice['N']
    return dice


dice_list = list(map(int, input().split()))
dice = {'FSEWNB'[i]: dice_list[i] for i in range(6)}
for i in range(int(input())):
    f, s = map(int, input().split())
    while True:
        dice = rotateDice(random.choice(['S', 'E', 'W', 'N']), dice)
        if dice['F'] == f and dice['S'] == s:
            print(dice['E'])
            break

Solution (Python):

dice = input().split()
q = int(input())
for x in range(q):
    n, m = input().split()
    
    d1 = {dice[0]: "123456", dice[1]: "263415", dice[2]: "326154", dice[3]: "421653", dice[4]: "513462",
          dice[5]: "653421"}
    dice = [dice[int(i) - 1] for i in d1[n]]
    
    d2 = {dice[1]: "123456", dice[2]: "135246", dice[3]: "142536", dice[4]: "154326"}
    dice = [dice[int(i) - 1] for i in d2[m]]
    print(dice[2])

Solution (Python):
f = list(map(int, input().split()))
q = int(input())
for i in range(q):
	a = list(map(int, input().split()))
	if a in [[f[0], f[1]], [f[1], f[5]], [f[5], f[4]], [f[4], f[0]]]:
		print(f[2])
	elif a in [[f[0], f[2]], [f[2], f[5]], [f[5], f[3]], [f[3], f[0]]]:
		print(f[4])
	elif a in [[f[0], f[4]], [f[4], f[5]], [f[5], f[1]], [f[1], f[0]]]:
		print(f[3])
	elif a in [[f[0], f[3]], [f[3], f[5]], [f[5], f[2]], [f[2], f[0]]]:
		print(f[1])
	elif a in [[f[1], f[2]], [f[2], f[4]], [f[4], f[3]], [f[3], f[1]]]:
		print(f[0])
	elif a in [[f[1], f[3]], [f[3], f[4]], [f[4], f[2]], [f[2], f[1]]]:
		print(f[5])

Solution (Python):
class Dice(object):
    def __init__(self, faces):
        self.top = faces[0]
        self.front = faces[1]
        self.right = faces[2]
        self.left = faces[3]
        self.back = faces[4]
        self.bottom = faces[5]

    def rotate(self, command):
        pre_top = self.top
        if command == "N":
            self.top = self.front
            self.front = self.bottom
            self.bottom = self.back
            self.back = pre_top
        elif command == "E":
            self.top = self.left
            self.left = self.bottom
            self.bottom = self.right
            self.right = pre_top
        elif command == "W":
            self.top = self.right
            self.right = self.bottom
            self.bottom = self.left
            self.left = pre_top
        elif command == "S":
            self.top = self.back
            self.back = self.bottom
            self.bottom = self.front
            self.front = pre_top
        elif command == "R":
            pre_front = self.front
            self.front = self.left
            self.left = self.back
            self.back = self.right
            self.right = pre_front


def main() -> None:
    dice_values = list(map(int, input().split()))
    dice_x = Dice(dice_values)

    q = int(input())
    for i in range(q):
        top, front = map(int, input().split())
        for j in range(6):
            if top == dice_x.top:
                break
            dice_x.rotate("N")
        for j in range(6):
            if top == dice_x.top:
                break
            dice_x.rotate("E")
        while front != dice_x.front:
            dice_x.rotate("R")
        print(dice_x.right)


if __name__ == "__main__":
    main()

Solution (Python):
dice = input().split()
div1 = [[0, 1, 5, 4], [0, 3, 5, 2], [1, 3, 4, 2]]
div2 = [[2, 3], [1, 4], [5, 0]]
for i in range(int(input())):
    x = 0
    a, b = [dice.index(j) for j in input().split()]
    for j, k in zip(div1, div2):
        if a in j and b in j:
            if j.index(a) < j.index(b):
                x = k[0]
            else:
                x = k[1]
            if abs(j.index(a) - j.index(b)) == 3:
                x = 5 - x
    print(dice[x])

Solution (Python):
a, b, c, d, e, f = map(int, input().split())

num = int(input())

def dice1(x):
    if x == b:
        return c
    elif x == c:
        return e
    elif x == d:
        return b
    else:
        return d

def dice2(x):
    if x == a:
        return d
    elif x == c:
        return a
    elif x == d:
        return f
    else:
        return c

def dice3(x):
    if x == a:
        return b
    elif x == b:
        return f
    elif x == e:
        return a
    else:
        return e

def dice4(x):
    if x == a:
        return e
    elif x == b:
        return a
    elif x == e:
        return f
    else:
        return b

def dice5(x):
    if x == a:
        return c
    elif x == c:
        return f
    elif x == d:
        return a
    else:
        return d

def dice6(x):
    if x == b:
        return d
    elif x == c:
        return b
    elif x == d:
        return e
    else:
        return c

for i in range(num):
    j, k = map(int, input().split())
    if j == a:
        print(dice1(k))
    elif j == b:
        print(dice2(k))
    elif j == c:
        print(dice3(k))
    elif j == d:
        print(dice4(k))
    elif j == e:
        print(dice5(k))
    else:
        print(dice6(k))

Solution (Python):
labels = input().split()
q = int(input())
dices = [[1, 3, 4, 2], 
         [0, 2, 5, 3], 
         [0, 4, 5, 1], 
         [0, 1, 5, 4], 
         [0, 3, 5, 2], 
         [1, 2, 4, 3]]

for i in range(q):
    n = [labels.index(s) for s in input().split()]
    b = (dices[n[1]].index(n[0]) + 1) % 4
    a = dices[n[1]][b]
    print(labels[a])

Solution (Python):
def f(d):
    s = {'E':"310542",'S':"402351",'R':"031425"}
    a, b = input().split()
    for c in "SE":
        for i in range(4):
            d = [d[int(i)]for i in s[c]]
            for j in range(4):
                d = [d[int(i)]for i in s['R']]
                if d[0] == a and d[1] == b:
                    print(d[2])
                    return

d = input().split()
for n in range(int(input())): f(d)

Solution (Python):
import sys

DIRECTIONS = (
    (0, 0, 1),
    (0, -1, 0),
    (1, 0, 0),
    (-1, 0, 0),
    (0, 1, 0),
    (0, 0, -1)
)

DIRECTION_INDEX = {vec: i for i, vec in enumerate(DIRECTIONS)}


def cross(x, y):
    return (
        x[1] * y[2] - x[2] * y[1],
        x[2] * y[0] - x[0] * y[2],
        x[0] * y[1] - x[1] * y[0]
    )


values = [int(v) for v in sys.stdin.readline().strip().split()]
value_index = {value: i for i, value in enumerate(values)}

q = int(sys.stdin.readline())
for _ in range(q):
    top, front = (
        value_index[value]
        for value in map(int, sys.stdin.readline().strip().split())
    )
    index = DIRECTION_INDEX[cross(DIRECTIONS[top], DIRECTIONS[front])]
    print(values[index])

Solution (Python):
import random

def main():
    dir = ['W', 'N', 'S', 'E']
    dice = Dice([int(a) for a in input().split()])
    n = int(input())
    for _ in range(n):
        top, front = (int(x) for x in input().split())
        while True:
            if top == dice.getSuf(0) and front == dice.getSuf(1):
                print(dice.getSuf(2))
                break
            dice.move(random.choice(dir))


class Dice():
    def __init__(self, surface):
        self.l = surface
 
    def move(self, dir):
        if(dir == 'N'):
            self.l = [self.l[1], self.l[5], self.l[2], self.l[3], self.l[0], self.l[4]]
        elif(dir == 'W'):
            self.l = [self.l[2], self.l[1], self.l[5], self.l[0], self.l[4], self.l[3]]
        elif(dir == 'E'):
            self.l = [self.l[3], self.l[1], self.l[0], self.l[5], self.l[4], self.l[2]]
        elif(dir == 'S'):
            self.l = [self.l[4], self.l[0], self.l[2], self.l[3], self.l[5], self.l[1]]

    def show(self):
        print(self.l[0])
    
    def getSuf(self, surface):
        return self.l[surface]


main()

Solution (Python):
class Dice:
    __TOP = 0
    __FRONT = 1
    __RIGHT = 2
    __LEFT = 3
    __BACK = 4
    __BOTTOM = 5

    def __init__(self, face_val=None):
        self.f_keys = [self.__TOP,  
                       self.__FRONT,  
                       self.__RIGHT,  
                       self.__LEFT,  
                       self.__BACK,  
                       self.__BOTTOM  
                       ]

        if face_val is None or len(face_val) != 6:
            face_val = ['1', '2', '3', '4', '5', '6']

        self.f_key_to_val = {}
        self.f_val_to_key = {}
        for f_key, val in zip(self.f_keys, face_val):
            self.f_key_to_val[f_key] = val
            self.f_val_to_key[val] = f_key

    MOVE_SWAP_FACES = {
        'S': [(__BACK, __TOP), (__TOP, __FRONT), (__FRONT, __BOTTOM), (__BOTTOM, __BACK)],
        'W': [(__RIGHT, __TOP), (__TOP, __LEFT), (__LEFT, __BOTTOM), (__BOTTOM, __RIGHT)],
        'E': [(__LEFT, __TOP), (__TOP, __RIGHT), (__RIGHT, __BOTTOM), (__BOTTOM, __LEFT)],
        'N': [(__FRONT, __TOP), (__TOP, __BACK), (__BACK, __BOTTOM), (__BOTTOM, __FRONT)],
        'RCW' : [(__LEFT, __FRONT), (__FRONT, __RIGHT), (__RIGHT, __BACK), (__BACK, __LEFT)],   
        'RCCW': [(__RIGHT, __FRONT), (__FRONT, __LEFT), (__LEFT, __BACK), (__BACK, __RIGHT)],   
    }

    def dice_move(self, direction_list):
        for direction in direction_list:
            prev_faces = self.f_keys[:]
            for prev_f, next_f in self.MOVE_SWAP_FACES[direction]:
                self.f_keys[next_f] = prev_faces[prev_f]
            

    MOVE_TO_TOP = {__TOP:[], __FRONT:['N'], __RIGHT:['W'], __LEFT:['E'], __BACK:['S'], __BOTTOM:['N','N']}

    def dice_fix(self, top, front):
        if type(top) == str:
            top = self.f_val_to_key[top]
            front = self.f_val_to_key[front]

        
        if top != self.f_keys[self.__TOP]:
            now_top = self.f_keys.index(top)
            move = self.MOVE_TO_TOP[now_top]
            self.dice_move(move)

        if top != self.f_keys[self.__TOP]:
            raise AssertionError 

        
        if front != self.f_keys[self.__FRONT]:
            for i in range(3):
                self.dice_move(['RCW'])
                if front == self.f_keys[self.__FRONT]:
                    break
            else:
                raise AssertionError    

    def get_value(self, face):
        return self.f_key_to_val[self.f_keys[face]]

    def __repr__(self):
        return f'({",".join(map(str, self.f_keys))})'


faces = input().split()
nq = int(input())

dice = Dice(faces)
for q in range(nq):
    top_val, front_val = input().split()
    dice.dice_fix(top=top_val, front=front_val)
    print(dice.get_value(Dice._Dice__RIGHT))

Solution (Python):



class Dice:
    def __init__(self, *num):
        if len(num) != 6:
            raise ValueError("6!")
        self.numbers = num

    def east(self):
        index = (3, 1, 0, 5, 4, 2)
        self.numbers = tuple((self.numbers[i] for i in index))

    def west(self):
        index = (2, 1, 5, 0, 4, 3)
        self.numbers = tuple((self.numbers[i] for i in index))

    def south(self):
        index = (4, 0, 2, 3, 5, 1)
        self.numbers = tuple((self.numbers[i] for i in index))

    def north(self):
        index = (1, 5, 2, 3, 0, 4)
        self.numbers = tuple((self.numbers[i] for i in index))

    def rot_top(self):
        """topを変えないように時計周りにくるくる回す"""
        index = (0, 2, 4, 1, 3, 5)
        self.numbers = tuple((self.numbers[i] for i in index))

    @property
    def top(self):
        return self.numbers[0]

    @property
    def front(self):
        return self.numbers[1]

    @property
    def right(self):
        return self.numbers[2]

    def __repr__(self):
        return "Dice(" + ", ".join([str(i) for i in self.numbers]) + ")"


def main():
    num = [int(i) for i in input().rstrip().split()]
    dice = Dice(*num)
    n = int(input().rstrip())
    for _ in range(n):
        top, front = (int(i) for i in input().rstrip().split())

        
        for i in range(4):
            if dice.top == top:
                break
            dice.east()

        for i in range(4):
            if dice.top == top:
                break
            dice.north()

        
        for i in range(4):
            if dice.front == front:
                break
            dice.rot_top()

        print(dice.right)


if __name__ == "__main__":
    main()

Solution (Python):
class dice:
    def __init__(self,label):
        self.top, self.front, self.right, self.left, self.rear, self.bottom \
                = label
  
    def roll(self,op):
        if   op=='N': self.top, self.front, self.bottom, self.rear = \
                                self.front, self.bottom, self.rear , self.top
        elif op=='E': self.top, self.left , self.bottom, self.right = \
                                self.left , self.bottom, self.right, self.top
        elif op=='W': self.top, self.right, self.bottom, self.left = \
                                self.right, self.bottom, self.left , self.top
        elif op=='S': self.top, self.rear , self.bottom, self.front = \
                                self.rear , self.bottom, self.front, self.top
  
    def print_top(self): print(self.top)
    def print_right(self): print(self.right)
  
    def get_label(self):
        return [self.top, self.front, self.right, self.left, self.rear, self.bottom]
label = list(map(int,input().split()))
d = dice(label)
q = int(input())
for _ in range(q):
    top_front = [int(x) for x in input().split()]
    for op in 'EEENEEENEEESEEESEEENEEEN':
        if d.get_label()[:2] == top_front:
            d.print_right()
            break
        d.roll(op)

Solution (Python):


class Dice:
    diceNum = []
    pos = 0
    def setNum(self,i):
        self.pos = i[0]
        self.diceNum.append(i[0])
        self.diceNum.append(i[1])
        self.diceNum.append(i[2])
        self.diceNum.append(i[3])
        self.diceNum.append(i[4])
        self.diceNum.append(i[5])
    def getTopPos(self):
        return self.diceNum[0]    
    def moveToTopnum(self,n):
        i = 0
        for i in range(0,4):
            if (self.getTopPos() == n ):
                return True
            self.move("S")
        else:
            for j in range(0,4):
                if (self.getTopPos() == n ):
                    return True
        return False
    def getFrontPos(self):
        return self.diceNum[1]
    def getRightPos(self):
        return self.diceNum[2]
    def yokoRotate(self):
            i0=self.diceNum[0]
            i1=self.diceNum[2] 
            i2=self.diceNum[4] 
            i3=self.diceNum[1]
            i4=self.diceNum[3]
            i5=self.diceNum[5]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
    def move(self,d):
        if (d == "E"):
            i0=self.diceNum[3]
            i1=self.diceNum[1] 
            i2=self.diceNum[0] 
            i3=self.diceNum[5]
            i4=self.diceNum[4]
            i5=self.diceNum[2]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
        if (d == "W"):
            i0=self.diceNum[2]
            i1=self.diceNum[1] 
            i2=self.diceNum[5] 
            i3=self.diceNum[0]
            i4=self.diceNum[4]
            i5=self.diceNum[3]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
        if (d == "S"):
            i0=self.diceNum[4]
            i1=self.diceNum[0] 
            i2=self.diceNum[2] 
            i3=self.diceNum[3]
            i4=self.diceNum[5]
            i5=self.diceNum[1]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
        if (d == "N"):
            i0=self.diceNum[1]
            i1=self.diceNum[5] 
            i2=self.diceNum[2] 
            i3=self.diceNum[3]
            i4=self.diceNum[0]
            i5=self.diceNum[4]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
 
l = list(map(int,input().split()))
dice = Dice()
dice.setNum(l)

n = int(input())
while (n > 0):
    n = n -1
    (t,f) = map(int,input().split())
    dice.moveToTopnum(t)
    for i in range(0,3):
        if ( dice.getFrontPos() == f):
            break
        dice.yokoRotate()
    print (dice.getRightPos())

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

def Check(Dice,P):
    if P == [3,1] or P ==[4,3] or P ==[2,4] or P ==[1,2]:
        a=0
    elif P == [2,0] or P ==[5,2] or P ==[3,5] or P ==[0,3]:
        a=1
    elif P == [0,1] or P ==[1,5] or P ==[5,4] or P ==[4,0]:
        a=2
    elif P == [1,0] or P ==[5,1] or P ==[4,5] or P ==[0,4]:
        a=3
    elif P == [0,2] or P ==[2,5] or P ==[5,3] or P ==[3,0]:
        a=4
    elif P == [1,3] or P ==[3,4] or P ==[4,2] or P ==[2,1]:
        a=5
    return a

for i in range(q):
    l1=list(map(int,input().split()))
    l2=[d.index(l1[0]),d.index(l1[1])]
    print(d[Check(d,l2)])

Solution (Python):
dice = list(map(int, input().split()))
q = int(input())

anslist = []

for _ in range(q):
    up, front = map(int, input().split())
    if up == dice[1]:
        slist = "N"
    elif up == dice[2]:
        slist = "W"
    elif up == dice[3]:
        slist = "E"
    elif up == dice[4]:
        slist = "S"
    elif up == dice[5]:
        slist = "NN"
    else:
        slist = ""

    for s in slist:
        if s == "N":
            next = list("263415")
        elif s == "E":
            next = list("421653")
        elif s == "S":
            next = list("513462")
        else:
            next = list("326154")

        tmp = [0] * 6
        for i, n in enumerate(next):
            tmp[i] = dice[int(n)-1]

        dice = tmp

    if front == dice[1]:
        ans = dice[2]
    elif front == dice[2]:
        ans = dice[4]
    elif front == dice[3]:
        ans = dice[1]
    elif front == dice[4]:
        ans = dice[3]

    anslist.append(ans)

for ans in anslist:
    print(ans)

Solution (Python):
d=list(map(int,input().split()))
for i in range(int(input())):
	o=list(map(int,input().split()))
	if o[0]==d[0]:
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
			
	elif o[0]==d[1]:
		d[0],d[1],d[4],d[5]=d[1],d[5],d[0],d[4]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
			
	elif o[0]==d[2]:
		d[0],d[2],d[3],d[5]=d[2],d[5],d[0],d[3]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
		
	elif o[0]==d[3]:
		d[0],d[2],d[3],d[5]=d[3],d[0],d[5],d[2]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
		
	elif o[0]==d[4]:
		d[0],d[1],d[4],d[5]=d[4],d[0],d[5],d[1]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
			
	elif o[0]==d[5]:
		d[0],d[1],d[4],d[5]=d[5],d[4],d[1],d[0]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])

Solution (Python):
def swap(dice,i,j,k,l):
    x=dice[l]
    dice[l]=dice[k]
    dice[k]=dice[j]
    dice[j]=dice[i]
    dice[i]=x
    return dice

def Sroll(dice):
    dice=swap(dice,0,1,5,4)
    return dice

def Eroll(dice):
    dice=swap(dice,0,2,5,3)
    return dice

def Wroll(dice):
    dice=swap(dice,0,3,5,2)
    return dice

def Nroll(dice):
    dice=swap(dice,0,4,5,1)
    return dice




dice=list(map(int,input().split()))
n=int(input())
for i in range(0,n):
    top,fro=map(int,input().split())
    for j in range(0,8):
        if(fro==dice[1]):
            break
        if(j==3):
            dice=Eroll(dice)
        dice=Sroll(dice)
    while(top!=dice[0]):
        dice=Eroll(dice)
    print(dice[2])

Solution (Python):
class dice:
    def __init__(self, d, t, s):
        self.d = d
        self.t = t
        self.s = s
    def top(self):
        from collections import OrderedDict
        p = OrderedDict()
        for k, v in zip(["T", "N", "S", "E", "W", "B"], [self.d[0], self.d[4], self.d[1], self.d[2], self.d[3], self.d[5]]):
            p[k] = v
        import random
        while True:
            c = random.choice(["N", "S", "E", "W"])
            t, n, s, e, w, b = p["T"], p["N"], p["S"], p["E"], p["W"], p["B"]
            
            if t == self.t and s == self.s:
                print(e)
                break
            if c == "N":
                
                p["N"], p["T"], p["S"], p["B"]= t, s, b, n
            if c == "S":
                p["N"], p["T"], p["S"], p["B"]= b, n, t, s
            if c == "E":
                p["E"], p["T"], p["W"], p["B"]= t, w, b, e
            if c == "W":
                p["E"], p["T"], p["W"], p["B"]= b, e, t, w
        
        

d = list(map(int, input().split()))        
for i in range(int(input())):
    t, s = map(int, input().split())
    dice1 = dice(d, t, s)
    dice1.top()

Solution (Python):
from enum import Enum
import sys
import math



class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.pattern = 'NNNNENNNENNNWNNNWNNNENNN'
        
    def setNumber(self, n0, n1, n2, n3, n4, n5):
        self.number[0] = n0
        self.number[1] = n1
        self.number[2] = n2
        self.number[3] = n3
        self.number[4] = n4
        self.number[5] = n5
        
    def roll(self, loc):
        for i in range(6):
            self.work[i] = self.number[i]
        
        if loc == 'E':
            self.setNumber(self.work[3], self.work[1], self.work[0], self.work[5], self.work[4], self.work[2])
        elif loc == 'N':
            self.setNumber(self.work[1], self.work[5], self.work[2], self.work[3], self.work[0], self.work[4])
        elif loc == 'S':
            self.setNumber(self.work[4], self.work[0], self.work[2], self.work[3], self.work[5], self.work[1])
        elif loc == 'W':
            self.setNumber(self.work[2], self.work[1], self.work[5], self.work[0], self.work[4], self.work[3])

    def match(self, top, front):
        for i in range(6):
            self.work[i] = self.number[i]
        
        ret = 0
        
        for i in range(len(self.pattern)):
            self.roll(self.pattern[i])
            if top == self.number[0] and front == self.number[1]:
                ret = self.number[2]
                break
                
    def get_Right(self):
        return self.number[2]        
        
dice = Dice()
table = list(map(int, input().split()))
dice.setNumber(table[0], table[1], table[2], table[3], table[4], table[5])

top_fro = []
q = int(input())
for i in range(q):
    top_fro.append(list(map(int, input().split())))

for i in range(q):
    dice.match(top_fro[i][0], top_fro[i][1])
    print("%d"%(dice.get_Right()))