Problem:
Dice II Construct a dice from a given sequence of integers in the same way as Dice I . You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I . Write a program to print an integer on the right side face.   Input In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given. In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively. Output For each question, print the integer on the right side face. Constraints  $0 \leq $ the integer assigned to a face $ \leq 100$  The integers are all different  $1 \leq q \leq 24$  Sample Input 1 2 3 4 5 6
3
6 5
1 3
3 2 Sample Output 3
5
6

Solution (Python):
class Dice:
    
    def __init__(self, eye1, eye2, eye3, eye4, eye5, eye6):
        self.__eyes = {
            "top": eye1,
            "front": eye2,
            "right": eye3,
            "left": eye4,
            "back": eye5,
            "bottom": eye6
        }
    
    def set_eyes(self, eye1, eye2, eye3, eye4, eye5, eye6):
        self.__eyes["top"] = eye1
        self.__eyes["front"] = eye2
        self.__eyes["right"] = eye3
        self.__eyes["left"] = eye4
        self.__eyes["back"] = eye5
        self.__eyes["bottom"] = eye6
    
    def get_eyes(self, key):
        return self.__eyes[key]

    
    def N(self):
        self.set_eyes(
            self.get_eyes("front"),
            self.get_eyes("bottom"),
            self.get_eyes("right"),
            self.get_eyes("left"),
            self.get_eyes("top"),
            self.get_eyes("back")
            )
    def S(self):
        self.set_eyes(
            self.get_eyes("back"),
            self.get_eyes("top"),
            self.get_eyes("right"),
            self.get_eyes("left"),
            self.get_eyes("bottom"),
            self.get_eyes("front")
            )
        
    def E(self):
        self.set_eyes(
            self.get_eyes("left"),
            self.get_eyes("front"),
            self.get_eyes("top"),
            self.get_eyes("bottom"),
            self.get_eyes("back"),
            self.get_eyes("right")
            )
        
    def W(self):
        self.set_eyes(
            self.get_eyes("right"),
            self.get_eyes("front"),
            self.get_eyes("bottom"),
            self.get_eyes("top"),
            self.get_eyes("back"),
            self.get_eyes("left")
            )
    
    def print_top(self):
        print(self.get_eyes("top"))
    

    def guess_right_from_top_front(self, top, front):
        key_top = None
        key_front = None
        keys = [ 
        "top", 
        "front", 
        "right", 
        "left", 
        "back", 
        "bottom"
        ]
        for key in keys:
            if top == self.get_eyes(key):
                key_top = key
            if front == self.get_eyes(key):
                key_front = key
        keys_for_top = [
            ["front", "right", "back", "left"],
            ["bottom", "right", "top", "left"],
            ["bottom", "back", "top", "front"],
            ["bottom", "front", "top", "back"],
            ["bottom", "left", "top", "right"],
            ["front", "left", "back", "right"]
        ]
        
        for i in range(6):
            if key_top == keys[i]:
                for j in range(4):
                    if key_front == keys_for_top[i][j]:
                        return keys_for_top[i][(j + 1) % 4]

eye1, eye2, eye3, eye4, eye5, eye6 = map(int, input().split())
q = int(input())
dice = Dice(eye1, eye2, eye3, eye4, eye5, eye6)
for _ in range(q):
    top, front = map(int, input().split())
    print(dice.get_eyes(dice.guess_right_from_top_front(top, front)))

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

for _ in range(q):
    problem = [int(x) for x in input().split()]
    
    
    if problem in [[N[0],N[1]],[N[1],N[5]],[N[5],N[4]],[N[4],N[0]]]:
        print(N[2])
    elif problem in [[N[3],N[1]],[N[1],N[2]],[N[2],N[4]],[N[4],N[3]]]:
        print(N[0])
    elif problem in [[N[3],N[5]],[N[5],N[2]],[N[2],N[0]],[N[0],N[3]]]:
        print(N[1])
    elif problem in [[N[4],N[5]],[N[5],N[1]],[N[1],N[0]],[N[0],N[4]]]:
        print(N[3])
    elif problem in [[N[2],N[5]],[N[5],N[3]],[N[3],N[0]],[N[0],N[2]]]:
        print(N[4])
    elif problem in [[N[1],N[3]],[N[3],N[4]],[N[4],N[2]],[N[2],N[1]]]:
        print(N[5])

Solution (Python):

class Dice:
    def __init__(self,a,b,c,d,e,f):
        self.face1 = a
        self.face2 = b
        self.face3 = c
        self.face4 = d
        self.face5 = e
        self.face6 = f

    def above_face(self):
        return self.face1

    def roll(self, order):
        if order == 0:
            tmp = self.face1
            self.face1 = self.face2
            self.face2 = self.face6
            self.face6 = self.face5
            self.face5 = tmp
        elif order == 1:
            tmp = self.face1
            self.face1 = self.face5
            self.face5 = self.face6
            self.face6 = self.face2
            self.face2 = tmp
        elif order == 2:
            tmp = self.face1
            self.face1 = self.face3
            self.face3 = self.face6
            self.face6 = self.face4
            self.face4 = tmp
        else:
            tmp = self.face1
            self.face1 = self.face4
            self.face4 = self.face6
            self.face6 = self.face3
            self.face3 = tmp

a,b,c,d,e,f = map(int,input().split())
dice1 = Dice(a,b,c,d,e,f)
q = int(input())
import random
for i in range(q):
    A,B = map(int,input().split())
    while True:
        dice1.roll(random.randint(0, 3))
        if dice1.face1 == A and dice1.face2 == B:
            break
    print(dice1.face3)

Solution (Python):
class Dice():
    
    state = [0 for _ in range(6)]

    def __init__(self, dice_list):
        self.state[0] = dice_list[0]
        self.state[1] = dice_list[1]
        self.state[2] = dice_list[2]
        self.state[3] = dice_list[3]
        self.state[4] = dice_list[4]
        self.state[5] = dice_list[5]

    def north(self):
        tmp = self.state[0]
        self.state[0] = self.state[1]
        self.state[1] = self.state[5]
        self.state[5] = self.state[4]
        self.state[4] = tmp

    def west(self):
        tmp = self.state[0]
        self.state[0] = self.state[2]
        self.state[2] = self.state[5]
        self.state[5] = self.state[3]
        self.state[3] = tmp

    def south(self):
        tmp = self.state[0]
        self.state[0] = self.state[4]
        self.state[4] = self.state[5]
        self.state[5] = self.state[1]
        self.state[1] = tmp


    def east(self):
        tmp = self.state[0]
        self.state[0] = self.state[3]
        self.state[3] = self.state[5]
        self.state[5] = self.state[2]
        self.state[2] = tmp

    
if __name__ == '__main__':
    L = list(map(int, input().split()))
    N = int(input())
    querys = [list(map(int, input().split())) for _ in range(N)]

    for query in querys:
        dice = Dice(L)

        for i in range(3):
            for j in range(4):
                if dice.state[1] == query[1]:
                    break
                dice.north()
                
            if dice.state[1] == query[1]:
                break

            dice.west()
            

        for i in range(4):
            if dice.state[0] == query[0]:
                break
            dice.west()

        print(dice.state[2])

Solution (Python):
def right_num(m,u):
    cube = [
        {2:3,3:5,4:2,5:4},
        {1:4,3:1,4:6,6:3},
        {1:2,2:6,5:1,6:5},
        {1:5,2:1,5:6,6:2},
        {1:3,3:6,4:1,6:4},
        {2:4,3:2,4:5,5:3},
    ]
    return cube[m][u]

d=input().split()
for i in range(int(input())):
    num = input().split()

    p = d.index(num[0])
    s = int(d.index(num[1])) + 1

    print("{}".format(d[(right_num(p, s)-1)]))

Solution (Python):
ptn=["23542","14631",
"12651","15621",
"13641","32453"]

def rNO(d):
  global ptn
  for i,e in enumerate(ptn):
    if d in e:
      return i
  return 0
      

dice = list(input().split(" "))
n = int(input())

for i in range(n):
  q = input().split()
  for i,e in enumerate(dice):
    if e == q[0] :
      q1 = i + 1
    if e == q[1]:
      q2 = i + 1
  
  qq=str(q1) + str(q2)
  ans = rNO(qq)
  print(dice[ans])

Solution (Python):


class Dice:
    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.order = 'NNNNWNNNWNNNENNNENNNWNNN'
    
    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])
        else:
            self.setNumber(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])
    
    def query(self, top_num, front_num):
        self.save_data = [i for i in range(6)]
        
        for i in range(len(self.number)):
            self.save_data[i] = self.number[i]
        
        ret = -1
        
        for i in range(len(self.order)):
            self.roll(self.order[i])
            if self.number[0] == top_num and self.number[1] == front_num:
                ret = self.number[2]
                break
        return ret;
    

dice = Dice()
table = list(map(int, input().split()))
dice.setNumber(table[0],table[1],table[2],table[3],table[4],table[5])


q = int(input())
for loop in range(q):
    u, f = map(int, input().split())
    print(dice.query(u, f))

Solution (Python):
import random

class Dice():
    def __init__(self, number):
        self.number = number

    def S(self):
        temp = self.number
        self.number = [temp[4], temp[0], temp[2], temp[3], temp[5], temp[1]]

    def N(self):
        temp = self.number
        self.number = [temp[1], temp[5], temp[2], temp[3], temp[0], temp[4]]

    def E(self):
        temp = self.number
        self.number = [temp[3], temp[1], temp[0], temp[5], temp[4], temp[2]]

    def W(self):
        temp = self.number
        self.number = [temp[2], temp[1], temp[5], temp[0], temp[4], temp[3]]

    def order(self, str):
        for ch in str:
            if ch == "S":
                self.S()
            elif ch == "N":
                self.N()
            elif ch == "E":
                self.E()
            else:
                self.W()

    def query(self, top, front):
        while True:
            drc = random.randint(0, 3)
            self.order("NEWS"[drc])
            if self.number[0] == top and self.number[1] == front:
                return self.number[2]
                break

number = list(map(int, input().split()))
q = int(input())
dice = Dice(number)

for i in range(q):
    t, f = map(int, input().split())
    print(dice.query(t, f))

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

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.order = 'NNNNWNNNWNNNENNNENNNWNNN'

    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 query(self,top_num,front_num):

        self.save_data = [i for i in range(6)]
        
        for i in range(len(self.number)):
            self.save_data[i] = self.number[i]

        ret = -1;

        for i in range(len(self.order)):
            self.roll(self.order[i])
            if self.number[0] == top_num and self.number[1] == front_num:
                ret = self.number[2]
                break

        
        for i in range(len(self.number)):
            self.number[i] = self.save_data[i]

        return ret;




dice = Dice()
table = list(map(int,input().split()))
dice.setNumber(table[0], table[1], table[2], table[3], table[4], table[5])

num_query = int(input())
for loop in range(num_query):
    top_num,front_num = map(int,input().split())
    print("%d"%(dice.query(top_num, front_num)))

Solution (Python):
import enum
class Direction(enum.Enum):
    N = "N"
    E = "E"
    W = "W"
    S = "S"

    @classmethod
    def value_of(cls, value):
        return [v for v in cls if v.value == value][0]


class Dice:
    def __init__(self, *value_list):
        self.__value_list = list(value_list)

    def rotate(self, direction):
        l = self.__value_list
        if  direction == Direction.N:
            l[0],l[1],l[5],l[4] = l[1],l[5],l[4],l[0]
        elif  direction == Direction.E:
            l[0],l[2],l[5],l[3] = l[3],l[0],l[2],l[5]
        elif  direction == Direction.W:
            l[0],l[2],l[5],l[3] = l[2],l[5],l[3],l[0]
        elif  direction == Direction.S:
            l[0],l[1],l[5],l[4] = l[4],l[0],l[1],l[5]

    def adjust_top_front(self, top_value, front_value):
        for _ in range(2):
            for _ in range(3):
                if self.front_value() == front_value:
                    break
                self.rotate(Direction.N)
            if self.front_value() == front_value:
                break
            self.rotate(Direction.E)
        
        for _ in range(3):
            if self.top_value() == top_value:
                break
            self.rotate(Direction.E)

    def top_value(self):
        return self.__value_list[0]

    def front_value(self):
        return self.__value_list[1]

    def right_value(self):
        return self.__value_list[2]


dice = Dice(*input().split())
for _ in range(int(input())):
    top,front = input().split()
    dice.adjust_top_front(top, front)
    print(dice.right_value())

Solution (Python):
class dice():
    top = 1
    flont = 2
    right = 3
    def do_N(self):
        temp = self.flont
        self.flont = 7-self.top
        self.top = temp

    def do_S(self):
        temp = 7-self.flont
        self.flont = self.top
        self.top = temp

    def do_E(self):
        temp = self.top
        self.top = 7-self.right
        self.right = temp

    def do_W(self):
        temp = 7-self.top
        self.top = self.right
        self.right = temp

num1 = list(map(int, input().split()))
q = int(input())
dice1 = dice()

for i in range(q):
    top, flont = map(int, input().split())
    if num1[dice1.flont-1] == flont and num1[dice1.top-1] == top:
        ans = num1[dice1.right-1]
    for j in range(4):
        for k in range(4):
            dice1.do_E()
            if num1[dice1.flont-1] == flont and num1[dice1.top-1] == top:
                ans = num1[dice1.right-1]
        dice1.do_N()
        if num1[dice1.flont-1] == flont and num1[dice1.top-1] == top:
            ans = num1[dice1.right-1]

    dice1.do_S()
    dice1.do_E()
    if num1[dice1.flont-1] == flont and num1[dice1.top-1] == top:
        ans = num1[dice1.right-1]
    for j in range(4):
        for k in range(4):
            dice1.do_E()
            if num1[dice1.flont-1] == flont and num1[dice1.top-1] == top:
                ans = num1[dice1.right-1]
        dice1.do_N()
        if num1[dice1.flont-1] == flont and num1[dice1.top-1] == top:
            ans = num1[dice1.right-1]


    print(str(ans))

Solution (Python):

dice1 = [int(d) for d in input().split()]
Q = int(input())

R1 = [
    [0, 1, 2, 3, 4, 5], 
    [3, 1, 0, 5, 4, 2], 
    [2, 1, 5, 0, 4, 3], 
    [4, 0, 2, 3, 5, 1], 
    [1, 5, 2, 3, 0, 4], 
    [5, 4, 2, 3, 1, 0]  
]

R2 = [0, 3, 1, 4, 2, 5] 

for _ in range(Q):
    F, S = map(int, input().split())
    for r1 in R1:
        dice2 = [dice1[i] for i in r1]
        for _ in range(4):
            dice2 = [dice2[i] for i in R2]
            if dice2[0] == F and dice2[1] == S:
                print(dice2[2])

Solution (Python):
F = [int(n) for n in input().split()]
N = int(input())

class Dice():

    def __init__(self,faces):
        self.top = faces[0]
        self.south = faces[1]
        self.east = faces[2]
        self.west = faces[3]
        self.north = faces[4]
        self.bottom = faces[5]

    def rotate(self,d):
        if d == "N":
            tmp = self.top
            self.top = self.south
            self.south = self.bottom
            self.bottom = self.north
            self.north = tmp

        elif d == "S":
            tmp = self.top
            self.top = self.north
            self.north = self.bottom
            self.bottom = self.south
            self.south = tmp

        elif d == "E":
            tmp = self.top
            self.top = self.west
            self.west = self.bottom
            self.bottom = self.east
            self.east = tmp

        elif d == "W":
            tmp = self.top
            self.top = self.east
            self.east = self.bottom
            self.bottom = self.west
            self.west = tmp



    def setTop(self,top):
        if self.south == top:
            self.rotate("N")
        elif self.east == top:
            self.rotate("W")
        elif self.west == top:
            self.rotate("E")
        elif self.north == top:
            self.rotate("S")
        elif self.bottom == top:
            self.rotate("N")
            self.rotate("N")
        

    def setFront(self,front):
        if self.south == front:
            return self.east
        elif self.east == front:
            return self.north
        elif self.west == front:
            return self.south
        elif self.north == front:
            return self.west

    def get_top_value(self):
        return self.top



D = Dice(F)




for _ in range(N):
    top,front = map(int,input().split())
    D.setTop(top)
    print(D.setFront(front))

Solution (Python):
class Dice:
    def __init__(self, num):
        self.num = num
    def roll(self, fn):
        if fn == "S":
            ue = self.num[4]
            self.num[4] = self.num[5]
            self.num[5] = self.num[1]
            self.num[1] = self.num[0]
            self.num[0] = ue
        elif fn == "W":
            ue = self.num[2]
            self.num[2] = self.num[5]
            self.num[5] = self.num[3]
            self.num[3] = self.num[0]
            self.num[0] = ue        
        elif fn == "N":
            ue = self.num[1]
            self.num[1] = self.num[5]
            self.num[5] = self.num[4]
            self.num[4] = self.num[0]
            self.num[0] = ue                
 
    def spin(self, fn):
        if fn == "migi":
            mae = self.num[3]
            self.num[3] = self.num[4]
            self.num[4] = self.num[2]
            self.num[2] = self.num[1]
            self.num[1] = mae
num = list(map(int, input().split(" ")))
dice1 = Dice(num)
n = int(input())
a = []
for i in range(n):
    u, f = map(int, input().split(" "))
    count = 0
    while num[0] != u and count<5:
        dice1.roll("S")
        count+=1
    count = 0
    while num[0] != u and count<5:
        dice1.roll("W")
        count+=1        
    while num[1] != f:
        dice1.spin("migi")
    a.append(num[2])
for i in range(n):
    print(a[i])

Solution (Python):
def Roll(c, lst) :
    if c == 'N' :
        lst[0], lst[1], lst[5], lst[4] = lst[1], lst[5], lst[4], lst[0]
    elif c == 'S' :
        lst[0], lst[1], lst[5], lst[4] = lst[4], lst[0], lst[1], lst[5]
    elif c == 'E' :
        lst[0], lst[2], lst[5], lst[3] = lst[3], lst[0], lst[2], lst[5]
    elif c == 'W' :
        lst[0], lst[2], lst[5], lst[3] = lst[2], lst[5], lst[3], lst[0]
    return lst
    
import copy
dice_pre = list(map(int, input().split()))
N = int(input())
for i in range(N) :
    final_top, final_front = map(int, input().split())
    dice = copy.copy(dice_pre)
    
    for i in range(4) :
        if dice[1] == final_front :
            break
        else :
            Roll('S', dice)
    if dice[1] != final_front :
        Roll('E', dice)
        for i in range(4) :
            if dice[1] == final_front :
                break
            else :
                Roll('S', dice)
    for i in range(4) :
        if dice[0] == final_top :
            break
        else :
            Roll('E', dice)
    
    print(dice[2])

Solution (Python):
class Dice:
    def __init__(self, top: str, front: str, right: str, left: str, back: str, bottom: str) -> None:
        self.top = top
        self.front = front
        self.right = right
        self.left = left
        self.back = back
        self.bottom = bottom

    def print_top(self):
        print(self.top)

    def print_right(self):
        print(self.right)

    def roll(self, direction: str) -> None:
        if direction == 'S':
            self.south()
        elif direction == 'N':
            self.north()
        elif direction == 'E':
            self.east()
        elif direction == 'W':
            self.west()

    def north(self):
        [self.top, self.back, self.bottom, self.front] = [self.front, self.top, self.back, self.bottom]

    def south(self):
        [self.top, self.back, self.bottom, self.front] = [self.back, self.bottom, self.front, self.top]

    def east(self):
        [self.top, self.right, self.bottom, self.left] = [self.left, self.top, self.right, self.bottom]

    def west(self):
        [self.top, self.right, self.bottom, self.left] = [self.right, self.bottom, self.left, self.top]

    def rotate_right(self) -> None:
        for direction in 'SWN':
            dice.roll(direction)


def solve(dice: Dice, top: str, front: str) -> None:
    
    found = False
    for direction in 'SSSS':
        dice.roll(direction)
        if dice.top == top:
            found = True
            break
    if not found:
        for direction in 'EEE':
            dice.roll(direction)
            if dice.top == top:
                break

    
    for _ in range(4):
        dice.rotate_right()
        if dice.front == front:
            break

    
    dice.print_right()


labels = input().split()
dice = Dice(*labels)
n = int(input())
for i in range(n):
    top, front = input().split()
    solve(dice, top, front)

Solution (Python):
class Dice():
    def __init__(self, a, b, c, d, e, f):
        """面の数字とindexを一致させるために0を挿入"""
        self.s = [0, a, b, c, d, e, f]

    def rotate(self, dir):
        if dir == "N":
            self.s[0] = self.s[1] 
            self.s[1] = self.s[2] 
            self.s[2] = self.s[6]
            self.s[6] = self.s[5]
            self.s[5] = self.s[0]
            return

        elif dir == "W":
            self.s[0] = self.s[1]
            self.s[1] = self.s[3] 
            self.s[3] = self.s[6]
            self.s[6] = self.s[4]
            self.s[4] = self.s[0]
            return

        elif dir == "S":
            self.s[0] = self.s[1]
            self.s[1] = self.s[5] 
            self.s[5] = self.s[6]
            self.s[6] = self.s[2]
            self.s[2] = self.s[0]
            return

        elif dir == "E":
            self.s[0] = self.s[1]
            self.s[1] = self.s[4] 
            self.s[4] = self.s[6]
            self.s[6] = self.s[3]
            self.s[3] = self.s[0]
            return
        
        elif dir == "R":
            self.s[0] = self.s[2]
            self.s[2] = self.s[3] 
            self.s[3] = self.s[5]
            self.s[5] = self.s[4]
            self.s[4] = self.s[0]
            return

        elif dir == "L":
            self.s[0] = self.s[2]
            self.s[2] = self.s[4] 
            self.s[4] = self.s[5]
            self.s[5] = self.s[3]
            self.s[3] = self.s[0]
            return

        else:
            return


dice = Dice(*list(map(int, input().split())))

for _ in range(int(input())):
    t, s = map(int, input().split())

    while dice.s[1] != t:
        dice.rotate("S")
        if dice.s[1] != t:
            dice.rotate("E")
    
    while dice.s[2] != s:
        dice.rotate("R")

    print(dice.s[3])

Solution (Python):
class Dice(object):
    def __init__(self, numbers: list):
        self.numbers = dict(zip(("T", "S", "E", "W", "N", "B"), numbers))

    def _roll(self, old_part: tuple):
        new_part = old_part[1:] + (old_part[0],)
        new_numbers = [self.numbers[face] for face in new_part]

        for old, new in zip(old_part, new_numbers):
            self.numbers[old] = new

    def vroll(self, reverse=False):
        """北方向に回転"""
        old_part = ("N", "T", "S", "B")
        self._roll(old_part if not reverse else old_part[::-1])

    def hroll(self, reverse=False):
        """時計回りに回転"""
        old_part = ("N", "W", "S", "E")
        self._roll(old_part if not reverse else old_part[::-1])

    def roll_to(self, direction: str):

        if direction == "N":
            self.vroll()
        elif direction == "S":
            self.vroll(reverse=True)
        elif direction == "E":
            self.hroll(reverse=True)
            self.vroll()
            self.hroll()
        elif direction == "W":
            self.hroll()
            self.vroll()
            self.hroll(reverse=True)
        else:
            raise AssertionError

    def set_pos(self, top, front):
        numbers = self.numbers
        if top in (numbers["E"], numbers["W"]):
            self.hroll()
        while top != numbers["T"]:
            self.vroll()
        while front != numbers["S"]:
            self.hroll()


numbers = list(map(int, input().split()))
dice = Dice(numbers)

outs = []
for _ in range(int(input())):
    top, front = map(int, input().split())
    dice.set_pos(top, front)
    outs.append(dice.numbers["E"])

print(*outs, sep="\n")

Solution (Python):
class Dice:
    def __init__(self, one: int, two: int, three: int, four: int, five: int, six: int) -> None:
        self.__one = one
        self.__two = two
        self.__three = three
        self.__four = four
        self.__five = five
        self.__six = six

    def roll(self, direction) -> None:
        if direction == "N":
            tmp = self.__one
            self.__one = self.__two
            self.__two = self.__six
            self.__six = self.__five
            self.__five = tmp

        if direction == "E":
            tmp = self.__one
            self.__one = self.__four
            self.__four = self.__six
            self.__six = self.__three
            self.__three = tmp

        if direction == "S":
            tmp = self.__one
            self.__one = self.__five
            self.__five = self.__six
            self.__six = self.__two
            self.__two = tmp

        if direction == "W":
            tmp = self.__one
            self.__one = self.__three
            self.__three = self.__six
            self.__six = self.__four
            self.__four = tmp

    def set_top(self, top: int) -> None:
        if dice.top != top:
            for i in range(3):
                dice.roll("N")
                if (dice.top == top):
                    break
        if dice.top != top:
            for i in range(3):
                dice.roll("E")
                if (dice.top == top):
                    break

    def rotate(self) -> None:
        tmp = self.__two
        self.__two = self.__four
        self.__four = self.__five
        self.__five = self.__three
        self.__three = tmp

    @property
    def top(self) -> int:
        return self.__one

    @property
    def front(self) -> int:
        return self.__two

    @property
    def right(self) -> int:
        return self.__three


if __name__ == "__main__":
    one, two, three, four, five, six = map(int, input().split(' '))
    dice = Dice(one, two, three, four, five, six)
    q = int(input())
    for i in range(q):
        top, front = map(int, input().split(' '))
        dice.set_top(top)
        while dice.front != front:
            dice.rotate()
        print(dice.right)

Solution (Python):
class Dice:
    def __init__(self, lst):
        self.a = lst[0]
        self.b = lst[1]
        self.c = lst[2]
        self.d = lst[3]
        self.e = lst[4]
        self.f = lst[5]
        
    def roll(self, orders):
        for order in orders:
            if order=='W':
                tmp = self.a
                self.a = self.c
                self.c = self.f
                self.f = self.d
                self.d = tmp
            elif order=='E':
                tmp = self.a
                self.a = self.d
                self.d = self.f
                self.f = self.c
                self.c = tmp
            elif order=='S':
                tmp = self.a
                self.a = self.e
                self.e = self.f
                self.f = self.b
                self.b = tmp
            elif order=='N':
                tmp = self.a
                self.a = self.b
                self.b = self.f
                self.f = self.e
                self.e = tmp


       
            
import random
if __name__ == '__main__':
    lst = [int(x) for x in input().split()]
    q = int(input())
    questions = [[int(x) for x in input().split()] for _ in range(q)]
    
    dice = Dice(lst)    
    direction = ['N', 'E', 'S', 'W']
    for question in questions:
        """ diceをランダムに回し、所望の状態になったら状態を出力
        """
        while True:
            dice.roll(random.choice(direction))
            if dice.a==question[0] and dice.b==question[1]:
                print(dice.c)
                break