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):
        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
    def get_Top(self):
        return self.number[0]

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(dice.query(top_num,front_num))

Solution (Python):
input_data = input().split(" ")
items = [int(cont) for cont in input_data]
keys = ['T', 'S', 'E', 'W', 'N', 'B']
dice = dict(zip(keys, items))

def q1():
    def rot_func_list(rot, dice):
        if rot == "N":
            keys = ['T', 'S', 'B', 'N']
            items = dice['S'], dice['B'], dice['N'], dice['T']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

        if rot == "S":
            keys = ['T', 'S', 'B', 'N']
            items = dice['N'], dice['T'], dice['S'], dice['B']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

        if rot == "E":
            keys = ['T', 'E', 'B', 'W']
            items = dice['W'], dice['T'], dice['E'], dice['B']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

        if rot == "W":
            keys = ['T', 'E', 'B', 'W']
            items = dice['E'], dice['B'], dice['W'], dice['T']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

    controls = list(input())
    for i in controls:
        rot_func_list(i, dice)
    print(dice["T"])

def q2():

    def search_surf(conds, dice):
        a,b=conds
        a_key = [k for k, v in dice.items() if v == a ]
        b_key = [k for k, v in dice.items() if v == b ]
        key_list = a_key + b_key

        part_st = ''.join(key_list)

        def search(part_st):
            if part_st in "TNBST":
                return "W"

            if part_st in "TSBNT":
                return "E"

            if part_st in "TEBWT":
                return "N"

            if part_st in "TWBET":
                return "S"

            if part_st in "NESWN":
                return "B"

            if part_st in "NWSEN":
                return "T"


        target_key = search(part_st)
        print(dice[target_key])

    a = input()
    repeater = int(a)

    for neee in range(repeater):
        control_input = input().split(" ")
        conds = [int(i) for i in control_input]
        search_surf(conds, dice)

q2()

Solution (Python):
dct = {
    0: (0, 0, 1),
    5: (0, 0, -1),
    2: (0, 1, 0),
    3: (0, -1, 0),
    1: (1, 0, 0),
    4: (-1, 0, 0)
}

def calc(x, y):
    res = (
        x[1] * y[2] - x[2] * y[1],
        x[2] * y[0] - x[0] * y[2],
        x[0] * y[1] - x[1] * y[0]
    )
    return res

if __name__ == "__main__":
    a = [int(x) for x in input().split()]
    Q = int(input())

    for _ in range(Q):
        x, y = map(int, input().split())
        
        for i, value in enumerate(a):
            if x == value:
                x_ind = i
            elif y == value:
                y_ind = i
        
        s, t = dct[x_ind], dct[y_ind]

        res = calc(s,t)
        for key in dct:
            if dct[key] == res:
                print(a[key])
                break

Solution (Python):
class Dice:
    def __init__(self):
        self.d = list(map(int, input().split()))

    def setNum(self, n0, n1, n2, n3, n4, n5):
        self.d[0], self.d[1], self.d[2], self.d[3], self.d[4], self.d[5] = (
            n0,
            n1,
            n2,
            n3,
            n4,
            n5,
        )

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

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

    def setTop(self, t):
        if self.d[0] == t:
            return
        elif self.d[1] == t:
            self.roll("N")
        elif self.d[2] == t:
            self.roll("W")
        elif self.d[3] == t:
            self.roll("E")
        elif self.d[4] == t:
            self.roll("S")
        elif self.d[5] == t:
            self.roll("N")
            self.roll("N")

    def getRight(self, f):
        if self.d[2] == f:
            return self.d[4]
        elif self.d[3] == f:
            return self.d[1]
        elif self.d[4] == f:
            return self.d[3]
        else:
            return self.d[2]


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

Solution (Python):
class Dice():
    def __init__(self, l):
        self.value = l
    def roll(self,s):
        new_list = [0]*6
        if s == "W":
            new_list[0] = self.value[2]
            new_list[1] = self.value[1]
            new_list[2] = self.value[5]
            new_list[3] = self.value[0]
            new_list[4] = self.value[4]
            new_list[5] = self.value[3]
        if s == "E":
            new_list[0] = self.value[3]
            new_list[1] = self.value[1]
            new_list[2] = self.value[0]
            new_list[3] = self.value[5]
            new_list[4] = self.value[4]
            new_list[5] = self.value[2]
        if s == "N":
            new_list[0] = self.value[1]
            new_list[1] = self.value[5]
            new_list[2] = self.value[2]
            new_list[3] = self.value[3]
            new_list[4] = self.value[0]
            new_list[5] = self.value[4]
        if s == "S":
            new_list[0] = self.value[4]
            new_list[1] = self.value[0]
            new_list[2] = self.value[2]
            new_list[3] = self.value[3]
            new_list[4] = self.value[5]
            new_list[5] = self.value[1]
        self.value = new_list

l = list(map(int,input().split()))
Dice1 = Dice(l)
n = int(input())

for i in range(n):
    one,two = map(int,input().split())
    one_index = Dice1.value.index(one)
    two_index = Dice1.value.index(two)
    if (one_index == 0 and two_index == 1) or (one_index == 1 and two_index == 5) or (one_index == 5 and two_index == 4) or (one_index == 4 and two_index == 0):
        print(Dice1.value[2])
    if (one_index == 1 and two_index == 0) or (one_index == 5 and two_index == 1) or (one_index == 4 and two_index == 5) or (one_index == 0 and two_index == 4):
        print(Dice1.value[3])
    if (one_index == 0 and two_index == 2) or (one_index == 2 and two_index == 5) or (one_index == 5 and two_index == 3) or (one_index == 3 and two_index == 0):
        print(Dice1.value[4])
    if (one_index == 2 and two_index == 0) or (one_index == 5 and two_index == 2) or (one_index == 3 and two_index == 5) or (one_index == 0 and two_index == 3):
        print(Dice1.value[1])
    if (one_index == 1 and two_index == 2) or (one_index == 2 and two_index == 4) or (one_index == 4 and two_index == 3) or (one_index == 3 and two_index == 1):
        print(Dice1.value[0])
    if (one_index == 2 and two_index == 1) or (one_index == 4 and two_index == 2) or (one_index == 3 and two_index == 4) or (one_index == 1 and two_index == 3):
        print(Dice1.value[5])

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

Solution (Python):
class Dice:
    def __init__(self,top,front,right,left,back,bottom):
        self.top, self.bottom=top,bottom
        self.right, self.left=right,left
        self.front, self.back=front,back
    def turn(self,order):
        if order=="N":
            self.top,self.front,self.bottom,self.back=self.front,self.bottom,self.back,self.top
        if order=="S":
            self.top,self.front,self.bottom,self.back=self.back,self.top,self.front,self.bottom
        if order=="E":
            self.top,self.right,self.bottom,self.left=self.left,self.top,self.right,self.bottom
        if order=="W":
            self.top,self.right,self.bottom,self.left=self.right,self.bottom,self.left,self.top

d=map(int,raw_input().split())
dice=Dice(d[0],d[1],d[2],d[3],d[4],d[5])
q=input()
for i in range(q):
    t,f=map(int,raw_input().split())
    if t==d[0]:
        if f==d[1]:
            print d[2]
        elif f==d[2]:
            print d[4]
        elif f==d[4]:
            print d[3]
        elif f==d[3]:
            print d[1]
    elif t==d[1]:
        if f==d[0]:
            print d[3]
        elif f==d[3]:
            print d[5]
        elif f==d[5]:
            print d[2]
        elif f==d[2]:
            print d[0]
    elif t==d[2]:
        if f==d[0]:
            print d[1]
        elif f==d[1]:
            print d[5]
        elif f==d[5]:
            print d[4]
        elif f==d[4]:
            print d[0]
    elif t==d[3]:
        if f==d[0]:
            print d[4]
        elif f==d[4]:
            print d[5]
        elif f==d[5]:
            print d[1]
        elif f==d[1]:
            print d[0]
    elif t==d[4]:
        if f==d[0]:
            print d[2]
        elif f==d[2]:
            print d[5]
        elif f==d[5]:
            print d[3]
        elif f==d[3]:
            print d[0]
    elif t==d[5]:
        if f==d[1]:
            print d[3]
        elif f==d[3]:
            print d[4]
        elif f==d[4]:
            print d[2]
        elif f==d[2]:
            print d[1]
    else:
        print "no answer"

Solution (Python):
def move(lon, lat, com):
    target, other, pop_index, insert_index = lon, lat, 0, 3
    if com == "E" or com == "W":
        target, other = lat, lon
    if com == "S" or com == "W":
        pop_index, insert_index = 3, 0
    target.insert(insert_index, target.pop(pop_index))
    other[0], other[2] = target[0], target[2]


def set_upper(lon, lat, upper):
    com = "N" if upper in lon else "E"
    for _i in range(4):
        move(lon, lat, com)
        if lon[0] == upper:
            break


def rotate(lon, lat, com):
    if com == "R":
        lon[1], lat[1], lon[3], lat[3] = lat[3], lon[1], lat[1], lon[3]
    elif com == "L":
        lon[1], lat[1], lon[3], lat[3] = lat[1], lon[3], lat[3], lon[1]


longitude = [1, 2, 6, 5]
latitude = [1, 4, 6, 3]
values = list(input().split())
n = int(input())
for i in range(n):
    U, F = input().split()
    set_upper(longitude, latitude, values.index(U) + 1)
    for j in range(3):
        if longitude[1] == values.index(F) + 1:
            break
        rotate(longitude, latitude, "R")
    print(values[latitude[3] - 1])

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 get_Top(self):
        return self.number[0]
        
    def query(self, top_num, front_num):
        self.save_data = [i for i in range(6)]
        for i in range(6):
            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(6):
            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 sys
import math



class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.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):
faces = input().split()

q = int(input())

for i in range(q):
    
    s = input().split()
    
    ss = s[0] + s[1]
    
    if ss in (faces[1] + faces[2] + faces[4] + faces[3] + faces[1]):
        
        print(faces[0])
        
    elif ss in (faces[0] + faces[3] + faces[5] + faces[2] + faces[0]):
        
        print(faces[1])
        
    elif ss in (faces[0] + faces[1] + faces[5] + faces[4] + faces[0]):
        
        print(faces[2])
        
    elif ss in (faces[0] + faces[4] + faces[5] + faces[1] + faces[0]):
        
        print(faces[3])
        
    elif ss in (faces[0] + faces[2] + faces[5] + faces[3] + faces[0]):
        
        print(faces[4])
        
    elif ss in (faces[1] + faces[3] + faces[4] + faces[2] + faces[1]):
        
        print(faces[5])
        
    else:
        print('error', ss)

Solution (Python):
def roll_N(self):
    self[0],self[1],self[4],self[5] = self[1],self[5],self[0],self[4]
def roll_W(self):
	self[0],self[2],self[3],self[5] = self[2],self[5],self[0],self[3]
dice = [int(e) for e in input().split()]
times = int(input())
for i in range(times):
    upper,front = map(int,input().split())
    rolleddice = list(dice)
    if front == rolleddice[2] or front == rolleddice[3]:
        roll_W(rolleddice)
    while front != rolleddice[1]:
        roll_N(rolleddice)
    while upper != rolleddice[0]:
        roll_W(rolleddice)
    print(rolleddice[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):
nums = list(map(int, input().split()))

import random

class dice:
    def __init__(self):
        self.top = 0
        self.right = 0
        self.front = 0
        self.left = 0
        self.back = 0
        self.bottom = 0
    
    def set_num(self, nums):
        self.top, self.front, self.right, self.left, self.back, self.bottom = nums
        return self
    
    def get_right(self, f_top, f_front):
        while f_top!=self.top or f_front!=self.front:
            self.roll(random.randint(0,3))
        return self.right
        
    def roll(self, command):
        if command==0:
            self.top, self.front, self.right, self.left, self.back, self.bottom = self.left, self.front, self.top, self.bottom, self.back, self.right
        elif command==1:
            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 command==2:
            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 command==3:
            self.top, self.front, self.right, self.left, self.back, self.bottom = self.back, self.top, self.right, self.left, self.bottom, self.front
            

d = dice()
d.set_num(nums)

n = int(input())
for i in range(n):
    f_t, f_r = map(int, input().split())
    print(d.get_right(f_t, f_r))

Solution (Python):
class Dice:
  def __init__(self, state):
    self.state = state

  def vertical(self, direction):
    s = self.state
    state = [s[1], s[5], s[0], s[4]]
    if direction < 0:
      s[0], s[1], s[4], s[5] = state
    elif 0 < direction:
      s[0], s[1], s[4], s[5] = reversed(state)
    return self
  
  def lateral(self, direction):
    s = self.state
    state = [s[2], s[5], s[0], s[3]]
    if direction < 0:
      s[0], s[2], s[3], s[5] = state
    elif 0 < direction:
      s[0], s[2], s[3], s[5] = reversed(state)
    return self

  def north(self):
    self.vertical(-1)
    return self

  def south(self):
    self.vertical(1)
    return self
  
  def east(self):
    self.lateral(1)
    return self

  def west(self):
    self.lateral(-1)
    return self

init_state = input().split()

for i in range(int(input())):
  dice = Dice(init_state)
  up, front = input().split()

  if up == dice.state[0] \
    and front == dice.state[1]:
      print(dice.state[2])
  else:
    for c in list('NNNNWNNNWNNNENNNENNNWNNN'):       
      if c == 'N':
        dice.north()
      elif c == 'S':
        dice.south()
      elif c == 'W':
        dice.west()
      elif c == 'E':
        dice.east()
      if up == dice.state[0] \
        and front == dice.state[1]:
          print(dice.state[2])
          break

Solution (Python):
class dice:
    def __init__(s, i):
        s.d1,s.d2,s.d3,s.d4,s.d5,s.d6 = map(int, i)
        s.dic = {s.d1:{s.d2:s.d3,s.d3:s.d5,s.d4:s.d2,s.d5:s.d4}
                ,s.d2:{s.d1:s.d4,s.d3:s.d1,s.d4:s.d6,s.d6:s.d3}
                ,s.d3:{s.d1:s.d2,s.d2:s.d6,s.d5:s.d1,s.d6:s.d5}
                ,s.d4:{s.d1:s.d5,s.d2:s.d1,s.d5:s.d6,s.d6:s.d2}
                ,s.d5:{s.d1:s.d3,s.d3:s.d6,s.d4:s.d1,s.d6:s.d4}
                ,s.d6:{s.d2:s.d4,s.d3:s.d2,s.d4:s.d5,s.d5:s.d3}}
 
    def get(s, i):
        up, bef = map(int, i)
        return s.dic[up][bef]

dice1 = dice(input().split())
q = int(input())
for i in range(q):
    print(dice1.get(input().split()))

Solution (Python):
import random

class  Dise:
    def __init__(self,top,flont,right,left,back,bottom):
        self.top,self.flont,self.right,self.left,self.back,self.bottom=top,flont,right,left,back,bottom
        
    def rot(self,d):
        if d=='N':
            self.top,self.flont,self.bottom,self.back=self.flont,self.bottom,self.back,self.top
        elif d=='E':
            self.top,self.right,self.bottom,self.left=self.left,self.top,self.right,self.bottom
        elif d=='S':
                self.top,self.flont,self.bottom,self.back=self.back,self.top,self.flont,self.bottom
        elif d=='W':
            self.top,self.right,self.bottom,self.left=self.right,self.bottom,self.left,self.top
    
    def gettop(self):
        return self.top
    def getfront(self):
        return self.flont
    def getright(self):
        return self.right

n=list(map(int,input().split()))
m=int(input())
dise=Dise(n[0],n[1],n[2],n[3],n[4],n[5])

for i in range(m):
    a,b=(int(x) for x in input().split())
    for j in range(1000):
        dise.rot(random.choice('NESW'))
        if a==dise.gettop() and b==dise.getfront():
            print(dise.getright())
            break

Solution (Python):
class Dice:
    NS = [1, 5, 6, 2]
    EW = [1, 3, 6, 4]

    def question(self, top, front):
        self.setTop(top)
        sides = [self.NS[1], self.EW[1], self.NS[3], self.EW[3]]
        return sides[(sides.index(front) + 3) % 4]

    def setTop(self, n):
        if n in self.NS:
            while self.NS[0] != n:
                self.N()
        else:
            while self.NS[0] != n:
                self.E()

    def N(self):
        tail = self.NS.pop()
        self.NS.insert(0, tail)
        self.EW[0] = self.NS[0]
        self.EW[2] = self.NS[2]

    def E(self):
        tail = self.EW.pop()
        self.EW.insert(0, tail)
        self.NS[0] = self.EW[0]
        self.NS[2] = self.EW[2]


if __name__ == '__main__':
    dnum = input().split()
    for _ in range(int(input())):
        t, f = input().split()
        r = Dice().question(dnum.index(t) + 1, dnum.index(f) + 1)
        print(dnum[r - 1])

Solution (Python):

class Dise():
    def __init__(self, aLabelList):
        self.LabelList = aLabelList
        self.LabelsRelationship = [[0 for i in range(5)] for j in range(6)]
        for i in range(6):
            if i == 0:
                self.LabelsRelationship[i][0] = self.LabelList[2 -1] 
                self.LabelsRelationship[i][1] = self.LabelList[3 -1] 
                self.LabelsRelationship[i][2] = self.LabelList[5 -1] 
                self.LabelsRelationship[i][3] = self.LabelList[4 -1] 
                self.LabelsRelationship[i][4] = self.LabelList[6 -1] 
            elif i == 1:
                self.LabelsRelationship[i][0] = self.LabelList[6 -1] 
                self.LabelsRelationship[i][1] = self.LabelList[3 -1] 
                self.LabelsRelationship[i][2] = self.LabelList[1 -1] 
                self.LabelsRelationship[i][3] = self.LabelList[4 -1] 
                self.LabelsRelationship[i][4] = self.LabelList[5 -1] 
            elif i == 2:
                self.LabelsRelationship[i][0] = self.LabelList[6 -1] 
                self.LabelsRelationship[i][1] = self.LabelList[5 -1] 
                self.LabelsRelationship[i][2] = self.LabelList[1 -1] 
                self.LabelsRelationship[i][3] = self.LabelList[2 -1] 
                self.LabelsRelationship[i][4] = self.LabelList[4 -1] 
            elif i == 3:
                self.LabelsRelationship[i][0] = self.LabelList[2 -1] 
                self.LabelsRelationship[i][1] = self.LabelList[1 -1] 
                self.LabelsRelationship[i][2] = self.LabelList[5 -1] 
                self.LabelsRelationship[i][3] = self.LabelList[6 -1] 
                self.LabelsRelationship[i][4] = self.LabelList[3 -1] 
            elif i == 4:
                self.LabelsRelationship[i][0] = self.LabelList[1 -1] 
                self.LabelsRelationship[i][1] = self.LabelList[3 -1] 
                self.LabelsRelationship[i][2] = self.LabelList[6 -1] 
                self.LabelsRelationship[i][3] = self.LabelList[4 -1] 
                self.LabelsRelationship[i][4] = self.LabelList[2 -1] 
            elif i == 5:
                self.LabelsRelationship[i][0] = self.LabelList[5 -1] 
                self.LabelsRelationship[i][1] = self.LabelList[3 -1] 
                self.LabelsRelationship[i][2] = self.LabelList[2 -1] 
                self.LabelsRelationship[i][3] = self.LabelList[4 -1] 
                self.LabelsRelationship[i][4] = self.LabelList[1 -1] 
            
    def DisePrintRight(self,aTopItem,aFront):
        xTopIndex = self.LabelList.index(str(aTopItem))
        for i in range(4):
            if int(self.LabelsRelationship[xTopIndex][i]) == aFront:
                if i + 1 == 4:
                    idxR = int(self.LabelsRelationship[xTopIndex][0])
                else:
                    idxR = int(self.LabelsRelationship[xTopIndex][i+1])
                print(idxR)
                break


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

Solution (Python):

class Dice():
 East = [ [0,3,5,2,4,0]
		 ,[4,0,1,6,0,3]
		 ,[2,6,0,0,1,5]
		 ,[5,1,0,0,6,2]
		 ,[3,0,6,1,0,4]
		 ,[0,4,2,5,3,0]
		 ]
 def __init__(self,N):
  self.label = N
  self.top = 1
  self.north = 5
 def move(self,ori):
  
  if ori=='S':
   t = self.top
   self.top = self.north
   self.north = 7-t
  elif ori=='N':
   t = self.top
   self.top = 7-self.north
   self.north = t
  elif ori=='E':
   east = self.East[self.north-1][self.top-1]
   self.top = 7-east
  elif ori=='W':
   east = self.East[self.north-1][self.top-1]
   self.top = east
 def gettop(self):
  return( self.label[self.top-1] )
  

label = list(map(int, input().split() ))
D = Dice(label)
q = int(input())
for qi in range(q):
 t,s = list(map(int, input().split() ))
 t = D.label.index(t)
 s = D.label.index(s)
 n = 5-s
 e = D.East[n][t]
 print(label[e-1])