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

import random
class Dice:
    def __init__(self):
        self.dice=[i for i in range(6)]
        self.workspace=[i for i in range(6)]
        self.top=self.dice=[0]
        
    def set(self,numbers):
        self.dice=[i for i in numbers]
        self.workspace=[i for i in numbers]
        
    def rotate(self,command):
        if command == "W":
            self.workspace=[self.dice[2], self.dice[1], self.dice[5], self.dice[0], self.dice[4], self.dice[3]]
            self.dice=self.workspace
        elif command == "E":
            self.workspace=[self.dice[3], self.dice[1], self.dice[0], self.dice[5], self.dice[4], self.dice[2]]
            self.dice = self.workspace
        
        if command == "S":
            self.workspace=[self.dice[4], self.dice[0], self.dice[2], self.dice[3], self.dice[5], self.dice[1]]
            self.dice=self.workspace
        elif command == "N":
            self.workspace=[self.dice[1], self.dice[5], self.dice[2], self.dice[3], self.dice[0], self.dice[4]]
            self.dice=self.workspace
            
    def get_top(self):
        self.top=self.dice[0]
        print(self.top)
        
    def predict(self, num):
        for i in range(num):
            top, front = map(int, input().split())
            while(True):
                if top == self.dice[0] and front == self.dice[1]:
                    break
                command_num=random.randint(1,4)
                if command_num == 1:
                    self.rotate("W")
                elif command_num == 2:
                    self.rotate("E")
                elif command_num == 3:
                    self.rotate("S")
                elif command_num == 4:
                    self.rotate("N")
            print(self.dice[2])
            
    def getter(self):
        return self.dice[0], self.dice[1], self.dice[2]
    
    def predict_same(self, top, front, right):
        for n in range(30):
            if top ==self.dice[0] and front== self.dice[1] and right ==self.dice[2]:
                print("Yes")
                break
            command_num=random.randint(1,4)
            if command_num == 1:
                    self.rotate("W")
            elif command_num == 2:
                    self.rotate("E")
            elif command_num == 3:
                    self.rotate("S")
            elif command_num == 4:
                    self.rotate("N")
        
        if top!=self.dice[0] or front!=self.dice[1] or front!=self.dice[2]:
            print("No")
        
            
                
    
        
numbers=list(map(int,input().split()))
dice=Dice()
dice.set(numbers)




q=int(input())
dice.predict(q)

Solution (Python):
def N(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[1]
  t1[1]=d1[5]
  t1[2]=d1[2]
  t1[3]=d1[3]
  t1[4]=d1[0]
  t1[5]=d1[4]
  return t1
def S(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[4]
  t1[1]=d1[0]
  t1[2]=d1[2]
  t1[3]=d1[3]
  t1[4]=d1[5]
  t1[5]=d1[1]
  return t1
def E(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[3]
  t1[1]=d1[1]
  t1[2]=d1[0]
  t1[3]=d1[5]
  t1[4]=d1[4]
  t1[5]=d1[2]
  return t1
def W(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[2]
  t1[1]=d1[1]
  t1[2]=d1[5]
  t1[3]=d1[0]
  t1[4]=d1[4]
  t1[5]=d1[3]
  return t1
d1=[0 for i in range(6)]
d1[:]=(int(x) for x in input().split())
q=int(input())

for i in range(q):
  t,f=(int(x) for x in input().split())
  if d1[1]==t:
    d1=N(d1)
  elif d1[2]==t:
    d1=W(d1)
  elif d1[3]==t:
    d1=E(d1)
  elif d1[4]==t:
    d1=S(d1)
  elif d1[5]==t:
    d1=N(d1)
    d1=N(d1)
  
  if d1[2]==f:
    d1=W(d1)
    d1=S(d1)
    d1=E(d1)
  elif d1[3]==f:
    d1=E(d1)
    d1=S(d1)
    d1=W(d1)
  elif d1[4]==f:
    d1=E(d1)
    d1=S(d1)
    d1=S(d1)
    d1=W(d1)
  print(d1[2])

Solution (Python):
class Dice():
    
    def __init__(self, labels):
        self.__pip = labels
        
    def roll_e(self): 
        self.__pip[0], self.__pip[3], self.__pip[5], self.__pip[2] = \
            self.__pip[3], self.__pip[5], self.__pip[2], self.__pip[0]
    
    def roll_n(self): 
        self.__pip[0], self.__pip[1], self.__pip[5], self.__pip[4] = \
            self.__pip[1], self.__pip[5], self.__pip[4], self.__pip[0]

    def roll_s(self): 
        self.__pip[0], self.__pip[4], self.__pip[5], self.__pip[1] = \
            self.__pip[4], self.__pip[5], self.__pip[1], self.__pip[0]

    def roll_w(self): 
        self.__pip[0], self.__pip[2], self.__pip[5], self.__pip[3] = \
            self.__pip[2], self.__pip[5], self.__pip[3], self.__pip[0]

    def roll_nn(self): 
        self.roll_n()
        self.roll_n()
        
    def turn_dice(self): 
        self.__pip[1], self.__pip[2], self.__pip[4], self.__pip[3] = \
            self.__pip[2], self.__pip[4], self.__pip[3], self.__pip[1]
            
    def get_right(self, top, front):
        if top != self.__pip[0]:
            if top == self.__pip[1]:
                self.roll_n()
            elif top == self.__pip[2]:
                self.roll_w()
            elif top == self.__pip[3]:
                self.roll_e()
            elif top == self.__pip[4]:
                self.roll_s()
            else:
                self.roll_nn()
        for i in range(3):
            if front == self.__pip[1]:
                break
            self.turn_dice()
        return self.__pip[2]
        
        
def main():
    dice = Dice(list(input().split()))
    n = int(input())
    for i in range(n):
        top, front = input().split()
        right = dice.get_right(top, front)
        print("{}".format(right))
        

if __name__ == "__main__":
    main()

Solution (Python):
class Dice:
    def __init__(self,dice):
        self.dice = dice
        
    
    
    def dice_num(self,a,b):
        if (a==2 and b == 4) or (a==3 and b==1) or (a==4 and b==3) or (a==1 and b== 2):
            return self.dice[0]
        elif (a==2 and b == 0) or (a==0 and b==3) or (a==3 and b==5) or (a==5 and b== 2):
            return self.dice[1]
        elif (a==4 and b == 0) or (a==0 and b==1) or (a==5 and b==4) or (a==1 and b== 5):
            return self.dice[2]
        elif (a==0 and b == 4) or (a==4 and b==5) or (a==5 and b==1) or (a==1 and b== 0):
            return self.dice[3]
        elif (a==2 and b == 5) or (a==3 and b==0) or (a==0 and b==2) or (a==5 and b== 3):
            return self.dice[4]
        else:
            return self.dice[5]
            
    
    
dice = list(map(int,input().split()))

n = int(input())
        
for _ in range(n):
    a,b = map(int,input().split())
    a_num = dice.index(a)
    b_num = dice.index(b)
    dice_2 = Dice(dice)
    print(dice_2.dice_num(a_num,b_num))

Solution (Python):
class Dice:
    def __init__(self,u,w,r,s,t,y):
        self.s1 = u
        self.s2= w
        self.s3= r
        self.s4= s
        self.s5 = t
        self.s6= y
    def right_s3(self):
        prev_s3 = self.s3
        self.s3 = prev_s3
    def right_s4(self):
        prev_s4 = self.s4
        self.s3 = prev_s4
    def right_s5(self):
        prev_s5 = self.s5
        self.s3 = prev_s5
    def right_s6(self):
        prev_s6 = self.s6
        self.s3 = prev_s6
    def right_s1(self):
        prev_s1 = self.s1
        self.s3 = prev_s1
    def right_s2(self):
        prev_s2 = self.s2
        self.s3 = prev_s2
    def top(self):
        return self.s3
s1,s2,s3,s4,s5,s6 = map(int,input().split())
order = int(input())
for c in range(order):
    dice = Dice(s1,s2,s3,s4,s5,s6)
    new_s1 , new_s2 = map(int,input().split())
    if (new_s1 == s1 and new_s2 == s2) or (new_s1 ==s2 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s5) or (new_s1==s5 and new_s2 == s1):
        dice.right_s3()
    elif (new_s1 == s1 and new_s2 == s5) or (new_s1 ==s5 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s2) or (new_s1==s2 and new_s2 == s1):
        dice.right_s4()
    elif (new_s1 == s1 and new_s2 == s3) or (new_s1 ==s3 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s4) or (new_s1==s4 and new_s2 == s1):
        dice.right_s5()
    elif (new_s1 == s2 and new_s2 == s4) or (new_s1 ==s4 and new_s2 == s5) or (new_s1 == s5 and new_s2 ==s3) or (new_s1==s3 and new_s2 == s2):
        dice.right_s6()
    elif (new_s1 == s2 and new_s2 == s3) or (new_s1 ==s3 and new_s2 == s5) or (new_s1 == s5 and new_s2 ==s4) or (new_s1==s4 and new_s2 == s2):
        dice.right_s1()
    elif (new_s1 == s1 and new_s2 == s4) or (new_s1 ==s4 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s3) or (new_s1==s3 and new_s2 == s1):
        dice.right_s2()
    print(dice.top())

Solution (Python):
N = list(map(int,input().split()))
q = int(input())
S = []
S += [42,23,35,54],
S += [14,46,63,31],
S += [12,26,65,51],
S += [21,15,56,62],
S += [13,36,64,41],
S += [24,45,53,32],
for i in range(q):
    a,b = map(int,input().split())
    c = [str(N.index(a)+1)]
    c += str(N.index(b)+1),
    c = "".join(c)
    for j in range(6):
        if int(c) in S[j]:
            print(N[j])

Solution (Python):
class Dice:
    def __init__(self,dim=list(range(1,7))):
        self.dim = dim

    def roll(self,direction):
        if direction == "N":
            buff = self.dim[0]
            self.dim[0] = self.dim[1]
            self.dim[1] = self.dim[5]
            self.dim[5] = self.dim[4]
            self.dim[4] = buff

        if direction == "E":
            buff = self.dim[3]
            self.dim[3] = self.dim[5]
            self.dim[5] = self.dim[2]
            self.dim[2] = self.dim[0]
            self.dim[0] = buff

        if direction == "W":
            buff = self.dim[0]
            self.dim[0] = self.dim[2]
            self.dim[2] = self.dim[5]
            self.dim[5] = self.dim[3]
            self.dim[3] = buff

        if direction == "S":
            buff = self.dim[0]
            self.dim[0] = self.dim[4]
            self.dim[4] = self.dim[5]
            self.dim[5] = self.dim[1]
            self.dim[1] = buff

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

D = Dice(lst)
q = int(input())
for i in range(q):
    top_front = [int(x) for x in input().split()]
    for op in 'EEENEEENEEESEEESEEENEEEN':
        if D.dim[0] == top_front[0] and D.dim[2] == top_front[1]:
            print(D.dim[4])
            break
        D.roll(op)

Solution (Python):

class Dice:
    def __init__(self, input_dice):
        self.flow = [input_dice[3], input_dice[1], input_dice[0], input_dice[5], input_dice[2], input_dice[4]]
        self.dice = [input_dice[3], input_dice[1], input_dice[0], input_dice[5], input_dice[2], input_dice[4]]
  
    def setNumber(self, n0, n1, n2, n3, n4, n5):
        self.dice[0] = n0
        self.dice[1] = n1
        self.dice[2] = n2
        self.dice[3] = n3
        self.dice[4] = n4
        self.dice[5] = n5
        return self.dice
    
    def roll(self, dir):
        if dir == 'S':
            self.setNumber(self.flow[0], self.flow[2], self.flow[5], self.flow[1], self.flow[4], self.flow[3])
            self.flow = self.dice
            return self.dice
        if dir == 'N':
            self.setNumber(self.flow[0], self.flow[3], self.flow[1], self.flow[5], self.flow[4], self.flow[2])
            self.flow = self.dice
            return self.dice
        if dir == 'W':
            self.setNumber(self.flow[2], self.flow[1], self.flow[4], self.flow[0], self.flow[3], self.flow[5])
            self.flow = self.dice
            return self.dice
        if dir == 'E':
            self.setNumber(self.flow[3], self.flow[1], self.flow[0], self.flow[4], self.flow[2], self.flow[5])
            self.flow = self.dice
            return self.dice

    def right_side(self, ue, mae):
        
        
        
        i = 0
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        
        self.roll('E')
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        
        self.roll('N')
        self.roll('N')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break

dice = list(map(int, input().split()))
n = int(input())
ue = [0]*n
mae = [0]*n
for i in range(n):
    ue[i], mae[i] = map(int, input().split())

d = Dice(dice)
for i in range(n):
    d.right_side(ue[i], mae[i])

Solution (Python):
from random import choice

class dice:
    def __init__(self, X):
        self.x = [0] * 6
        self.x[0] = X[0]
        self.x[1] = X[1]
        self.x[2] = X[2]
        self.x[3] = X[3]
        self.x[4] = X[4]
        self.x[5] = X[5]

    def roll(self, d):
        if d == 'S':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[4], self.x[0], self.x[2], self.x[3], self.x[5], self.x[1]
        elif d == 'E':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[3], self.x[1], self.x[0], self.x[5], self.x[4], self.x[2]
        elif d == 'W':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[2], self.x[1], self.x[5], self.x[0], self.x[4], self.x[3]
        elif d == 'N':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[1], self.x[5], self.x[2], self.x[3], self.x[0], self.x[4]

X = list(map(int, input().split()))
n = int(input())
dice1 = dice(X)
distances = ['S', 'E', 'W', 'N']
for i in range(n):
    x1, x2 = map(int, input().split())
    while True:
        d = choice(distances)
        dice1.roll(d)
        if dice1.x[0] == x1 and dice1.x[1] == x2:
            print(dice1.x[2])
            break

Solution (Python):
import random

class Dice():
    def __init__(self, numbers):
        self.numbers = numbers
        S = [1, 5, 2, 3, 0, 4]
        N = [4, 0, 2, 3, 5, 1]
        W = [3, 1, 0, 5, 4, 2]
        E = [2, 1, 5, 0, 4, 3]
        self.tumble = {0: S, 1: N, 2: W, 3: E}
        
    def rol(self, command):
        prev = self.numbers.copy()
        for i, t in enumerate(self.tumble[command]):
            self.numbers[t] = prev[i]
                
numbers = list(map(int, input().split()))
q = int(input())

dice = Dice(numbers)
for _ in range(q):
    query = list(map(int, input().split()))
    while True:
        dice.rol(random.randint(0, 3))
        if dice.numbers[0] == query[0] and dice.numbers[1] == query[1]:
            print(dice.numbers[2])
            break

Solution (Python):

def dice():
    import random as ran

    dnum = list(map(int,input().split()))
    q = int(input())
    up = dnum[0]
    un = dnum[5]
    ri = dnum[2]
    le = dnum[3]
    fr = dnum[1]
    ba = dnum[4]
    tmp = int(0)
    tmp2 = int(0)
    order = ["S","N","E","W"]

    for i in range(q):
        qup,qfr = (int(x) for x in input().split())
        while True :
            x = ran.choice(order)
            if (x == "S"):
                tmp = fr
                tmp2 = un
                fr = up
                up = ba
                un = tmp
                ba = tmp2

            elif (x == "N"):
                tmp = un
                tmp2 = up
                up = fr
                un = ba
                fr = tmp
                ba = tmp2

            elif (x == "E"):
                tmp = up
                tmp2 = un
                up = le
                un = ri
                ri = tmp
                le = tmp2

            elif (x == "W"):
                tmp = un
                tmp2 = up
                up = ri
                un = le
                ri = tmp
                le = tmp2

            if (up == qup and fr == qfr):
                print(ri)
                break
dice()

Solution (Python):
def rollDice(dice, direction):
    buf = [x for x in dice]
    if direction == "S":
        buf[0] = dice[4]
        buf[1] = dice[0]
        buf[4] = dice[5]
        buf[5] = dice[1]
    elif direction == "E":
        buf[0] = dice[3]
        buf[2] = dice[0]
        buf[3] = dice[5]
        buf[5] = dice[2]
    elif direction == "W":
        buf[0] = dice[2]
        buf[2] = dice[5]
        buf[3] = dice[0]
        buf[5] = dice[3]
    elif direction == "N":
        buf[0] = dice[1]
        buf[1] = dice[5]
        buf[4] = dice[0]
        buf[5] = dice[4]

    for i in range(6):
        dice[i] = buf[i]


def rollDiceSide(dice):
    buf = [x for x in dice]
    buf[1] = dice[2]
    buf[2] = dice[4]
    buf[3] = dice[1]
    buf[4] = dice[3]

    for i in range(6):
        dice[i] = buf[i]


d = list(map(int, input().split()))
n = int(input())

for i in range(n):
    q1, q2 = list(map(int, input().split()))

    wq = d.index(q1)
    if wq == 1:
        rollDice(d, "N")
    elif wq == 2:
        rollDice(d, "W")
    elif wq == 3:
        rollDice(d, "E")
    elif wq == 4:
        rollDice(d, "S")
    elif wq == 5:
        rollDice(d, "S")
        rollDice(d, "S")

    wq = d.index(q2)
    if wq == 2:
        rollDiceSide(d)
    elif wq == 3:
        rollDiceSide(d)
        rollDiceSide(d)
        rollDiceSide(d)
    elif wq == 4:
        rollDiceSide(d)
        rollDiceSide(d)

    print(d[2])

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



class Dice:
    def __init__(self,lst):
        self.face=lst
        self.order="NNNNWNNNWNNNENNNENNNWNNN"
    
    def roll(self,x):
        tmp=self.face
        if x=="E":
            self.face=[tmp[3],tmp[1],tmp[0],tmp[5],tmp[4],tmp[2]]
        if x=="W":
            self.face=[tmp[2],tmp[1],tmp[5],tmp[0],tmp[4],tmp[3]]
        if x=="N":
            self.face=[tmp[1],tmp[5],tmp[2],tmp[3],tmp[0],tmp[4]]
        if x=="S":
            self.face=[tmp[4],tmp[0],tmp[2],tmp[3],tmp[5],tmp[1]]
    
    def showDown(self):
        return self.face[0]
    
    def query(self,lst):
        tmp=self.face
        ans=-1
        for i in self.order:
            self.roll(i)
            if self.face[0]==lst[0] and self.face[1]==lst[1]:
                ans=self.face[2]
                break
        self.face=tmp
        return ans
    
dice=Dice(list(map(int,input().split(" "))))
num=int(input())
q=[list(map(int,input().split())) for i in range(num)]
for i in q:
    print(dice.query(i))

Solution (Python):

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

    def roll(self, direction: str):
        if direction == "N":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.front,
                self.bottom,
                self.right,
                self.left,
                self.top,
                self.back,
            )
        elif direction == "W":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.right,
                self.front,
                self.bottom,
                self.top,
                self.back,
                self.left,
            )
        elif direction == "S":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.back,
                self.top,
                self.right,
                self.left,
                self.bottom,
                self.front,
            )
        elif direction == "E":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.left,
                self.front,
                self.top,
                self.bottom,
                self.back,
                self.right,
            )

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

    def get_top_front(self):
        return f"{self.top} {self.front}"

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










(*label,) = map(int, input().split())
dice = Dice(label)
q = int(input())
for _ in range(q):
    t, f = map(int, input().split())
    for i in "EEEN" * 2 + "EEES" * 2 + "EEEN" * 2:
        if f"{t} {f}" == dice.get_top_front():
            dice.print_right()
            break
        dice.roll(i)

Solution (Python):
class Dice:
    def __init__(self, d1, d2, d3, d4, d5, d6):
        self.d1 = d1
        self.d2 = d2
        self.d3 = d3
        self.d4 = d4
        self.d5 = d5
        self.d6 = d6
 
    def output(self, a, b):
        if (a == self.d1 and b == self.d2) or (a == self.d2 and b == self.d6) \
         or (a == self.d6 and b == self.d5) or (a == self.d5 and b == self.d1):
            print(self.d3)
        elif (a == self.d2 and b == self.d1) or (a == self.d6 and b == self.d2) \
         or (a == self.d5 and b == self.d6) or (a == self.d1 and b == self.d5):
            print(self.d4)
        elif (a == self.d1 and b == self.d3) or (a == self.d3 and b == self.d6) \
         or (a == self.d6 and b == self.d4) or (a == self.d4 and b == self.d1):
            print(self.d5)
        elif (a == self.d3 and b == self.d1) or (a == self.d6 and b == self.d3) \
         or (a == self.d4 and b == self.d6) or (a == self.d1 and b == self.d4):
            print(self.d2)
        elif (a == self.d2 and b == self.d3) or (a == self.d3 and b == self.d5) \
         or (a == self.d5 and b == self.d4) or (a == self.d4 and b == self.d2):
            print(self.d1)
        elif (a == self.d3 and b == self.d2) or (a == self.d5 and b == self.d3) \
         or (a == self.d4 and b == self.d5) or (a == self.d2 and b == self.d4):
            print(self.d6)  

d1, d2, d3, d4, d5, d6 = map(int, input().split())
dice = Dice(d1, d2, d3, d4, d5, d6)
n = int(input())
for _ in range(n):
    a, b = map(int, input().split())
    dice.output(a, b)

Solution (Python):
import copy


class Dice:
    def __init__(self, eyes: [int]):
        self.__eyes = eyes

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

    def right(self):
        return self.__eyes[2]

    def roll_s(self):
        tmp = copy.copy(self.__eyes)
        tmp[0] = self.__eyes[4]
        tmp[1] = self.__eyes[0]
        tmp[2] = self.__eyes[2]
        tmp[3] = self.__eyes[3]
        tmp[4] = self.__eyes[5]
        tmp[5] = self.__eyes[1]
        self.__eyes = tmp

    def roll_e(self):
        tmp = copy.copy(self.__eyes)
        tmp[0] = self.__eyes[3]
        tmp[1] = self.__eyes[1]
        tmp[2] = self.__eyes[0]
        tmp[3] = self.__eyes[5]
        tmp[4] = self.__eyes[4]
        tmp[5] = self.__eyes[2]
        self.__eyes = tmp

    def roll_n(self):
        tmp = copy.copy(self.__eyes)
        tmp[0] = self.__eyes[1]
        tmp[1] = self.__eyes[5]
        tmp[2] = self.__eyes[2]
        tmp[3] = self.__eyes[3]
        tmp[4] = self.__eyes[0]
        tmp[5] = self.__eyes[4]
        self.__eyes = tmp

    def roll_w(self):
        tmp = copy.copy(self.__eyes)
        tmp[0] = self.__eyes[2]
        tmp[1] = self.__eyes[1]
        tmp[2] = self.__eyes[5]
        tmp[3] = self.__eyes[0]
        tmp[4] = self.__eyes[4]
        tmp[5] = self.__eyes[3]
        self.__eyes = tmp

    def turn_right(self):
        tmp = copy.copy(self.__eyes)
        tmp[0] = self.__eyes[0]
        tmp[1] = self.__eyes[3]
        tmp[2] = self.__eyes[1]
        tmp[3] = self.__eyes[4]
        tmp[4] = self.__eyes[2]
        tmp[5] = self.__eyes[5]
        self.__eyes = tmp

    def adjust_top(self, top: int):
        for _ in range(4):
            if self.top() is top:
                break
            self.roll_e()
        for _ in range(4):
            if self.top() is top:
                break
            self.roll_s()

    def adjust_front(self, front: int):
        for _ in range(4):
            if self.front() is front:
                break
            self.turn_right()

    def front(self):
        return self.__eyes[1]

    def roll(self, direction: str):
        if direction == 'N':
            self.roll_n()
        if direction == 'E':
            self.roll_e()
        if direction == 'S':
            self.roll_s()
        if direction == 'W':
            self.roll_w()


def resolve():
    eyes = list(map(int, input().split()))
    n = int(input())

    for _ in range(n):
        dice = Dice(eyes)
        top, front = map(int, input().split())
        dice.adjust_top(top)
        dice.adjust_front(front)
        print(dice.right())


resolve()

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

Solution (Python):
class Dice():
    
    def __init__(self,numlist):
        self.lst=numlist
    
    def roll(self,direction):
        for x in direction:
            if x=="S" : self.lst=[self.lst[4],self.lst[0],self.lst[2],self.lst[3],self.lst[5],self.lst[1]]
            elif x=="N" : self.lst=[self.lst[1],self.lst[5],self.lst[2],self.lst[3],self.lst[0],self.lst[4]]
            elif x=="W" : self.lst=[self.lst[2],self.lst[1],self.lst[5],self.lst[0],self.lst[4],self.lst[3]]
            elif x=="E" : self.lst=[self.lst[3],self.lst[1],self.lst[0],self.lst[5],self.lst[4],self.lst[2]]        
    
    def dice_set_make(self):
        self.dice_set=[]
        for x in ["","W","WW","WWW","SE","SW"]:
            self.work=Dice(self.lst[:])
            self.work.roll(x)
            self.dice_set.append(self.work.lst)
            for _ in range(3):
                self.work.roll("S")
                self.dice_set.append(self.work.lst)
        

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

d1=Dice(lst)
d1.dice_set_make()
dice_set=d1.dice_set

for _ in range(q):
    a,b=map(int,input().split())
    for x in dice_set:
        if x[0]==a and x[1]==b:
            print(x[2])
            break

Solution (Python):
class Dice:
    D = {'E':(3,1,0,5,4,2), 'W':(2,1,5,0,4,3), 'S':(4,0,2,3,5,1), 'N':(1,5,2,3,0,4)}
    def __init__(self, tp, fwd, rs, ls, bk, bm):
        self.nbrs = [tp, fwd, rs, ls, bk, bm]
        self.RS = [[0, rs, bk, fwd, ls, 0], [ls, 0, tp, bm, 0, rs], [fwd, bm, 0, 0, tp, bk], [bk, tp, 0, 0, bm, fwd],
              [rs, 0, bm, tp, 0, ls], [0, ls, fwd, bk, rs, 0]]
    def rll(self, drctn):
        return [self.nbrs[i] for i in self.D[drctn]]
    def rs(self,tp,fwd):
        return self.RS[tp][fwd]


A = input().split()
dice = Dice(A[0], A[1], A[2], A[3], A[4], A[5])
for _ in range(int(input())):
    tp, fwd = input().split()
    print(dice.rs(dice.nbrs.index(tp), dice.nbrs.index(fwd)))