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

def dice_rotation(list1, str1):
    if str1 == "S":
        list1[0],list1[1],list1[4],list1[5] = list1[4],list1[0],list1[5],list1[1]
    elif str1 == "E":
        list1[0],list1[2],list1[3],list1[5] = list1[3],list1[0],list1[5],list1[2]
    elif str1 == "N":
        list1[0],list1[1],list1[4],list1[5] = list1[1],list1[5],list1[0],list1[4]
    elif str1 == "W":
        list1[0],list1[2],list1[3],list1[5] = list1[2],list1[5],list1[0],list1[3]
    return list1


def dice_find(list1, int1, int2):
    position_numnber = list1.index(int2)
    if position_numnber == 0:
        list1 = dice_rotation(list1, "S")
    elif position_numnber == 2:
        list1 = dice_rotation(list1, "W")
        list1 = dice_rotation(list1, "S")
    elif position_numnber == 3:
        list1 = dice_rotation(list1, "E")
        list1 = dice_rotation(list1, "S")
    elif position_numnber == 4:
        list1 = dice_rotation(list1, "S")
        list1 = dice_rotation(list1, "S")
    elif position_numnber == 5:
        list1 = dice_rotation(list1, "N")
    position_numnber = list1.index(int1)
    if position_numnber == 2:
        list1 = dice_rotation(list1, "W")
    elif position_numnber == 3:
        list1 = dice_rotation(list1, "E")
    elif position_numnber == 5:
        list1 = dice_rotation(list1, "W")
        list1 = dice_rotation(list1, "W")
    return list1


input_number = list(map(int, input().split()))
input_count = int(input())
result = list()


for count in range(input_count):
    check_number = list(map(int, input().split()))
    input_number = dice_find(input_number, check_number[0], check_number[1])
    result.append(input_number[2])


[print(i) for i in result]

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

    def N(self):
        self.top, self.front, self.bottom, self.back = self.front, self.bottom, self.back, self.top

    def E(self):
        self.top, self.right, self.bottom, self.left = self.left, self.top, self.right, self.bottom

    def S(self):
        self.top, self.front, self.bottom, self.back = self.back, self.top, self.front, self.bottom

    def W(self):
        self.top, self.right, self.bottom, self.left = self.right, self.bottom, self.left, self.top

    def up_front_to_right(self, top, front):
        if (top, front) in [(self.top, self.front), (self.front, self.bottom), (self.bottom, self.back), (self.back, self.top)]:
            return self.right
        if (top, front) in [(self.front, self.top), (self.bottom, self.front), (self.back, self.bottom), (self.top, self.back)]:
            return self.left
        if (top, front) in [(self.left, self.front), (self.front, self.right), (self.right, self.back), (self.back, self.left)]:
            return self.top
        if (top, front) in [(self.front, self.left), (self.right, self.front), (self.back, self.right), (self.left, self.back)]:
            return self.bottom
        if (top, front) in [(self.top, self.left), (self.left, self.bottom), (self.bottom, self.right), (self.right, self.top)]:
            return self.front
        if (top, front) in [(self.left, self.top), (self.bottom, self.left), (self.right, self.bottom), (self.top, self.right)]:
            return self.back
            
*x,=map(int,input().split())
dice=Dice(x)
q=int(input())
for _ in range(q):
    a,b=map(int,input().split())
    print(dice.up_front_to_right(a,b))

Solution (Python):
tbl = [[0,4,2,5,3,0],[3,0,6,1,0,4],[5,1,0,0,6,2],\
       [2,6,0,0,1,5],[4,0,1,6,0,3],[0,3,5,2,4,0]]
num = (input().split())
ite = int(input())
for i in range(ite):
    T,F = map(int,input().split())
    Tin = num.index(str(T))
    Fin = num.index(str(F))
    print(num[tbl[Fin][Tin]-1])

Solution (Python):
import math

class Dice():
    top   = 0 
    left  = 0 
    right = 0 
    front = 0 
    back  = 0 
    below = 0 

    DEBUG_MODE = False
    def __init__(self,lst,dbg = False):
        if len(lst) == 6:
            self.top   = lst[0]
            self.front = lst[1]
            self.right = lst[2]
            self.left  = lst[3]
            self.back  = lst[4]
            self.below = lst[5]

        if dbg == True:
            self.DEBUG_MODE = True
        else:
            self.DEBUG_MODE = False
    
    def rotate(self,string):
        
        for ch in string:
            if   ch == 'W':
                self.left,self.top,self.right,self.below = self.top,self.right,self.below,self.left

            elif ch == 'E':
                self.left,self.top,self.right,self.below = self.below,self.left,self.top,self.right

            elif ch == 'S':
                self.top,self.back,self.below,self.front = self.back,self.below,self.front,self.top

            elif ch == 'N':
                self.top,self.back,self.below,self.front = self.front,self.top,self.back,self.below
            
            
            if self.DEBUG_MODE:
                self.dbg_PRINT_ALL_VALUE()

    def front_and_top_to_right(self,top,front):

        for i in range(4):
            for j in range(4):
                if self.top == top and self.front == front:
                    return self.right
                self.rotate("W")
            self.rotate("S")
        
        self.rotate("NES") 
        
        for i in range(2):
            for j in range(4):
                if self.top == top and self.front == front:
                    return self.right
                self.rotate("W")
            self.rotate("SS")
        
        return None


        
    def get_topvalue(self):
        return self.top
    
    def dbg_PRINT_ALL_VALUE(self):
        print(f"        bk:{self.back:04} ")
        print(f"le:{self.left:04} tp:{self.top:04} ri:{self.right:04} be:{self.below:04}")
        print(f"        fr:{self.front:04} ")

lst = list(map(int,input().split()))

rep = int(input())
for i in range(rep):
    dice = Dice(lst)
    top,front = list(map(int,input().split()))
    print(dice.front_and_top_to_right(top,front))

Solution (Python):
class Dice:
    def __init__(self, dice):
        self.dice = dice
        self.rule={'N':(1,5,2,3,0,4),'S':(4,0,2,3,5,1),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3)}
    def output(self):
        return self.dice
    def move(self, direction):
        for _ in direction:
            self.dice = [self.dice[y] for y in self.rule[_]]
        return self.dice

dice = Dice([int(x) for x in input().split()])
for _ in range(int(input())):
    tmp = [int(x) for x in input().split()]
    count = 0
    while dice.output()[0] != tmp[0]:
        if count > 4:
            while dice.output()[0] != tmp[0]: dice.move("E")
            break
        else:
            dice.move("N")
            count += 1

    while dice.output()[1] != tmp[1]: dice.move("ESW")
    print(dice.output()[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):
dice = list(map(int, input().split()))
q = int(input())

def dicing(lis, inst):
    if inst == 'N':
        ans_lis = [lis[1],lis[5],lis[2],lis[3],lis[0],lis[4]]
    if inst == 'W':
        ans_lis = [lis[2],lis[1],lis[5],lis[0],lis[4],lis[3]]
    if inst == 'S':
        ans_lis = [lis[4],lis[0],lis[2],lis[3],lis[5],lis[1]]
    if inst == 'E':
        ans_lis = [lis[3],lis[1],lis[0],lis[5],lis[4],lis[2]]
    
    return(ans_lis)

def turn_right(lis):
    inst = list('ENW')
    for i in range(len(inst)):
        lis = dicing(lis, inst[i])
    return(lis)

def find_above(lis, num):
    inst1 = list('NNNEWW')
    for i in range(len(inst1)):
        if lis[0] == num:
            break
        
        lis = dicing(lis, inst1[i])
        if lis[0] == num:
            break
    return(lis)

for i in range(q):
    question = list(map(int, input().split()))
    lis = [1,2,3,4,5,6]
    lis = find_above(lis, dice.index(question[0]) + 1)
    for j in range(4):
        lis = turn_right(lis)
        if dice[lis[1]- 1] == question[1]:
            break

    print(dice[lis[2]-1])

Solution (Python):
class Dice:
    def __init__(self, e1, e2, e3, e4, e5, e6):
        self.eye1 = e1
        self.eye2 = e2
        self.eye3 = e3
        self.eye4 = e4
        self.eye5 = e5
        self.eye6 = e6
    
    def roll_e(self):
        tmp_eye   = self.eye3
        self.eye3 = self.eye1
        self.eye1 = self.eye4
        self.eye4 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_n(self):
        tmp_eye   = self.eye5
        self.eye5 = self.eye1
        self.eye1 = self.eye2
        self.eye2 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_s(self):
        tmp_eye   = self.eye2
        self.eye2 = self.eye1
        self.eye1 = self.eye5
        self.eye5 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_w(self):
        tmp_eye   = self.eye4
        self.eye4 = self.eye1
        self.eye1 = self.eye3
        self.eye3 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_h(self):
        tmp_eye   = self.eye2
        self.eye2 = self.eye3
        self.eye3 = self.eye5
        self.eye5 = self.eye4
        self.eye4 = tmp_eye
    
    def get_eye1(self):
        return self.eye1
    
    def get_eye2(self):
        return self.eye2

    def get_eye3(self):
        return self.eye3

e1, e2, e3, e4 ,e5, e6 = map(int,input().split())
dice1 = Dice(e1, e2, e3, e4 ,e5, e6)
roll = int(input())
for i in range(roll):
    t1, t2 = map(int,input().split())
    while t1 != dice1.get_eye1():
        dice1.roll_e()
        if t1 != dice1.get_eye1():
            dice1.roll_n()
        else:
            break
    while t2 != dice1.get_eye2():
        dice1.roll_h()
    print(dice1.get_eye3())

Solution (Python):
from collections import deque

class Dice():
    
    
    

    def __init__(self, seq):
        self.v = deque([seq[5], seq[4], seq[0], seq[1]])
        self.h = deque([seq[3], seq[0], seq[2], seq[5]])

    def lotate(self, direction):

        if   direction == "E":
            self.h.rotate()
            self.v[2] = self.h[1]
            self.v[0] = self.h[3]

        elif direction == "W":
            self.h.rotate(-1)
            self.v[2] = self.h[1]
            self.v[0] = self.h[3]

        elif direction == "N":
            self.v.rotate(-1)
            self.h[1] = self.v[2]
            self.h[3] = self.v[0]

        elif direction == "S":
            self.v.rotate()
            self.h[1] = self.v[2]
            self.h[3] = self.v[0]

        else:
            pass

    def disp_top_face(self):
        return self.v[2]

    def disp_front_face(self):
        return self.v[3]

    def disp_back_face(self):
        return self.v[1]

    def disp_right_face(self):
        return self.h[2]

    def disp_left_face(self):
        return self.h[0]

    def disp_bottom_face(self):
        return self.h[3]

if __name__ == "__main__":

    param = input().split(" ")

    seq = [int(a) for a in param]
    d = Dice(seq)

    n = int(input())

    for _ in range(n):

        param = input().split()
        top = int(param[0])
        front = int(param[1])

        flag = 1

        while(flag):

            for _ in range(4):
                d.lotate("S")

                if d.disp_front_face() == front :
                    flag = 0
                    break

            if flag != 0:
                d.lotate("E")


        flag = 1

        while(flag):

            for _ in range(4):
                d.lotate("E")

                if d.disp_top_face() == top :
                    flag = 0
                    break

        print(d.disp_right_face())

Solution (Python):
class Dice():
    def __init__(self, dice_labels):
        self.top = dice_labels[0]
        self.south = dice_labels[1]
        self.east = dice_labels[2]
        self.west = dice_labels[3]
        self.north = dice_labels[4]
        self.bottom = dice_labels[5]

    def roll(self, str_direction):
        if str_direction == 'E':
            self.__roll_to_east()
        elif str_direction == 'W':
            self.__roll_to_west()
        elif str_direction == 'S':
            self.__roll_to_south()
        elif str_direction == 'N':
            self.__roll_to_north()

    def turn(self, str_direction):
        if str_direction == 'L':
            self.__turn_left()
        elif str_direction == 'R':
            self.__turn_right()

    def __roll_to_east(self):
        self.top, self.east, self.west, self.bottom = (self.west, self.top, self.bottom, self.east)

    def __roll_to_west(self):
        self.top, self.east, self.west, self.bottom = (self.east, self.bottom, self.top, self.west)

    def __roll_to_south(self):
        self.top, self.south, self.north, self.bottom = (self.north, self.top, self.bottom, self.south)

    def __roll_to_north(self):
        self.top, self.south, self.north, self.bottom = (self.south, self.bottom, self.top, self.north)

    def __turn_right(self):
        self.south, self.east, self.west, self.north = (self.east, self.north, self.south, self.west)

    def __turn_left(self):
        self.south, self.east, self.west, self.north = (self.west, self.south, self.north, self.east)

dice_labels = input().split(' ')
num_question = int(input())
dice = Dice(dice_labels)

for i in range(0, num_question):
    target_top_label, target_for_label = tuple(input().split(' '))
    for j in range(0, 2):
        for k in range(0, 4):
            if dice.top != target_top_label:
                dice.roll('N')
        dice.turn('R')

    for j in range(0, 4):
        if dice.south != target_for_label:
            dice.turn('R')

    print(dice.east)

Solution (Python):
S = [
    (1, 2, 4, 3), 
    (0, 3, 5, 2), 
    (0, 1, 5, 4), 
    (0, 4, 5, 1), 
    (0, 2, 5, 3), 
    (1, 3, 4, 2), 
]

*L, = map(int, input().split())
q = int(input())
for i in range(q):
    a, b = map(int, input().split())
    for i in range(6):
        if L[i] == a:
            for k in range(4):
                if L[S[i][k]] == b:
                    print(L[S[i][k-3]])

Solution (Python):
def lotate(dic, dire):
    if dire == 'N':
        x,y,z,w = dic['up'], dic['back'], dic['bottom'], dic['front']
        dic['back'], dic['bottom'], dic['front'], dic['up'] = x,y,z,w
    elif dire == 'S':
        x, y, z, w = dic['up'], dic['back'], dic['bottom'], dic['front']
        dic['front'], dic['up'], dic['back'], dic['bottom'] = x, y, z, w
    elif dire == 'W':
        x, y, z, w = dic['up'], dic['left'], dic['bottom'], dic['right']
        dic['left'], dic['bottom'], dic['right'], dic['up'] = x, y, z, w
    elif dire == 'E':
        x, y, z, w = dic['up'], dic['left'], dic['bottom'], dic['right']
        dic['right'], dic['up'], dic['left'], dic['bottom'] = x, y, z, w
    return dic


a,b,c,d,e,f = map(int, input().split())
n = int(input())
for _ in range(n):
    dic = {'up': a, 'front': b, 'right': c, 'left': d, 'back': e, 'bottom': f}
    x, y = map(int, input().split())
    s = 'EEENEEENEEESEEESEEENEEEN'
    for i in s:
        if dic['up'] == x and dic['front'] == y:
            print(dic['right'])
            break
        dic = lotate(dic, i)

Solution (Python):
import random


class Dice(object):

    def __init__(self, *args):
        self.faces = [None]
        self.faces.extend(args)
        self.top = self.faces[1]
        self.up = self.faces[5]
        self.down = self.faces[2]
        self.left = self.faces[4]
        self.right = self.faces[3]

    def roll(self, direction):
        if direction == "N":
            self.top, self.up, self.down = self.down, self.top, self.faces[7 - self.faces.index(self.top)]
        elif direction == "S":
            self.top, self.up, self.down = self.up, self.faces[7 - self.faces.index(self.top)], self.top
        elif direction == "W":
            self.top, self.left, self.right = self.right, self.top, self.faces[7 - self.faces.index(self.top)]
        elif direction == "E":
            self.top, self.left, self.right = self.left, self.faces[7 - self.faces.index(self.top)], self.top
        else:
            raise ValueError("{} is not valid direction.".format(direction))


f = [int(i) for i in input().split()]
dice = Dice(*f)

n = int(input())
for i in range(n):
    top, down = [int(i) for i in input().split()]
    while dice.top != top:
        dice.roll(random.choice("NSWE"))
    if dice.down == down:
        print(dice.right)
    elif dice.right == down:
        print(dice.up)
    elif dice.up == down:
        print(dice.left)
    elif dice.left == down:
        print(dice.down)
    else:
        raise ValueError()

Solution (Python):
class Dice():
    def __init__(self, up, back, right, left, front, down):
        self.up = up
        self.down = down
        self.right = right
        self.left = left
        self.front = front
        self.back = back
    
    def north(self):
        temp = self.up
        self.up = self.back
        self.back = self.down
        self.down = self.front
        self.front = temp
    
    def south(self):
        temp = self.up
        self.up = self.front
        self.front = self.down
        self.down = self.back
        self.back = temp
        
    def east(self):
        temp = self.up
        self.up = self.left
        self.left = self.down
        self.down = self.right
        self.right = temp
    
    def west(self):
        temp = self.up
        self.up = self.right
        self.right = self.down
        self.down = self.left
        self.left = temp
    
    def counterclockwise(self):
        temp = self.back
        self.back = self.left
        self.left = self.front
        self.front = self.right
        self.right = temp
    
    def clockwise(self):
        temp = self.back
        self.back = self.right
        self.right = self.front
        self.front = self.left
        self.left = temp
        
    def operate(self, op):
        if op == "N":
            self.north()
        elif op == "S":
            self.south()
        elif op == "E":
            self.east()
        elif op == "W":
            self.west()
        
    def fix_up(self, up):
        if self.down == up:
            self.north()
            self.north()
        elif self.right == up:
            self.west()
        elif self.left == up:
            self.east()
        elif self.front == up:
            self.south()
        elif self.back == up:
            self.north()

    def fix_back_with_fixed_up(self, back):
        if self.right == back:
            self.clockwise()
        elif self.left == back:
            self.counterclockwise()
        elif self.front == back:
            self.clockwise()
            self.clockwise()

up, back, right, left, front, down = map(int, input().split())
dice = Dice(up, back, right, left, front, down)

question_n = int(input())
for _ in range(question_n):
    q_up, q_back = map(int, input().split())
    dice.fix_up(q_up)
    dice.fix_back_with_fixed_up(q_back)
    print(dice.right)

Solution (Python):
def right(a,b,a1,a2,a3,a4,a5,a6):
    b1=a6
    b2=a5
    b3=a4
    b4=a3
    b5=a2
    b6=a1
  
    if a>b:
        tmp_a=a
        a=b
        b=tmp_a
        
    if [a,b]==[a1,a2]:
        right_side=[a3,b3]
    elif [a,b]==[a1,a3]:
        right_side=[a5,b5]
    elif [a,b]==[a1,a4]:
        right_side=[a2,b2]
    elif [a,b]==[a1, a5]:
        right_side=[a4,b4]
    elif [a,b]==[a2, a3]:
        right_side=[a1,b1]
    elif [a, b]==[a2, a4]:
        right_side=[a6,b6]
    elif [a,b]==[a2,a6]:
        right_side=[a3,b3]
    elif [a,b]==[a3, a5]:
        right_side=[a1,b1]
    elif [a,b]==[a3, a6]:
        right_side=[a5,b5]
    elif [a, b]==[a4, a5]:
        right_side=[a6,b6]
    elif [a,b]==[a4,a6]:
        right_side=[a2,b2]
    elif [a,b]==[a5,a6]:
        right_side=[a4,b4]
  
    return right_side
    

initial=list( map(int,input().split()))
num_of_q=int(input())

for i in range(0, num_of_q):
    a=list(map(int, input().split()))
    flag=0
    if a[0]>a[1]:
        flag=1
    answer=right(*a,*initial)
    print(answer[flag])

Solution (Python):
class Dice():
    top=1
    n=2
    e=4
    w=3
    s=5
    dice=[1,2,3,4,5,6]

    top_front_right=[[-1,3,5,2,4,-1],[4,-1,1,6,-1,3],[2,6,-1,-1,1,5],
                     [5,1,-1,-1,6,2],[3,-1,6,1,-1,4],[-1,4,2,5,3,-1]]

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

    def roll(self,direction):
        if(direction=="N"):
            self.top,self.n,self.s = self.n,7-self.top,self.top
        elif(direction=="E"):
            self.top,self.e,self.w = self.e,7-self.top,self.top
        elif(direction=="W"):
            self.top,self.e,self.w = self.w,self.top,7-self.top
        elif(direction=="S"):
            self.top,self.n,self.s = self.s,self.top,7-self.top




d=list(map(int,input().split()))
dice=Dice(d)
n=int(input())
for i in range(n):
    top,front=map(int,input().split())
    print(d[dice.top_front_right[d.index(top)][d.index(front)]-1])

Solution (Python):
class Dice:
    def __init__(self,u,s,e,w,n,d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d
    def rot(self,way):
        if way == 0:
            c = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = c
        if way == 1:
            c = self.e
            self.e = self.n
            self.n = self.w
            self.w = self.s
            self.s = c
        if way == 2:
            c = self.w
            self.w = self.d
            self.d = self.e
            self.e = self.u
            self.u = c
u,s,e,w,n,d = map(int,input().split())
dice = Dice(u,s,e,w,n,d)
questions = int(input())
for q in range(questions):
    top, south = map(int,input().split())
    flag = 0
    count = 0
    while True:
        for v in range(5):
            if dice.u == top and dice.s == south:
                print(dice.e)
                flag = 1
                break
            else:
                dice.rot(0)
        if flag:
            break
        elif count == 5:
            dice.rot(2)
            count = 0
        else:
            dice.rot(1)
            count += 1

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


dm = {0: [1, 2, 4, 3, 1],
      1: [0, 3, 5, 2, 0],
      2: [0, 1, 5, 4, 0],
      3: [0, 4, 5, 1, 0],
      4: [0, 2, 5, 3, 0],
      5: [1, 3, 4, 2, 1]}

for i in range(q):
    t, f = map(lambda x: s.index(int(x)), input().split())
    print(s[dm[t][dm[t].index(f)+1]])

Solution (Python):
class rightnumber():
    def __init__(self):
        self.top = [[0, 0, 0, 0], [4, 2, 3, 5], [4, 6, 3, 1], [1, 2, 6, 5], [1, 5, 6, 2], [1, 3, 6, 4], [4, 5, 3, 2]]
        self.front =[[0, 0, 0, 0], [2, 3, 5, 4], [6, 3, 1, 4], [2, 6, 5, 1], [5, 6, 2, 1], [3, 6, 4, 1], [5, 3, 2, 4]]
inp = list(map(int, input().split()))
q = int(input())
rightnumbers = rightnumber()
top= [0]*q
front = [0]*q
key1 = [0]*q
key2 = [0]*q
for n in range(q):
    top[n], front[n] = map(int, input().split())
    for k in range(6):
        if top[n] == inp[k]:
            key1[n] = k;
        if front[n] == inp[k]:
            key2[n] = k
for n in range(q):
    for i in range(1, 7):
        for j in range(0, 4):
            if rightnumbers.top[i][j] == key1[n]+1 and rightnumbers.front[i][j] == key2[n]+1:
                print(inp[i-1])

Solution (Python):
rot_set = [
    (1,2,4,3,1), 
    (0,3,5,2,0), 
    (0,1,5,4,0), 
    (0,4,5,1,0), 
    (0,2,5,3,0), 
    (1,3,4,2,1)  
]

dice_int = list(map(int, input().split()))
q = int(input())
right_face = []

for _ in range(q):
    a, b = map(int, input().split())
    a_idx = dice_int.index(a)
    b_idx = dice_int.index(b)

    for i in range(6):
        for j in range(4):
            if (a_idx, b_idx) == (rot_set[i][j], rot_set[i][j+1]):
                right_face.append(dice_int[i])

for i in range(q):
    print(right_face[i])

Solution (Python):
import random

class Dice():
    def __init__(self):
        self.face = [i for i in range(6)]
        self.memo = [i for i in range(6)]

    def setface(self,initface):
        for i in range(6):
            self.face[i] = initface[i]

    def roll(self,direction):
        for i in range(6):
            self.memo[i] = self.face[i]
        index = {'E':(3,1,0,5,4,2),'N':(1,5,2,3,0,4),'S':(4,0,2,3,5,1),'W':(2,1,5,0,4,3)}
        for i in range(6):
            self.face[i] = self.memo[index[direction][i]]

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

dice = Dice()
initface = list(map(int,input().split()))
dice.setface(initface)
q = int(input())
for _ in range(q):
    u,f = map(int,input().split())
    while True:
        dice.roll(['E','N','S','W'][random.randint(0,3)])
        if dice.face[0]==u and dice.face[1]==f:
            print(dice.face[2])
            break

Solution (Python):
class dice:
    def __init__(self, f):
        assert(len(f) == 6)
        self.face = f
        self.N = [1, 5, 2, 3, 0, 4]
        self.S = [4, 0, 2, 3, 5, 1]
        self.E = [3, 1, 0, 5, 4, 2]
        self.W = [2, 1, 5, 0, 4, 3]
        self.R = [0, 2, 4, 1, 3, 5]

    def rotate(self, dir):
        rot = self.__getattribute__(dir)
        old = self.face
        new = [-1 for _ in range(6)]
        for i in range(6):
            new[i] = old[rot[i]]
        self.face = new


F = list(map(int, input().split()))
D = dice(F)
m = int(input())
for _ in range(m):
    a, b = map(int, input().split())
    for _ in range(4):
        if a == D.face[0]:
            break
        else:
            D.rotate('N')
    if a != D.face[0]:
        for _ in range(4):
            if a == D.face[0]:
                break
            else:
                D.rotate('E')
    assert(a == D.face[0])
    for _ in range(4):
        if b == D.face[1]:
            break
        else:
            D.rotate('R')
    assert(b == D.face[1])
    print(D.face[2])

Solution (Python):
class Dice():

    def __init__(self, d):
        self.d = d
        self.north = [2, 6, 3, 4, 1, 5]
        self.south = [5, 1, 3, 4, 6, 2]
        self.east = [4, 2, 1, 6, 5, 3]
        self.west = [3, 2, 6, 1, 5, 4]
        self.label = {(d[0], d[1]): d[2]}

    def roll(self, direction):
        if direction == 'S':
            self.d = [ self.d[self.south[i]-1] for i in range(6) ]
        elif direction == 'N':
            self.d = [ self.d[self.north[i]-1] for i in range(6) ]
        elif direction == 'E':
            self.d = [ self.d[self.east[i]-1] for i in range(6) ]
        elif direction == 'W':
            self.d = [ self.d[self.west[i]-1] for i in range(6) ]

    def rollAll(self):
        for i in range(4):
            for i in range(4):
                self.roll('E')
                self.label[(self.d[0], self.d[1])] = self.d[2]
            self.roll('S')
        self.roll('E')
        self.roll('S')
        for i in range(4):
            self.roll('E')
            self.label[(self.d[0], self.d[1])] = self.d[2]
        self.roll('S')
        self.roll('S')
        for i in range(4):
            self.roll('E')
            self.label[(self.d[0], self.d[1])] = self.d[2]

    def getLabel(self):
        return self.label

    def getDice(self):
        return self.d

s = [ int(s) for s in input().split() ]
n = int(input())
dice = Dice(s)
dice.rollAll()
l = dice.getLabel()

for i in range(n):
    a,b = [ int(s) for s in input().split() ]
    print(l[(a,b)])

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

diceB = [[1,2,3,5,4,2],[2,1,4,6,3,1],[3,1,2,6,5,1],[4,1,5,6,2,1],[5,1,3,6,4,1],[6,2,4,5,3,2]]

q = int(input())

for i in range(q):
  quest = list(map(int, input().split()))
  ans = 0
  for j in range(6):
    if quest[0] == diceA[j]:
      for k in range(6):
        if quest[1] == diceA[k]:
          for l in range(1,5):
            if diceB[j][l] == k+1:
              ans = diceB[j][l+1]
  print(diceA[ans-1])

Solution (Python):
import sys


class Dice:
    def __init__(self, top, bottom, front, rear, right, left):
        self.top = top
        self.front = front
        self.right = right
        self.bottom = bottom
        self.rear = rear
        self.left = left

    def rotate_front(self):
        t, b, f, r = self.top, self.bottom, self.front, self.rear
        self.top = r
        self.bottom = f
        self.front = t
        self.rear = b

    def rotate_rear(self):
        t, b, f, r = self.top, self.bottom, self.front, self.rear
        self.top = f
        self.bottom = r
        self.front = b
        self.rear = t

    def rotate_right(self):
        t, b, r, l = self.top, self.bottom, self.right, self.left
        self.top = l
        self.bottom = r
        self.right = t
        self.left = b

    def rotate_left(self):
        t, b, r, l = self.top, self.bottom, self.right, self.left
        self.top = r
        self.bottom = l
        self.right = b
        self.left = t

    def get_tfr(self):
        return [self.top, self.front, self.right]

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    top, front, right, left, rear, bottom = map(int, input().split())
    d = Dice(top, bottom, front, rear, right, left)
    tftor = {}

    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    d.rotate_front()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r

    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    d.rotate_front()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r

    d.rotate_right()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    d.rotate_front()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    

    q = int(input())
    for _ in range(q):
        t, f = map(int, input().split())
        print(tftor[(t, f)])

main()