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 sys
import os
import math


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

def dice_roll(c):
    copy = dice[:]
    if c == 'E':
        dice[2] = copy[0]
        dice[5] = copy[2]
        dice[3] = copy[5]
        dice[0] = copy[3]
    elif c == 'N':
        dice[0] = copy[1]
        dice[4] = copy[0]
        dice[5] = copy[4]
        dice[1] = copy[5]
        pass
    elif c == 'S':
        dice[1] = copy[0]
        dice[0] = copy[4]
        dice[4] = copy[5]
        dice[5] = copy[1]
    elif c == 'W':
        dice[0] = copy[2]
        dice[2] = copy[5]
        dice[5] = copy[3]
        dice[3] = copy[0]
    
    elif c == 'R':
        dice[2] = copy[1]
        dice[4] = copy[2]
        dice[3] = copy[4]
        dice[1] = copy[3]

for i in range(q):
    up, front = list(map(int, input().split()))

    
    num = 0
    while dice[0] != up:
        if num % 2 == 0:
            dice_roll('E')
        else:
            dice_roll('N')
        num += 1

    
    while dice[1] != front:
        dice_roll('R')

    print(dice[2])

Solution (Python):
class dice_2:
    def __init__(self,label):
        self.spots = label

    def result(self,top,front):
        if self.spots.index(top)+1 == 1:
            self.order=[2,3,5,4,2]
        elif self.spots.index(top)+1 == 2:
            self.order=[6,3,1,4,6]
        elif self.spots.index(top)+1 == 3:
            self.order=[2,6,5,1,2]
        elif self.spots.index(top)+1 == 4:
            self.order=[2,1,5,6,2]
        elif self.spots.index(top)+1 == 5:
            self.order=[1,3,6,4,1]
        elif self.spots.index(top)+1 == 6:
            self.order=[2,4,5,3,2]

        for i in range(4):
            if self.order[i] == self.spots.index(front)+1:
                self.right = self.order[i+1]

    def output(self):
        print(self.spots[self.right-1])

label = list(map(int,input().split()))
dice = dice_2(label)
for i in range(int(input())):
    top,front = map(int,(input().split()))
    dice.result(top,front)
    dice.output()

Solution (Python):
class Dice:
    """????????????????????????"""
    def __init__(self, s):
        self.s1 = s[0]
        self.s2 = s[1]
        self.s3 = s[2]
        self.s4 = s[3]
        self.s5 = s[4]
        self.s6 = s[5]

    def roll(self, direction):          
        temp = self.s1
        if direction == "E":
            self.s1 = self.s4
            self.s4 = self.s6
            self.s6 = self.s3
            self.s3 = temp
        elif direction == "N":
            self.s1 = self.s2
            self.s2 = self.s6
            self.s6 = self.s5
            self.s5 = temp
        elif direction == "S":
            self.s1 = self.s5
            self.s5 = self.s6
            self.s6 = self.s2
            self.s2 = temp
        elif direction == "W":
            self.s1 = self.s3
            self.s3 = self.s6
            self.s6 = self.s4
            self.s4 = temp

    def setposition(self, a, b):
        while 1:
            if self.s1 == a:
                break
            self.roll("N")
            if self.s1 == a:
                break
            self.roll("E")
        while 1:
            if self.s2 == b:
                break
            self.roll("N")
            self.roll("E")
            self.roll("S")


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

for _ in range(q):
    a, b = map(int, input().split())
    dice1.setposition(a, b)
    print(dice1.s3)

Solution (Python):

rep=[
    [1,2,4,3,1],
    [2,0,3,5,2],
    [0,1,5,4,0],
    [0,4,5,1,0],
    [0,2,5,3,0],
    [1,3,4,2,1]
]
d = input().split(" ")
q = int(input())
dic={}
for i in range(6):
    dic.update({d[i]:i})
for i in range(q):
    a,b = input().split(" ")
    for j in range(4):
        top=dic[a]
        front = dic[b]
        if(d[rep[top][j]]==d[front]):
            print(d[rep[top][j+1]])

Solution (Python):
class Dice:
    def __init__(self):
        self.up = 1
        self.under = 6
        self.N = 5
        self.W = 4
        self.E = 3
        self.S = 2

    def roll(self, d):
        if d == "N":
            tmp = self.up
            self.up = self.S
            self.S = self.under
            self.under = self.N
            self.N = tmp
        elif d == "W":
            tmp = self.up
            self.up = self.E
            self.E = self.under
            self.under = self.W
            self.W = tmp
        elif d == "E":
            tmp = self.up
            self.up = self.W
            self.W = self.under
            self.under = self.E
            self.E = tmp
        elif d == "S":
            tmp = self.up
            self.up = self.N
            self.N = self.under
            self.under = self.S
            self.S = tmp

    def getUpward(self):
        return self.up

    def setRoll(self,up,under,N,W,E,S):
        self.up = up
        self.under = under
        self.N = N
        self.W = W
        self.E = E
        self.S = S

    def rotate(self):
        tmp = self.S
        self.S = self.E
        self.E = self.N
        self.N = self.W
        self.W = tmp

    def getE(self, up, S):
        '''
        
        :param up:num 
        :param S: num
        :return: num
        '''
        count = 0
        while True:
            if self.up == up:
                break
            if count != 3:
                self.roll("N")
                count += 1
            else:
                self.roll("E")
        while True:
            if self.S == S:
                break
            self.rotate()
        return self.E



myd = Dice()
myd.up, myd.S, myd.E, myd.W, myd.N, myd.under = map(int,input().split())
n = int(input())
for i in range(n):
    up, S = map(int,input().split())
    print(myd.getE(up,S))

Solution (Python):
def main():
    labels = list(map(int, input().split(' ')))

    n = int(input())

    for i in range(n):
        dice = Dice(labels)
        t, f = map(int, input().split(' '))

        for j in range(8):
            if j%4 == 0:
                dice.toE()

            if f == dice.front:
                break

            dice.toN()

        for j in range(4):
            if t == dice.top:
                break
            dice.toE()

        print(dice.right)

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

        self._tb = (0, 5)
        self._fb = (1, 4)
        self._lr = (3, 2)

    def toN(self):
        tb = self._tb
        self._tb = self._fb
        self._fb = tuple(reversed(tb))

        return self

    def toS(self):
        tb = self._tb
        self._tb = tuple(reversed(self._fb))
        self._fb = tb

        return self

    def toW(self):
        tb = self._tb
        self._tb = tuple(reversed(self._lr))
        self._lr = tb

        return self

    def toE(self):
        tb = self._tb
        self._tb = self._lr
        self._lr = tuple(reversed(tb))

        return self

    def get_top(self):
        return self.labels[self._tb[0]]

    def get_front(self):
        return self.labels[self._fb[0]]

    def get_right(self):
        return self.labels[self._lr[1]]

    top   = property(get_top)
    front = property(get_front)
    right = property(get_right)

if __name__ == '__main__':
    main()

Solution (Python):
def roll(inst):
    global u, s, e, w, n, d
    if inst == 'N':
        u, s, n, d = s, d, u, n
    elif inst == 'E':
        u, e, w, d = w, u, d, e
    elif inst == 'S':
        u, s, n, d = n, u, d, s
    elif inst == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = map(int, input().split())
q = int(input())
for i in range(q):
    u1, s1 = map(int, input().split())
    if u1 == u:
        pass
    elif u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Solution (Python):
def roll(inst):
    global u, s, e, w, n, d
    if inst == 'N':
        u, s, n, d = s, d, u, n
    elif inst == 'E':
        u, e, w, d = w, u, d, e
    elif inst == 'S':
        u, s, n, d = n, u, d, s
    elif inst == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = map(int, input().split())
q = int(input())
for i in range(q):
    u1, s1 = map(int, input().split())
    if u1 == u:
        pass
    elif u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Solution (Python):
class Dice:
    def __init__(self,t,f,r,l,ba,bo):
        self.t = t
        self.f = f
        self.r = r
        self.l = l
        self.ba = ba
        self.bo = bo

s = input().split(" ")
dice = Dice(s[0],s[1],s[2],s[3],s[4],s[5])
n = int(input())
for i in range(n):
    t = input().split(" ")
    if t[0] == dice.t:
        if t[1] == dice.f:
            print(dice.r)
        elif t[1] == dice.r:
            print(dice.ba)
        elif t[1] == dice.ba:
            print(dice.l)
        else:
            print(dice.f)
    elif t[0] == dice.f:
        if t[1] == dice.bo:
            print(dice.r)
        elif t[1] == dice.r:
            print(dice.t)
        elif t[1] == dice.t:
            print(dice.l)
        else:
            print(dice.bo)
    elif t[0] == dice.r:
        if t[1] == dice.bo:
            print(dice.ba)
        elif t[1] == dice.t:
            print(dice.f)
        elif t[1] == dice.f:
            print(dice.bo)
        else:
            print(dice.t)
    elif t[0] == dice.l:
        if t[1] == dice.bo:
            print(dice.f)
        elif t[1] == dice.t:
            print(dice.ba)
        elif t[1] == dice.f:
            print(dice.t)
        else:
            print(dice.bo)
    elif t[0] == dice.ba:
        if t[1] == dice.bo:
            print(dice.l)
        elif t[1] == dice.t:
            print(dice.r)
        elif t[1] == dice.r:
            print(dice.bo)
        else:
            print(dice.t)
    elif t[0] == dice.bo:
        if t[1] == dice.f:
            print(dice.l)
        elif t[1] == dice.l:
            print(dice.ba)
        elif t[1] == dice.r:
            print(dice.f)
        else:
            print(dice.r)

Solution (Python):
class Dice:
    def __init__(self):
        self.face = {12:3, 13:5, 14:2, 15:4, 21:4, 23:1, 24:6, 26:3, 31:2, 32:6, 35:1, 36:5, 41:5, 42:1, 45:6, 46:2, 51:3, 53:6, 54:1, 56:4, 62:4, 63:2, 64:5, 65:3}
    
    def right_face(self, face):
        return self.face[face]

d = Dice()
dice = [dice for dice in input().split()]
q = int(input())
for i in range(q):
    face_up, face_front = input().split()
    face_up = dice.index(face_up)
    face_front = dice.index(face_front)
    face = str(face_up+1) + str(face_front+1)
    face = int(face)
    n = d.right_face(face)
    print(dice[n-1])

Solution (Python):
def roll(i):
    global u, s, e, w, n, d
    if i == 'N':
        u, s, n, d = s, d, u, n
    elif i == 'E':
        u, e, w, d = w, u, d, e
    elif i == 'S':
        u, s, n, d = n, u, d, s
    elif i == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = input().split()
q = int(input())
for i in range(q):
    u1, s1 = input().split()
    if u1 == u:
        pass
    elif u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Solution (Python):
def roll(i):
    global u, s, e, w, n, d
    if i == 'N':
        u, s, n, d = s, d, u, n
    elif i == 'E':
        u, e, w, d = w, u, d, e
    elif i == 'S':
        u, s, n, d = n, u, d, s
    elif i == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = input().split()
q = int(input())
for i in range(q):
    u1, s1 = input().split()
    if u1 == u:
        pass
    elif u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Solution (Python):
def roll(i):
    global u, s, e, w, n, d
    if i == 'N':
        u, s, n, d = s, d, u, n
    elif i == 'E':
        u, e, w, d = w, u, d, e
    elif i == 'S':
        u, s, n, d = n, u, d, s
    elif i == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = input().split()
q = int(input())
for i in range(q):
    u1, s1 = input().split()
    if u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Solution (Python):
class Dice:
    def __init__(self):
        self.front = '1'
        self.back = '6'
        self.top = '5'
        self.bottom = '2'
        self. right = '3'
        self.left = '4'
    def turnRight(self):
        temp = self.top
        self.top = self.left
        self.left = self.bottom
        self.bottom = self.right
        self.right = temp
        return self.top
    def turnLeft(self):
        temp = self.top
        self.top = self.right
        self.right = self.bottom
        self.bottom = self.left
        self.left = temp
        return self.top
    def turnFront(self):
        temp = self.top
        self.top = self.back
        self.back = self.bottom
        self.bottom = self.front
        self.front = temp
        return self.top
    def turnBack(self):
        temp = self.top
        self.top = self.front
        self.front = self.bottom
        self.bottom = self.back
        self.back = temp
        return self.top
 
mydice = Dice()
mydice.top, mydice.front, mydice.right, mydice.left, mydice.back, mydice.bottom = input().split()
 
n = int(input())
for q in range(n):
    top, front = input().split()
 
    for rotate in range(3):
        mydice.turnFront()
        if mydice.top == front:
            break
        mydice.turnRight()
        if mydice.top == front:
            break
    mydice.turnFront()
    while top != mydice.top:
        mydice.turnRight()
    print(mydice.right)

Solution (Python):
x=[int(i)for i in input().split()]
y=[[int(0)for i in range(4)]for i in range(3)]
y[0][1]=x[0]
y[1][1]=x[1]
y[1][2]=x[2]
y[1][0]=x[3]
y[1][3]=x[4]
y[2][1]=x[5]
m=int(input())
for i in range(m):
    r,l= [int(i) for i in input().split()]
    for j in range(3):
        if y[0][1]==r:
            break
        y[0][1],y[1][1]=y[1][1],y[0][1]
        y[1][1],y[2][1]=y[2][1],y[1][1]
        y[2][1],y[1][3]=y[1][3],y[2][1]
        
    if y[0][1]==r:
        for j in range(4):
            if y[1][1]==l:
                break
            y[1][1],y[1][2]=y[1][2],y[1][1]
            y[1][2],y[1][3]=y[1][3],y[1][2]
            y[1][3],y[1][0]=y[1][0],y[1][3]
            if y[1][1]==l:
                break
    else:
        for j in range(4):
            if y[0][1]==r:
                break
            y[0][1],y[1][2]=y[1][2],y[0][1]
            y[1][2],y[2][1]=y[2][1],y[1][2]
            y[2][1],y[1][0]=y[1][0],y[2][1]
            if y[0][1]==r:
                break
        for j in range(4):
            if y[1][1]==l:
                break
            y[1][1],y[1][2]=y[1][2],y[1][1]
            y[1][2],y[1][3]=y[1][3],y[1][2]
            y[1][3],y[1][0]=y[1][0],y[1][3]
            if y[1][1]==l:
                break
    print(y[1][2])

Solution (Python):


"""
???????????? I

?¬?????±??????????????????????????????????????????¢??????????????\??¬????????§????????????????????°????????????????????????????????????
   +--+
 ???|???|
+--+--+--+--+
|???|???|???|???|
+--+--+--+--+
 ???|???|
   +--+

"""

class Dice:
    T = 0
    S = 1
    E = 2
    W = 3
    N = 4
    B = 5

    """ ????????????????????? """
    def __init__(self): 
        self.side = [1, 2, 3, 4, 5, 6]
        self.top = 0

    def setDice(self, arr):  
        self.side = arr[0:6]

    def Turn(self, dir): 
        s = self.side

        if dir == "N":     
            
            t = [s[Dice.S],s[Dice.B],s[Dice.E],s[Dice.W],s[Dice.T],s[Dice.N]]
        elif dir == "S": 
            t = [s[Dice.N],s[Dice.T],s[Dice.E],s[Dice.W],s[Dice.B],s[Dice.S]]
        elif dir == "E": 
            t = [s[Dice.W],s[Dice.S],s[Dice.T],s[Dice.B],s[Dice.N],s[Dice.E]]
        elif dir == "W": 
            t = [s[Dice.E],s[Dice.S],s[Dice.B],s[Dice.T],s[Dice.N],s[Dice.W]]
        elif dir == "L": 
            t = [s[Dice.T],s[Dice.W],s[Dice.S],s[Dice.N],s[Dice.E],s[Dice.B]]
        elif dir == "R": 
            t = [s[Dice.T],s[Dice.E],s[Dice.N],s[Dice.S],s[Dice.W],s[Dice.B]]

        self.side = t


    def Search(self, tval, sval, dir): 
    
        if self.side[Dice.E] == tval:
            self.Turn("W")
        elif self.side[Dice.W] == tval:
            self.Turn("E")
        elif self.side[Dice.N] == tval:
            self.Turn("S")
        elif self.side[Dice.S] == tval:
            self.Turn("N")
        elif self.side[Dice.B] == tval:
            self.Turn("N")
            self.Turn("N")

        for i in range(3): 
            if self.side[Dice.S] == sval:
                break
            self.Turn("L")
            
        return self.side[dir]

dnum = list(map(int,input().strip().split()))
d = Dice()
d.setDice(dnum)
cnum = int(input().strip())
for c in range(cnum):
    cmd = list(map(int,input().strip().split()))
    val = d.Search(cmd[0], cmd[1], Dice.E)
    print(val)

Solution (Python):
def CP(a,b):
	x = a[1]*b[2]-a[2]*b[1]
	y = a[2]*b[0]-a[0]*b[2]
	z = a[0]*b[1]-a[1]*b[0]
	return (x,y,z)

minus = lambda d:tuple(-i for i in d)

class dice:
	def __init__(self,data):
		names = [(1,0,0),(0,0,1),(0,1,0),(0,-1,0),(0,0,-1),(-1,0,0)]
		self.faces = {n:d for n,d in zip(names,data)}
		self.top = (1,0,0)
		self.north = (0,0,-1)
	
	def turn(self,direction):
		if direction == 'N':
			self.top,self.north = minus(self.north),self.top
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'W':
			self.top = CP(self.top,self.north)
		elif direction == 'E':
			[self.turn('W') for i in range(3)]
	
	def get_name(self,value):
		for n,v in self.faces.items():
			if v == value:
				return n


if __name__ == '__main__':
	data = [int(i) for i in input().split()]
	d = dice(data)
	q = int(input())
	for i in range(q):
		a,b = [int(s) for s in input().split()]
		a_name = d.get_name(a)
		b_name = d.get_name(b)
		c_name = CP(b_name,a_name)
		print(d.faces[c_name])

Solution (Python):
x1,x2,x3,x4,x5,x6 = map(int,(input().split()))
n = int(input())
T=[]
for k in range(n):
    i,j = map(int, (input().split()))
    T.append([i,j])
L=[x1,x2,x3,x4,x5,x6]

for k in T:
    while L[0] != k[0]:
        L = [L[4], L[0], L[2], L[3], L[5], L[1]]
        if L[0] == k[0]:
            pass
        else:
            L = [L[3], L[1], L[0], L[5], L[4], L[2]]

    while L[1] != k[1]:
        L = [L[0], L[3], L[1], L[4], L[2], L[5]]
        if L[1] == k[1]:
            pass
        else:
            L = [L[0], L[3], L[1], L[4], L[2], L[5]]
    print(L[2])

Solution (Python):
class dice2():
    def __init__(self,men1,men2,men3,men4,men5,men6):
        self.men=[men1,men2,men3,men4,men5,men6]
        
    def find_num_rside(self,top,front):
        t=self.men.index(top)+1
        f=self.men.index(front)+1
        if [t,f] in [[1,2],[2,6],[6,5],[5,1]]:
            return self.men[3-1]
        elif  [t,f] in [[5,6],[6,2],[2,1],[1,5]]:
            return self.men[4-1]
        elif [t,f] in [[4,2],[2,3],[3,5],[5,4]]:
            return self.men[1-1]
        elif  [t,f] in [[5,3],[3,2],[2,4],[4,5]]:
            return self.men[6-1]
        elif  [t,f] in [[1,3],[3,6],[6,4],[4,1]]:
            return self.men[5-1]
        elif  [t,f] in [[4,6],[6,3],[3,1],[1,4]]:
            return self.men[2-1]
        
mn=list(map(int,input().split()))
q=int(input())

d=dice2(*mn)

for i in range(q):
    top, front=list(map(int,input().split()))
    ans=d.find_num_rside(top, front)
    print(ans)

Solution (Python):
class Dice:
    __slots__ = ['n_list', 'pair_list']

    def __init__(self, n_tup):
        self.n_list = list(n_tup)
        self.pair_list = [self.return_dice_round_pair(self.n_list, 2, 3, 5, 4),
                          self.return_dice_round_pair(self.n_list, 1, 4, 6, 3),
                          self.return_dice_round_pair(self.n_list, 1, 2, 6, 5),
                          self.return_dice_round_pair(self.n_list, 1, 5, 6, 2),
                          self.return_dice_round_pair(self.n_list, 1, 3, 6, 4),
                          self.return_dice_round_pair(self.n_list, 2, 4, 5, 3)
                          ]

    @staticmethod
    def roll_switch(n_list, from_idx_list, to_idx_list):
        n_list[from_idx_list[0] - 1], \
        n_list[from_idx_list[1] - 1], \
        n_list[from_idx_list[2] - 1], \
        n_list[from_idx_list[3] - 1] \
            = n_list[to_idx_list[0] - 1], \
              n_list[to_idx_list[1] - 1], \
              n_list[to_idx_list[2] - 1], \
              n_list[to_idx_list[3] - 1]
        return n_list

    def roll(self, direction):
        if direction == "N":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [2, 6, 5, 1])

        elif direction == "E":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [4, 1, 3, 6])

        if direction == "S":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [5, 1, 2, 6])

        if direction == "W":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [3, 6, 4, 1])

    @staticmethod
    def return_dice_round_pair(n_list, a, b, c, d):
        return [(n_list[a-1], n_list[b-1]),
                (n_list[b-1], n_list[c-1]),
                (n_list[c-1], n_list[d-1]),
                (n_list[d-1], n_list[a-1])]

    def return_right_num(self, top, forward):
        for i in range(6):
            if (top, forward) in self.pair_list[i]:
                return self.n_list[i]


dice = Dice([int(x) for x in input().split()])
cmd_cnt = int(input())

for i in range(cmd_cnt):
    top, forward = (int(x) for x in input().split())
    print(dice.return_right_num(top, forward))

Solution (Python):
def roll(die, d):
    if d == "E":
        return [die[3], die[1], die[0], die[5], die[4], die[2]]
    if d == "N":
        return [die[1], die[5], die[2], die[3], die[0], die[4]]
    if d == "S":
        return [die[4], die[0], die[2], die[3], die[5], die[1]]
    if d == "W":
        return [die[2], die[1], die[5], die[0], die[4], die[3]]
    if d == "L":
        return [die[0], die[3], die[1], die[4], die[2], die[5]]
    if d == "R":
        return [die[0], die[2], die[4], die[1], die[3], die[5]]

die = list(map(int, input().split()))
n = int(input())
for i in range(n):
    a, b = list(map(int, input().split()))
    if die[0] == a:
        pass
    elif die[1] == a:
        die = roll(die, "N")
    elif die[2] == a:
        die = roll(die, "W")
    elif die[3] == a:
        die = roll(die, "E")
    elif die[4] == a:
        die = roll(die, "S")
    elif die[5] == a:
        die = roll(roll(die, "E"), "E")
    while True:
        if die[1] == b:
            break
        die = roll(die, "R")
    print(die[2])

Solution (Python):


def np(dl,nu):
    l=0
    while dl[l]!=nu:
        l+=1
    return l
def si(n1,n2):
    if n1==1:
        if n2==2:
            return 3
        elif n2==3:
            return 5
        elif n2==4:
            return 2
        else:
            return 4
    elif n1==2:
        if n2==1:
            return 4
        elif n2==3:
            return 1
        elif n2==4:
            return 6
        else:
            return 3
    elif n1==3:
        if n2==2:
            return 6
        elif n2==5:
            return 1
        elif n2==1:
            return 2
        else:
            return 5
    elif n1==4:
        if n2==1:
            return 5
        elif n2==2:
            return 1
        elif n2==5:
            return 6
        else:
            return 2
    elif n1==5:
        if n2==1:
            return 3
        elif n2==3:
            return 6
        elif n2==4:
            return 1
        else:
            return 4
    elif n1==6:
        if n2==3:
            return 2
        elif n2==2:
            return 4
        elif n2==5:
            return 3
        else:
            return 5
D=[int(i) for i in input().split()]
q=int(input())
for i in range(q):
    n=[int(i) for i in input().split()]
 
    p=[]
    for j in range(2):
        p.append(np(D,n[j]))
  
    print(D[si(p[0]+1,p[1]+1)-1])

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def do_roll(dice, roll):
    d1, d2, d3, d4, d5, d6 = dice
    if roll == 'E':
        return [d4, d2, d1, d6, d5, d3]
    elif roll == 'W':
        return [d3, d2, d6, d1, d5, d4]
    elif roll == 'N':
        return [d2, d6, d3, d4, d1, d5]
    elif roll == 'S':
        return [d5, d1, d3, d4, d6, d2]
    else:
        assert False, "We should never reach this point!"
    

right_pos = {(1, 2): 3, (1, 4): 2, (1, 5): 4, (1, 3): 5,
             (2, 3): 1, (2, 1): 4, (2, 4): 6, (2, 6): 3,
             (3, 1): 2, (3, 2): 6, (3, 6): 5, (3, 5): 1,
             (4, 1): 5, (4, 2): 1, (4, 6): 2, (4, 5): 6,
             (5, 1): 3, (5, 4): 1, (5, 6): 4, (5, 3): 6,
             (6, 2): 4, (6, 3): 2, (6, 5): 3, (6, 4): 5}
def get_right(dice, top, front):
    p_top = dice.index(top) + 1
    p_front = dice.index(front)  + 1
    return dice[right_pos[(p_top, p_front)] - 1]
    """
    for k in range(4):
        dice = do_roll(dice, 'S')
        if dice[:2] == [top, front]:
            return dice[2]
    for j in range(4):
        dice = do_roll(dice, 'E')
        for k in range(4):
            dice = do_roll(dice, 'S')
            if dice[:2] == [top, front]:
                return dice[2]
    for j in range(4):
        dice = do_roll(dice, 'W')
        for k in range(4):
            dice = do_roll(dice, 'S')
            if dice[:2] == [top, front]:
                return dice[2]
    """
    
dice = [int(_) for _ in input().split()]
Q = int(input())
for q in range(Q):
    top, front = [int(_) for _ in input().split()]
    print(get_right(dice[:], top, front))

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

    def get_top(self):
        return self.top

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

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

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

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

    def get_east(self):
        return self.east
        
dice = Dice(list(map(int,input().split())))
q = int(input())

for i in range(q):
    rot = list(map(int,input().split()))
    if rot[0]==dice.top:
        for _ in range(4):
           dice.rotate_horizon()
           if dice.south==rot[1]:
               h=dice.get_east()
    elif rot[0]==dice.west:
        dice.rotate_east()
        for _ in range(4):
           dice.rotate_horizon()
           if dice.south==rot[1]:
               h=dice.get_east()
    elif rot[0]==dice.bottom:
        dice.rotate_east()
        dice.rotate_east()
        for _ in range(4):
           dice.rotate_horizon()
           if dice.south==rot[1]:
               h=dice.get_east()
    elif rot[0]==dice.east:
        dice.rotate_west()
        for _ in range(4):
           dice.rotate_horizon()
           if dice.south==rot[1]:
               h=dice.get_east()
    elif rot[0]==dice.north:
        dice.rotate_south()
        for _ in range(4):
           dice.rotate_horizon()
           if dice.south==rot[1]:
               h=dice.get_east()
    else:
        dice.rotate_north()
        for _ in range(4):
           dice.rotate_horizon()
           if dice.south==rot[1]:
               h=dice.get_east()
    print(h)

Solution (Python):
def North(dice):
    w = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = w
    return dice
def East(dice):
    w = Dice[1]
    dice[1] = dice[2]
    dice[2] = dice[4]
    dice[4] = dice[3]
    dice[3] = w
    return dice

def West(dice):
    w = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = w
    return dice

maxloop = 4
Ncnt = 0
Ecnt = 0
Wcnt = 0

Dice = input().split()
q = int(input())

CopyDice = Dice

for i1 in range(q):
    Nloop = True
    Eloop = True
    Wloop = True
    Ncnt = 0
    Ecnt = 0
    Wcnt = 0
    I = input().split()
    top = I[0]
    front = I[1]
    while Eloop:
        while Nloop:
            while Wloop:
                if Dice[0] == top and Dice[1] == front:
                    print(Dice[2])
                    Nloop = False
                    Eloop = False
                    Wloop = False
                    break
                Dice = West(Dice)
                Wcnt += 1
                if Wcnt == maxloop:
                    Wloop = False
            Dice = North(Dice)
            Wcnt = 0
            Wloop = True
            Ncnt += 1
            if Ncnt == maxloop:
                Nloop = False
        Dice = East(Dice)
        Ncnt = 0
        Nloop = True
        Ecnt += 1
        if Ecnt == maxloop:
            Eloop = False
    Dice = CopyDice

Solution (Python):
data = list(map(int, input().split()))
n = int(input())
dice = ['12431', '03520', '01540', '04510', '02530', '13421']
for i in range(n):
    up, front = map(int, input().split())
    u = data.index(up)
    f = data.index(front)
    a = dice[u].find(str(f))
    print(data[int(dice[u][a+1])])