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 initialize(faces_int: list) -> list:
    list_sn = [faces_int[5], faces_int[4], faces_int[0], faces_int[1]]  
    list_we = [faces_int[5], faces_int[2], faces_int[0], faces_int[3]]  
    return [list_sn, list_we]


def roll_positive(list_a: list, list_b: list) -> list:
    list_a = list_a[1:] + list_a[0:1]  
    list_b[0] = list_a[0]  
    list_b[2] = list_a[2]  
    return [list_a, list_b]


def roll_negative(list_a: list, list_b: list) -> list:
    list_a = list_a[3:] + list_a[0:3]
    list_b[0] = list_a[0]  
    list_b[2] = list_a[2]  
    return [list_a, list_b]


if __name__ == "__main__":
    faces_int = list(map(lambda x: int(x), input().split()))  
    question_num = int(input())

    for _ in range(question_num):
        SN_direction, WE_direction = initialize(faces_int)
        q_top, q_front = map(lambda x: int(x), input().split())
        for command in "NNNNWNNNWNNNENNNENNNWNNN":  
            if ("N" == command):
                SN_direction, WE_direction = roll_positive(SN_direction, WE_direction)
            elif ("S" == command):
                SN_direction, WE_direction = roll_negative(SN_direction, WE_direction)
            elif ("E" == command):
                WE_direction, SN_direction = roll_positive(WE_direction, SN_direction)
            else:
                WE_direction, SN_direction = roll_negative(WE_direction, SN_direction)
            if SN_direction[2] == q_top and SN_direction[3] == q_front:
                print(f"{WE_direction[1]}")  
                break

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;




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

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

right = []
for i in range(qn):
    x = n
    if question[i][0] == x[1]:  
        temp = x[0]
        x[0] = x[1]
        x[1] = x[5]
        x[5] = x[4]
        x[4] = temp
    elif question[i][0] == x[2]:  
        temp = x[0]
        x[0] = x[2]
        x[2] = x[5]
        x[5] = x[3]
        x[3] = temp
    elif question[i][0] == x[3]:  
        temp = x[0]
        x[0] = x[3]
        x[3] = x[5]
        x[5] = x[2]
        x[2] = temp
    elif question[i][0] == x[4]:  
        temp = x[0]
        x[0] = x[4]
        x[4] = x[5]
        x[5] = x[1]
        x[1] = temp
    elif question[i][0] == x[5]:  
        for k in range(2):
            temp = x[0]
            x[0] = x[4]
            x[4] = x[5]
            x[5] = x[1]
            x[1] = temp

    if question[i][1] != x[1]:
        if question[i][1] == x[2]:  
            temp = x[2]
            x[2] = x[4]
            x[4] = x[3]
            x[3] = x[1]
            x[1] = temp
        elif question[i][1] == x[3]:  
            temp = x[3]
            x[3] = x[4]
            x[4] = x[2]
            x[2] = x[1]
            x[1] = temp
        elif question[i][1] == x[4]:  
            for k in range(2):
                temp = x[3]
                x[3] = x[4]
                x[4] = x[2]
                x[2] = x[1]
                x[1] = temp
    right.append(x[2])

for i in right:
    print(i)

Solution (Python):
import copy
class Dice:
    def __init__(self, a, b, c, d, e, f):
        self.top = a
        self.front = b
        self.right = c
        self.left = d
        self.behind = e
        self.bottom = f

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

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

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

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


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

n=int(input())
for i in range(n):
    top,front=[int(j) for j in input().split(" ")]
    while(dice.front!=front):
        if(dice.left==front or dice.right==front):
            dice.E()
        else:
            dice.N()
    while(dice.top!=top):
        dice.E()
    print(dice.right)

Solution (Python):
def move(dice, order):
  if order == "E":
    temp = dice[0]
    dice[0] = dice[3]
    dice[3] = dice[5]
    dice[5] = dice[2]
    dice[2] = temp
  elif order == "N":
    temp = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = temp
  elif order == "S":
    temp = dice[0]
    dice[0] = dice[4]
    dice[4] = dice[5]
    dice[5] = dice[1]
    dice[1] = temp
  elif order == "W":
    temp = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = temp
  else:
    pass
  return dice
 
def roll(dice):
  temp = dice[1]
  dice[1] = dice[2]
  dice[2] = dice[4]
  dice[4] = dice[3]
  dice[3] = temp
  return dice
 
dice = list(map(int, input().split()))
q = int(input())
for i in range(q):
  up, front = map(int, input().split())
  cnt = 0
  while (dice[0] != up and cnt <= 3):
    dice = move(dice, "E")
    cnt += 1
  while dice[0] != up:
    dice = move(dice, "N")
  while dice[1] != front:
    dice = roll(dice)
  print(dice[2])

Solution (Python):
class dice():
    
    def __init__(self,a,b,c,d,e,f):
        self.A=[a,b,c,d,e,f]
        self.temp=[]
        
    def q_method(self,x,y):
       for i in self.A:
        self.temp.append(i)       

        
        n=self.A.index(x)
        m=self.A.index(y)

       if n==0:
           if m==1:
               print(self.A[2])
           elif m==2:
               print(self.A[4])
           elif m==3:
               print(self.A[1])
           elif m==4:
               print(self.A[3])
       elif n==1:
           if m==0:
              print(self.A[3])
           elif m==2:
              print(self.A[0])
           elif m==3:
              print(self.A[5])
           elif m==5:
               print(self.A[2])
       elif n==2:
           if m==0:
               print(self.A[1])
           elif m==1:
               print(self.A[5])
           elif m==4:
               print(self.A[0])
           elif m==5:
               print(self.A[4])
       elif n==3:
           if m==0:
               print(self.A[4])
           elif m==1:
               print(self.A[0])
           elif m==4:
               print(self.A[5])
           elif m==5:
               print(self.A[1])

       elif n==4:
           if m==0:
               print(self.A[2])
           elif m==2:
               print(self.A[5])
           elif m==3:
               print(self.A[0])
           elif m==5:
               print(self.A[3])
       elif n==5:
           if m==1:
               print(self.A[3])
           elif m==2:
               print(self.A[1])
           elif m==3:
               print(self.A[4])
           elif m==4:
               print(self.A[2])

       
       
if __name__=="__main__":

    input_text=""
    input_text=input()
    array=[]
    array=input_text.split()

    ob1=dice(array[0],array[1],array[2],array[3],array[4],array[5])

    question=input()
    question=int(question)

    for i in range(question):
        input_order=input()

        order_array=[]
        order_array=input_order.split()
        ob1.q_method(order_array[0],order_array[1])

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

Solution (Python):
def dice_move(a1,a2,a3,a4,a5,a6,s):
    for i in s:
        if i=="E":
            a1,a2,a3,a4,a5,a6=a4,a2,a1,a6,a5,a3
        elif i=="S":
            a1,a2,a3,a4,a5,a6=a5,a1,a3,a4,a6,a2
        elif i=="W":
            a1,a2,a3,a4,a5,a6=a3,a2,a6,a1,a5,a4
        elif i=="N":
            a1,a2,a3,a4,a5,a6=a2,a6,a3,a4,a1,a5
    return None

def dice_place(top,front,a1,a2,a3,a4,a5,a6):
    if top==a1:
        ans=[0,0,a3,a5,a2,a4,0]
    elif top==a2:
        ans=[0,a4,0,a1,a6,0,a3]
    elif top==a3:
        ans=[0,a2,a6,0,0,a1,a5]
    elif top==a4:
        ans=[0,a5,a1,0,0,a6,a2]
    elif top==a5:
        ans=[0,a3,0,a6,a1,0,a4]
    elif top==a6:
        ans=[0,0,a4,a2,a5,a3,0]
    front%=10
    return ans[front]
    
a1,a2,a3,a4,a5,a6=map(int,input().split())
n=int(input())
for i in range(n):
    t,f=map(int,input().split())
    print(dice_place(t,f,a1,a2,a3,a4,a5,a6))

Solution (Python):
class Dice():
	def __init__(self):
		self.d = [0,1,2,3,4,5]
		self.rp = []

	def dice_n(self):
		self.d[4],self.d[0],self.d[1],self.d[5] = self.d[0],self.d[1],self.d[5],self.d[4]
		if (([self.d[0],self.d[1]],self.d[2])) not in self.rp:
			self.rp.append(([self.d[0],self.d[1]],self.d[2]))

	def dice_s(self):
		self.d[0],self.d[4],self.d[5],self.d[1] = self.d[4],self.d[5],self.d[1],self.d[0]

	def dice_w(self):
		self.d[0],self.d[2],self.d[5],self.d[3] = self.d[2],self.d[5],self.d[3],self.d[0]

	def dice_e(self):
		self.d[0],self.d[3],self.d[5],self.d[2] = self.d[3],self.d[5],self.d[2],self.d[0]

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

	def check_dice(self,n,m):
		for i in range(len(self.rp)):
			if self.rp[i][0] == [n,m]:
				return self.rp[i][1]

	def print_dice(self):
		print(self.rp)

if __name__ == '__main__':

	dd = list(map(int,input().split()))
	num = int(input())
	line = "NNNNWNNNNWNNNNWNNNNNENNNNENNNNENNNN"

	d = Dice()

	for di in line:
		if di == "N":
			d.dice_n()
		elif di == "S":
			d.dice_s()
		elif di == "W":
			d.dice_w()
		elif di == "E":
			d.dice_e()

	for i in range(num):
		n,m = map(int,input().split())
		nn = dd.index(n)
		mm = dd.index(m)
		ret = d.check_dice(nn,mm)
		print(dd[ret])

Solution (Python):



class Dice:
    def __init__(self, num):
        self.num = num
        
    def show_r(self):
        """ 出目を表示する """
        print(self.num[2])
        
    def roll(self, direction):
        """ 転がす処理 """
        data = []
        new_num = []
        if direction == "N":
            data = [1, 5, 2, 3, 0, 4]
        elif direction == "S":
            data = [4, 0, 2, 3, 5, 1]
        elif direction == "E":
            data = [3, 1, 0, 5, 4, 2]
        elif direction == "W":
            data = [2, 1, 5, 0, 4, 3]
        elif direction == "L":
            data = [0, 2, 4, 1, 3, 5]
        elif direction == "R":
            data = [0, 3, 1, 4, 2, 5]

        for i in data:
            new_num.append(self.num[i])
        self.num = new_num
        
def dice_roll1(dice, dice_tf):
    dice_roll = ""
    top = dice_tf[0]
    
    if dice.num.index(top) == 1:
        dice_roll = "N"
    elif dice.num.index(top) == 2:
        dice_roll = "W"
    elif dice.num.index(top) == 3:
        dice_roll = "E"
    elif dice.num.index(top) == 4:
        dice_roll = "S"
    elif dice.num.index(top) == 5:
        dice_roll = "NN"
        
    return dice_roll

def dice_roll2(dice, dice_tf):
    dice_roll = ""
    front = dice_tf[1]
    
    if dice.num.index(front) == 2:
        dice_roll = "L"
    elif dice.num.index(front) == 3:
        dice_roll = "R"
    elif dice.num.index(front) == 4:
        dice_roll = "LL"
        
    return dice_roll

dice1 = Dice(list(map(int, input().split())))

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

for i in dice_tf:
    direction = dice_roll1(dice1, i)
    for j in direction:
        dice1.roll(j)
    
    direction = dice_roll2(dice1, i)
    for j in direction:
        dice1.roll(j)
        
    dice1.show_r()

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

for i in range(n):
  q=list(map(int,input().split()))
  if q in [[S[3],S[1]],[S[1],S[2]],[S[2],S[4]],[S[4],S[3]]]:
           print(S[0])
           
  if q in [[S[0],S[3]],[S[3],S[5]],[S[5],S[2]],[S[2],S[0]]]:
           print(S[1])
           
  if q in [[S[0],S[1]],[S[1],S[5]],[S[5],S[4]],[S[4],S[0]]]:
           print(S[2])
           
  if q in [[S[4],S[5]],[S[5],S[1]],[S[1],S[0]],[S[0],S[4]]]:
           print(S[3])
           
  if q in [[S[0],S[2]],[S[2],S[5]],[S[5],S[3]],[S[3],S[0]]]:
           print(S[4])
  
  if q in [[S[2],S[1]],[S[1],S[3]],[S[3],S[4]],[S[4],S[2]]]:
           print(S[5])

Solution (Python):
table = [[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]]

l = list(map(int, input().split()))
for i in range(int(input())):
    u, f = map(int, input().split())
    print(l[table[l.index(u)][l.index(f)] - 1])

Solution (Python):
class Dice:
    def __init__(self, num_list):
        self.faces= num_list
        
    def find_rf(self,top,front):
        tf = str(self.faces.index(top)) + str(self.faces.index(front))
        if tf in '12431':
            return self.faces[0]
        elif tf in '03520':
            return self.faces[1]
        elif tf in '01540':
            return self.faces[2]
        elif tf in '04510':
            return self.faces[3]
        elif tf in '02530':
            return self.faces[4]
        else:
            return self.faces[5]


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

for i in range(q):
    top, front = map(int, input().split())
    right_face = dice.find_rf(top,front)
    print(right_face)

Solution (Python):
class Dice():
    def __init__(self,rot,Face):
        self.rot= rot
        self.Face = Face
    def Rotate(rot,Face):
        r=len(rot)
        intFace=[int(Face[i]) for i in range(6)]

        for i in range(r):
            Temp=[intFace[i] for i in range(6)]

            if rot[i]== "E":
                intFace[0]=Temp[3]
                intFace[1]=Temp[1]
                intFace[2]=Temp[0]
                intFace[3]=Temp[5]
                intFace[4]=Temp[4]
                intFace[5]=Temp[2]

            elif rot[i]== "W":
                intFace[0]=Temp[2]
                intFace[1]=Temp[1]
                intFace[2]=Temp[5]
                intFace[3]=Temp[0]
                intFace[4]=Temp[4]
                intFace[5]=Temp[3]

            elif rot[i]== "N":
                intFace[0]=Temp[1]
                intFace[1]=Temp[5]
                intFace[2]=Temp[2]
                intFace[3]=Temp[3]
                intFace[4]=Temp[0]
                intFace[5]=Temp[4]

            elif rot[i]== "S":
                intFace[0]=Temp[4]
                intFace[1]=Temp[0]
                intFace[2]=Temp[2]
                intFace[3]=Temp[3]
                intFace[4]=Temp[5]
                intFace[5]=Temp[1]

        return intFace


Face = input().split()
q=int(input())
intFace=[int(Face[i]) for i in range(6)]
j=0
output=[0 for i in range(q)]
while True:
    if j>=q:
        break
    else:
        rot=""
        faces =input().split()
        
        if faces[0]==Face[0]:
            rot=""

        if faces[0]==Face[1]:
            rot+="N"

        if faces[0]==Face[2]:
            rot+="W"

        if faces[0]==Face[3]:
            rot+="E"

        if faces[0]==Face[4]:
            rot+="S"

        if faces[0]==Face[5]:
            rot+="NN"
    dice=Dice(rot,Face)
    tempFace= Dice.Rotate(rot,Face)
    non_intFace=[f"{tempFace[i]}" for i in range(6)]
    D= ["","NWS","NWSNWS","NWSNWSNWS",] 
    
    for i in range(4):
        temp2=Dice.Rotate(D[i],tempFace)
        if temp2[1]==int(faces[1]):
            finFace=temp2
            break
    output[j]=finFace[2]
    j+=1
for i in range(q):
    print(output[i])

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

for i in range(n):
  q=list(map(int,input().split()))
  if q in [[S[3],S[1]],[S[1],S[2]],[S[2],S[4]],[S[4],S[3]]]:
           print(S[0])
           
  if q in [[S[0],S[3]],[S[3],S[5]],[S[5],S[2]],[S[2],S[0]]]:
           print(S[1])
           
  if q in [[S[0],S[1]],[S[1],S[5]],[S[5],S[4]],[S[4],S[0]]]:
           print(S[2])
           
  if q in [[S[4],S[5]],[S[5],S[1]],[S[1],S[0]],[S[0],S[4]]]:
           print(S[3])
           
  if q in [[S[0],S[2]],[S[2],S[5]],[S[5],S[3]],[S[3],S[0]]]:
           print(S[4])
  
  if q in [[S[2],S[1]],[S[1],S[3]],[S[3],S[4]],[S[4],S[2]]]:
           print(S[5])

Solution (Python):
class Dice:
    def __init__(self):
        self.number = [i for i in range(6)]
        self.command_allpattern = "NNNNENNNNENNNNENNNNNENNNNEENNNNWWWSE"

    def set_number(self, d0, d1, d2, d3, d4, d5):
        self.number[0] = d0
        self.number[1] = d1
        self.number[2] = d2
        self.number[3] = d3
        self.number[4] = d4
        self.number[5] = d5

    def roll(self, command):
        if   command == "E":
            self.set_number(self.number[3],self.number[1],self.number[0],self.number[5],self.number[4],self.number[2])
        elif command == "N":
            self.set_number(self.number[1],self.number[5],self.number[2],self.number[3],self.number[0],self.number[4])
        elif command == "S":
            self.set_number(self.number[4],self.number[0],self.number[2],self.number[3],self.number[5],self.number[1])
        elif command == "W":
            self.set_number(self.number[2],self.number[1],self.number[5],self.number[0],self.number[4],self.number[3])
        else:
            raise

    def get_top(self):
        return self.number[0]

    def get_d2(self, d0, d1):
        flag_correct = False
        
        for command in self.command_allpattern:
            self.roll(command)

            if ((d0 == self.number[0]) and
                (d1 == self.number[1])):
                d2 = self.number[2]
                flag_correct = True

        if flag_correct:
            return d2
        else:
            raise



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

dice = Dice()
dice.set_number(list_face[0], list_face[1], list_face[2], list_face[3], list_face[4], list_face[5])


q = int(input())

for _ in range(q):

    d0, d1 = map(int, input().split())

    print(dice.get_d2(d0, d1))

Solution (Python):

dice = input().split()
top_list =  [
    (-1, 2, 4, 1, 3, -1),
    (3, -1, 0, 5, -1, 2),
    (1, 5, -1, -1, 0, 4),
    (4, 0, -1, -1, 5, 1),
    (2, -1, 5, 0, -1, -3),
    (-1, 3, 1, 4, 2, -1)
]

for q in range(int(input())):
    top, front = [dice.index(s) for s in input().split()]
    print(dice[top_list[top][front]])

Solution (Python):
class Dice:
    N = [0, 1, 5, 4]
    S = N[::-1]
    E = [0, 3, 5, 2]
    W = E[::-1]
    R = [1, 2, 4, 3]
    L = R[::-1]

    def __init__(self, numbers):
        self.numbers = numbers

    def toX(self, x):
        data = getattr(self, x)
        self.numbers.append(0)
        for k in range(5):
            i1 = data[k - 1] if k != 0 else 6
            i2 = data[k] if k != 4 else 6
            self.numbers[i1] = self.numbers[i2]
        del self.numbers[6]

    def setTopandFront(self, top, front):
        while self.numbers[0] != top:
            if self.numbers.index(top) in self.N:
                self.toX('N')
            else:
                self.toX('E')
        while self.numbers[1] != front:
            self.toX('R')

dice = Dice([int(i) for i in input().split()])
for _ in range(int(input())):
    top, front = map(int, input().split())
    dice.setTopandFront(top, front)
    print(dice.numbers[2])

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

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


class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.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):
		self.number = [int(i) for i in range(1,7)]
	
	def set_number(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,s):
		tmp = self.number
		if s == "S":
			self.set_number(tmp[4],tmp[0],tmp[2],tmp[3],tmp[5],tmp[1])
		elif s == "E":
			self.set_number(tmp[3],tmp[1],tmp[0],tmp[5],tmp[4],tmp[2])
		elif s == "N":
			self.set_number(tmp[1],tmp[5],tmp[2],tmp[3],tmp[0],tmp[4])
		elif s == "W":
			self.set_number(tmp[2],tmp[1],tmp[5],tmp[0],tmp[4],tmp[3])

	def get_number(self,i):
		
		return self.number[i]

	def get_position(self,i):
		return self.number.index(i)

if __name__ == '__main__':
	dice_num = [int(s) for s in input().split()]
	num = int(input())
	dice = Dice()
	dice.set_number(dice_num[0],dice_num[1],dice_num[2],dice_num[3],dice_num[4],dice_num[5])

	for i in range(num):
		top,front = map(int,input().split())
		while dice.get_number(1) != front:
			if dice.get_position(front) == 2 or dice.get_position(front) == 3:
				dice.roll("E")
			else:
				dice.roll("S")
		while dice.get_number(0) != top:
			dice.roll("E")
		print("%d" % (dice.get_number(2)))
		dice.set_number(dice_num[0],dice_num[1],dice_num[2],dice_num[3],dice_num[4],dice_num[5])

Solution (Python):
class Dice:
    def __init__(self):
        self.number = [i for i in range(6)]
        self.tmp = [i for i in range(6)]
        self.set_cmd = '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 next(self,direction):
        for i in range(6):
            self.tmp[i] = self.number[i]

        if direction == 'E':
            self.setNumber(self.tmp[3], self.tmp[1], self.tmp[0], self.tmp[5], self.tmp[4], self.tmp[2])
        elif direction == 'N':
            self.setNumber(self.tmp[1], self.tmp[5], self.tmp[2], self.tmp[3], self.tmp[0], self.tmp[4])
        elif direction == 'S':
            self.setNumber(self.tmp[4], self.tmp[0], self.tmp[2], self.tmp[3], self.tmp[5], self.tmp[1])
        elif direction == 'W':
            self.setNumber(self.tmp[2], self.tmp[1], self.tmp[5], self.tmp[0], self.tmp[4], self.tmp[3])
    def get_result(self):
        return self.number[0]



    def search(self,top,forword):

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


        for i in range(len(self.set_cmd)):
            self.next(self.set_cmd[i])

            right = 40

            if self.number[0] == top and self.number[1] == forword:
                right = self.number[2]
                break
        for i in range(len(self.number)):
            self.number[i] = self.search_arr[i]


        return right

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

cmd_fig = int(input())
for i in range(cmd_fig):
    top , forword = map(int,input().split())
    right_side = dice.search(top,forword)
    print("{}".format(right_side))

Solution (Python):



def getKeyFromValue(dice, value):
  keys = [k for k, v in dice.items() if v == value]
  if keys:
    return keys[0]
  return None

def initDice(numbers):  
    
    dice = {'top': numbers[0], 'bottom': numbers[5], 'front': numbers[1],
            'back': numbers[4], 'right': numbers[2], 'left': numbers[3]}
    return dice

def retrunRight(dice, first, second):  
    first = getKeyFromValue(dice, first)
    second = getKeyFromValue(dice, second)
    if first == 'top':
        if second == 'front':
            right = dice['right']
        elif second == 'right':
            right = dice['back']
        elif second == 'back':
            right = dice['left']
        elif second == 'left':
            right = dice['front']
    elif first == 'bottom':
        if second == 'front':
            right = dice['left']
        elif second == 'left':
            right = dice['back']
        elif second == 'back':
            right = dice['right']
        elif second == 'right':
            right = dice['front']
    elif first == 'right':
        if second == 'front':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['back']
        elif second == 'back':
            right = dice['top']
        elif second == 'top':
            right = dice['front']
    elif first == 'left':
        if second == 'front':
            right = dice['top']
        elif second == 'top':
            right = dice['back']
        elif second == 'back':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['front']
    elif first == 'front':
        if second == 'top':
            right = dice['left']
        elif second == 'left':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['right']
        elif second == 'right':
            right = dice['top']
    elif first == 'back':
        if second == 'top':
            right = dice['right']
        elif second == 'right':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['left']
        elif second == 'left':
            right = dice['top']
    return right


numbers = [int(i) for i in input().split()]
dice = initDice(numbers)
q = int(input())

for i in range(q):
    first, second = [int(i) for i in input().split()]
    right = retrunRight(dice, first,second)
    print(right)