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):
arr = input().split()

n = {
    0 : [(3, 1), (4, 3), (2, 4), (1, 2)],
    1 : [(0, 3), (3, 5), (5, 2), (2, 0)],
    2 : [(0, 1), (4, 0), (5, 4), (1, 5)],
    3 : [(0, 4), (4, 5), (5, 1), (1, 0)],
    4 : [(0, 2), (2, 5), (5, 3), (3, 0)],
    5 : [(1, 3), (3, 4), (4, 2), (2, 1)],
}

q = int(input())
for _ in range(q):
    a, b = input().split()
    
    for i in range(6):
        if (arr.index(a), arr.index(b)) in n[i]:
            print(arr[i])

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

def E(ls):
    ls=[ls[3],ls[1],ls[0],ls[5],ls[4],ls[2]]
    return ls

def N(ls):
    ls=[ls[1],ls[5],ls[2],ls[3],ls[0],ls[4]]
    return ls

def R(ls):
    ls=[ls[0],ls[2],ls[4],ls[1],ls[3],ls[5]]
    return ls

for i in range(q):
    num0,num1=list(map(int,input().split()))
    if num.index(num0) in [0,2,3,5]:
        while num[0]!=num0:
            num=E(num)
        while num[1]!=num1:
            num=R(num)
        print(num[2])
    else:
        while num[0]!=num0:
            num=N(num)
        while num[1]!=num1:
            num=R(num)
        print(num[2])

Solution (Python):
def rotate(arr,i):
    if i=='W':
        arr=[arr[k-1] for k in [3,2,6,1,5,4]]
    if i=='E':
        arr=[arr[k-1] for k in [4,2,1,6,5,3]]
    if i=='N':
        arr=[arr[k-1] for k in [2,6,3,4,1,5]]
    if i=='S':
        arr=[arr[k-1] for k in [5,1,3,4,6,2]]
    return arr
arr=input().split()

deck=[]
for i in 'W'*4:
    arr=rotate(arr,i)
    for k in 'NNNN':
        arr=rotate(arr,k)
        deck.append(arr)
arr=rotate(arr,'N')
arr=rotate(arr,'W')
for k in 'NNNN':
    arr=rotate(arr,k)
    deck.append(arr)
arr=rotate(arr,'W')
arr=rotate(arr,'W')
for k in 'NNNN':
    arr=rotate(arr,k)
    deck.append(arr)
n=int(input())
for i in range(n):
    query=input().split()
    for v in deck:
        if v[0]==query[0] and v[1]==query[1]:
            print(v[2])
            break

Solution (Python):
class Dice:
    
    def __init__(self, U, S, E, W, N, D):
        self.u, self.d = U, D
        self.e, self.w = E, W
        self.s, self.n = S, N       
    
    def roll(self, commmand):
        if command == 'E':
            self.e, self.u, self.w, self.d = self.u, self.w, self.d, self.e
        elif command == 'W':
            self.e, self.u, self.w, self.d = self.d, self.e, self.u, self.w
        elif command == 'S':
            self.s, self.u, self.n, self.d = self.u, self.n, self.d, self.s
        else:
            self.s, self.u, self.n, self.d = self.d, self.s, self.u, self.n

    def rightside(self, upside, forward):
        self.structure = {self.u:{self.w: self.s, self.s: self.e, self.e: self.n, self.n: self.w},
                          self.d:{self.n: self.e, self.e: self.s, self.s: self.w, self.w: self.n},
                          self.e:{self.u: self.s, self.s: self.d, self.d: self.n, self.n: self.u},
                          self.s:{self.u: self.w, self.w: self.d, self.d: self.e, self.e: self.u},
                          self.w:{self.u: self.n, self.n: self.d, self.d: self.s, self.s: self.u},
                          self.n:{self.u: self.e, self.e: self.d, self.d: self.w, self.w: self.u}}
        
        print(self.structure[upside][forward])

U, S, E, W, N, D = [int(i) for i in input().split()]
turns = int(input())
dice1 = Dice(U, S, E, W, N, D)
for i in range(turns):
    upside, forward = [int(k) for k in input().split()]
    dice1.rightside(upside, forward)

Solution (Python):
apex = ((1,2,3),
        (1,4,2),
        (1,5,4),
        (1,3,5),

        (2,1,4),
        (2,3,1),
        (2,6,3),
        (2,4,6),

        (3,1,2),
        (3,5,1),
        (3,6,5),
        (3,2,6),

        (4,1,5),
        (4,2,1),
        (4,6,2),
        (4,5,6),

        (5,3,6),
        (5,1,3),
        (5,4,1),
        (5,6,4),

        (6,3,2),
        (6,5,3),
        (6,4,5),
        (6,2,4)
        )

dice = {}
label = []
label[:] = map(int,input().split())


for i in range(0,6):
    dice[label[i]] = i+1



q = int(input())

for _ in range(0,q):
    numbers = []
    numbers[:] = map(int,input().split())
    right = 0
    for t in apex:
        if (t[0] == dice[numbers[0]] and t[1] == dice[numbers[1]]):
            right = t[2]
            
            break
    for key, n in dice.items():
        if (n == right):
            print(key)
            break

Solution (Python):
S = [
    (1, 2, 4, 3), 
    (0, 3, 5, 2), 
    (0, 1, 5, 4), 
    (0, 4, 5, 1), 
    (0, 2, 5, 3), 
    (1, 3, 4, 2), 
]

*L, = map(int, input().split())
q = int(input())
for i in range(q):
    a, b = map(int, input().split())
    for i in range(6):
        if L[i] == a:
            for k in range(4):
                if L[S[i][k]] == b:
                    print(L[S[i][k-3]])

Solution (Python):
import random

class Dice:
    def __init__(self):
        self.u=1
        self.w=2
        self.s=3
        self.e=4
        self.n=5
        self.d=6
        self.dic={"W":0,"S":1,"E":2,"N":3}
    
    def __init__(self,u,w,s,e,n,d):
        self.u=u
        self.w=w
        self.s=s
        self.e=e
        self.n=n
        self.d=d
        self.dic={"W":0,"S":1,"E":2,"N":3}

    def rot(self,way):
        if isinstance(way,str):
            way=self.dic[way]

        if way==0:
            c=self.u
            self.u=self.e
            self.e=self.d
            self.d=self.w
            self.w=c
        elif way==1:
            c=self.u
            self.u=self.n
            self.n=self.d
            self.d=self.s
            self.s=c
        elif way==2:
            c=self.u
            self.u=self.w
            self.w=self.d
            self.d=self.e
            self.e=c
        else :
            c=self.u
            self.u=self.s
            self.s=self.d
            self.d=self.n
            self.n=c


lst=["W","S","E","N"]
u,s,e,w,n,d=map(int,input().split())
N=int(input())
dice=Dice(u,w,s,e,n,d)
for i in range(N):
    top,front=map(int,input().split())
    while True:
        if dice.u==top and dice.w==front:
            break
        else:
            dice.rot(random.choice(lst))
    print(dice.s)

Solution (Python):
class Dice:
    def __init__(self, top, front, right, left, back, bottom):
        self.TOP = top
        self.BOTTOM = bottom
        self.FRONT = front
        self.BACK = back
        self.LEFT = left
        self.RIGHT = right
    
    def move(self, direction):
        if direction == "N":
            self.move_N()
        elif direction == "E":
            self.move_E()
        elif direction == "S":
            self.move_S()
        elif direction == "W":
            self.move_W()
        elif direction == "SIDE":
            self.move_SIDE()
        else:
            raise Exception
    
    def move_N(self):
        tmp = self.TOP
        self.TOP = self.FRONT
        self.FRONT = self.BOTTOM
        self.BOTTOM = self.BACK
        self.BACK = tmp

    def move_S(self):
        tmp = self.TOP
        self.TOP = self.BACK
        self.BACK = self.BOTTOM
        self.BOTTOM = self.FRONT
        self.FRONT = tmp
        
    def move_W(self):
        tmp = self.TOP
        self.TOP = self.RIGHT
        self.RIGHT = self.BOTTOM
        self.BOTTOM = self.LEFT
        self.LEFT = tmp

    def move_E(self):
        tmp = self.TOP
        self.TOP = self.LEFT
        self.LEFT = self.BOTTOM
        self.BOTTOM = self.RIGHT
        self.RIGHT = tmp
    
    def move_SIDE(self):
        tmp = self.FRONT
        self.FRONT = self.RIGHT
        self.RIGHT = self.BACK
        self.BACK = self.LEFT
        self.LEFT = tmp

def get_right(t, f):
    dice = Dice(top, front, right, left, back, bottom)
    has_top = False
    for _ in range(4):
        dice.move("N")
        if dice.TOP == t:
            has_top = True
            break
    if not has_top:
        for _ in range(4):
            dice.move("E")
            if dice.TOP == t:
                has_top = True
                break
    for _ in range(4):
        dice.move("SIDE")
        if dice.FRONT == f:
            return dice.RIGHT
    
top, front, right, left, back, bottom = map(int,input().split())
dice = Dice(top, front, right, left, back, bottom)
n = int(input())
for _ in range(n):
    t, f = map(int, input().split())
    l = None
    if top == t and front == f:
        r = right
    else:
        r = get_right(t, f)
    print(r)

Solution (Python):
d=input().split()
for _ in range(int(input())):
 t, f =[d.index(s)for s in input().split()]
 n = [(1,2,4,3,1),(0,3,5,2,0),(0,1,5,4,0),(1,0,4,5,1),(0,2,5,3,0),(1,3,4,2,1)][t]
 print(d[n[n.index(f)+1]])

Solution (Python):
class Dice:
    def __init__(self,num):
        self.top = num[0]
        self.front = num[1]
        self.right = num[2]
        self.left = num[3]
        self.back = num[4]
        self.bottom = num[5]

    def south_direction(self):
        front = self.top
        bottom = self.front
        back = self.bottom
        top = self.back
        right = self.right
        left = self.left
        
        self.top = top
        self.front = front
        self.bottom = bottom
        self.back = back
        self.right = right
        self.left = left

    def north_direction(self):
        back = self.top
        top = self.front
        front = self.bottom
        bottom = self.back
        right = self.right
        left = self.left
        
        self.top = top
        self.front = front
        self.bottom = bottom
        self.back = back
        self.right = right
        self.left = left

    def east_direction(self):
        right = self.top
        front = self.front
        left = self.bottom
        back = self.back
        bottom = self.right
        top = self.left
        
        self.top = top
        self.front = front
        self.bottom = bottom
        self.back = back
        self.right = right
        self.left = left
        
    def west_direction(self):
        left = self.top
        front = self.front
        right = self.bottom
        back = self.back
        top = self.right
        bottom = self.left
        
        self.top = top
        self.front = front
        self.bottom = bottom
        self.back = back
        self.right = right
        self.left = left
    
    def clockwise_rotate(self):
        top = self.top
        left = self.front
        bottom = self.bottom
        right = self.back
        front = self.right
        back = self.left
        
        self.top = top
        self.front = front
        self.bottom = bottom
        self.back = back
        self.right = right
        self.left = left
        
    
    def print_top(self):
        print(self.top)

    def judge_right(self, top, front):
        while True:
            if self.top == top and self.front == front:
                print(self.right)
                break
            elif top==self.front:
                self.north_direction()
            elif top == self.left:
                self.east_direction()
            elif top == self.back:
                self.south_direction()
            elif top == self.right:
                self.west_direction()
            elif top == self.bottom:
                self.north_direction()
                self.north_direction()
        
            elif front == self.front:
                pass
            elif front == self.left:
                self.clockwise_rotate()
                self.clockwise_rotate()
                self.clockwise_rotate()
            elif front == self.back:
                self.clockwise_rotate()
                self.clockwise_rotate()
            elif front == self.right:
                self.clockwise_rotate()
    
num = list(map(int,input().split()))
rep=int(input())
d = Dice(num)

for i in range(rep):
    top, front = map(int, input().split())
    d.judge_right(top, front)

Solution (Python):
sokumen={1:[2,3,5,4,2],2:[6,3,1,4,6],3:[2,6,5,1,2],4:[2,1,5,6,2],5:[1,3,6,4,1],6:[5,3,2,4,5]}
number=list(map(int,input().split()))
q=int(input())
for _ in range(q):
    u,f=map(int,input().split())
    u=number.index(u)+1
    f=number.index(f)+1
    ans=sokumen[u][sokumen[u].index(f)+1]
    print(number[ans-1])

Solution (Python):
class dice():
    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
    
    def setnum(self,s):
        self.number = s

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

    def Qright(self,up,flont,num):
        uf = (up,flont)
        if uf == (num[1],num[2]) or uf ==(num[2],num[4]) or uf ==(num[4],num[3]) or uf ==(num[3],num[1]):
            ans = num[0]
        elif uf == (num[0],num[3]) or uf ==(num[3],num[5]) or uf ==(num[5],num[2]) or uf ==(num[2],num[0]):
            ans = num[1]
        elif uf == (num[0],num[1]) or uf ==(num[1],num[5]) or uf ==(num[5],num[4]) or uf ==(num[4],num[0]):
            ans = num[2]
        elif uf == (num[0],num[4]) or uf ==(num[4],num[5]) or uf ==(num[5],num[1]) or uf ==(num[1],num[0]):
            ans = num[3]
        elif uf == (num[0],num[2]) or uf ==(num[2],num[5]) or uf ==(num[5],num[3]) or uf ==(num[3],num[0]):
            ans = num[4]
        elif uf == (num[1],num[3]) or uf ==(num[3],num[4]) or uf ==(num[4],num[2]) or uf ==(num[2],num[1]):
            ans = num[5]
        print(ans)


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

q = int(input())
dice = dice()
dice.setnum(num)
for i in range(q):
    up,flont = map(int,input().split())
    dice.Qright(up,flont,num)

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

class Dice():
    def __init__(self, dice, top, front):
        self.dice = dice
        self.top = top
        self.front = front

    def get_right(self):
        right_list =[
            (1, 2, 4, 3), 
            (0, 3, 5, 2), 
            (0, 1, 5, 4), 
            (0, 4, 5, 1), 
            (0, 2, 5, 3), 
            (1, 3, 4, 2)  
        ]

        for i in range(6):
            if self.dice[i] == self.top:
                for j in range(4):
                    if self.dice[right_list[i][j]] == self.front:
                        print(self.dice[right_list[i][j-3]])

for i in range(q):
    top, front = map(int, input().split())
    dice_right = Dice(dice, top, front)
    dice_right.get_right()

Solution (Python):
from copy import deepcopy
from copy import copy

class Dice:
    def __init__(self, labels):
        self.surface = {x: labels[x-1] for x in range(1, 6+1)}
    def move_towards(self, direction):
        if direction == 'N':
            self._move(1, 2, 6, 5)
        elif direction == 'S':
            self._move(1, 5, 6, 2)
        elif direction == 'E':
            self._move(1, 4, 6, 3)
        elif direction == 'W':
            self._move(1, 3, 6, 4)
    def get_upper(self):
        return self.surface[1]
    def _move(self, i, j, k, l):
        temp_label = self.surface[i]
        self.surface[i] = self.surface[j]
        self.surface[j] = self.surface[k]
        self.surface[k] = self.surface[l]
        self.surface[l] = temp_label
    
    def find_pattern(self, pattern):
        dice = deepcopy(self)
        if dice.surface[1] == pattern[0] and dice.surface[2] == pattern[1]:
            return dice.surface[3]
        
        pattern_tested = {'original': dice.surface}
        moves = ['N', 'S', 'E', 'W']
        
        def find(dice, moves):
            next_moves = []
            for move in moves:
                if len(move) == 1:
                    start = 'original'
                else:
                    start = move[:-1]
                    
                dice.surface =pattern_tested[start]
                dice.move_towards(move[-1])
                
                if dice.surface[1] == pattern[0] and dice.surface[2] == pattern[1]:
                    right_label = dice.surface[3]
                    return right_label
                else:
                    move_tested = move
                    pattern_tested[move_tested] = copy(dice.surface)
                    next_moves.extend([move+'N', move+'S', move+'E', move+'W'])
            return find(dice, next_moves)
                
        return find(dice, moves)
    
labels = list(map(int, input().split()))
num_questions = int(input())
questions = [list(map(int, input().split())) for _ in range(num_questions)]
dice = Dice(labels)
results = []

for question in questions:
    right_label = dice.find_pattern(question)
    results.append(right_label)

for result in results:
    print(result)

Solution (Python):


tfr = ((1, 2, 4, 3), (0, 3, 5, 2), (0, 1, 5, 4),
(0, 4, 5, 1), (0, 2, 5, 3), (1, 3, 4, 2))

label = list(map(int, input().split()))
q = int(input())
for i in range(q):
    t, f = map(int, input().split())
    idxt = label.index(t)
    idxf = label.index(f)
    idxr = tfr[idxt].index(idxf) + 1
    if idxr == 4:
        idxr = 0
    print(label[tfr[idxt][idxr]])

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

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

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

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

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

    def r(self):
        self.d[1], self.d[2], self.d[3], self.d[4] = self.d[3], self.d[1], self.d[4], self.d[2]

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

    def forward(self):
        return self.d[1]

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


def main():
    dice = Dice([int(i) for i in input().split()])
    n = int(input())
    for i in range(n):
        t, f = map(int, input().split())
        for i in range(4):
            if dice.top() == t:
                break
            dice.e()
        else:
            for j in range(3):
                if dice.top() == t:
                    break
                dice.s()

        for i in range(3):
            if dice.forward() == f:
                break
            dice.r()

        print(dice.right())


if __name__ == '__main__':
    main()

Solution (Python):
class Dice:
    
    def __init__(self,l):
        self.f=l
        
    def __str__(self):
        l=self.f
        s=f'[U:{l[0]},S:{l[1]},E:{l[2]},W:{l[3]},N:{l[4]},D:{l[5]}]'
        return(s)
       
    def roll(self,c):
        if c=='N':
            ni=[1,5,2,3,0,4]
        elif c=='S':
            ni=[4,0,2,3,5,1]
        elif c=='E':
            ni=[3,1,0,5,4,2]
        elif c=='W':
            ni=[2,1,5,0,4,3]
        self.f=[self.f[j] for j in ni]
    
    def rotate(self,r):
        if r==+1: 
            ni=[0,3,1,4,2,5]
        elif r==-1: 
            ni=[0,2,4,1,3,5]
        self.f=[self.f[j] for j in ni]

    def patterns(self):
        fs=[]
        for i in range(6):
            if i%2==0:
                self.roll('N')
            else:
                self.roll('E')
            for j in range(4):
                self.rotate(+1)
                fs.append(self.f)
        return fs
        
    def lookEast(self,l):
        for f in self.patterns():
            if f[0]==l[0] and f[1]==l[1]:
                return f[2]

d=Dice(list(map(int,input().split())))
q=int(input())
for i in range(q):
    print(d.lookEast(list(map(int,input().split()))))

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


kaisuu = int(input())

lastlist = []
frlist = []
for i in range(kaisuu):
  up,front = list(map(int,input().split()))
  if up == dice[0] :
    frlist = [1,2,4,3,1]
  if up == dice[1]:
    frlist = [0,3,5,2,0]
  if up == dice[2]:
    frlist = [0,1,5,4,0]
  if up == dice[3]:
    frlist = [0,4,5,1,0]
  if up == dice[4]:
    frlist = [0,2,5,3,0]
  if up == dice[5] :
    frlist = [1,3,4,2,1]
 
  k = dice.index(int(front))
  for n in frlist[0:4] :
    if n == k :
      l = frlist.index(int(n))
      m = l + 1
  lastlist.append(dice[frlist[m]])
for g in lastlist :
  print(g)

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

class Dice():
    def __init__(self, t, s, e, w, n, b):
        self.a = [t, s, e, w, n, b]
    
    def shake(self, me):
        if me == 'S':
            self.b = [self.a[int(i)] for i in '402351']
            self.a = self.b
        elif me == 'N':
            self.b = [self.a[int(i)] for i in '152304']
            self.a = self.b
        if me == 'W':
            self.b = [self.a[int(i)] for i in '215043']
            self.a = self.b
        if me == 'E':
            self.b = [self.a[int(i)] for i in '310542']
            self.a = self.b  
    def get(self, dir):
        if dir == 't': return self.a[0]
        elif dir == 'f': return self.a[1]
        elif dir == 'e': return self.a[2]
        elif dir == 'w': return self.a[3]
        else: print('input err', dir)

d = Dice(*num)
for i in range(q):
    u, f = map(int, input().split())
    for j in range(50):
        if d.get('t') == u and d.get('f') == f:
            print(d.get('e'))
            break
        elif d.get('w') == f or d.get('e') == f or d.get('f') == f:
            d.shake('W')
        else:
            d.shake('S')

Solution (Python):

class Dice:
    def __init__(self,list):
        self.list = []
        for i in range(6):
            self.list.append(list[i])

    def rotate(self,dir):
        if dir == "N":
            self.list[0], self.list[1], self.list[5], self.list[4] = self.list[1], self.list[5], self.list[4], self.list[0]
        elif dir == "S":
            self.list[0], self.list[4], self.list[5], self.list[1] = self.list[4], self.list[5], self.list[1], self.list[0]
        elif dir == "W":
            self.list[0], self.list[3], self.list[5], self.list[2] = self.list[2], self.list[0], self.list[3], self.list[5]
        elif dir == "E":
            self.list[0], self.list[3], self.list[5], self.list[2] = self.list[3], self.list[5], self.list[2], self.list[0]
        elif dir == "H":
            self.list[1], self.list[2], self.list[4], self.list[3] = self.list[2], self.list[4], self.list[3], self.list[1]
        elif dir =="V":
            self.list[0], self.list[3], self.list[5], self.list[2] = self.list[3], self.list[5], self.list[2], self.list[0]
        else:
            pass

    def show(self):
        print("D:{}".format(repr(self.list)))

    def isface(self,f):
        if f in self.list:
            return True
        else:
            return False

    def matchone(self, f):
        if self.isface(f) == False:
            return False
        if f == self.list[0]:
            pass
        elif f == self.list[1]:
            self.rotate("N")
        elif f == self.list[2]:
            self.rotate("W")
        elif f == self.list[3]:
            self.rotate("E")
        elif f == self.list[4]:
            self.rotate("S")
        else:
            self.rotate("N")
            self.rotate("N")
        return True

    def matchtwo(self, f):
        if self.isface(f) == False:
            return False
        if self.list[1] ==f:
            return True
        for i in range(3):
            self.rotate("H")
            if self.list[1] ==f:
                return True
        return False


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

for i in range(num):
    one, two = [int(x) for x in input().split()]
    dice.matchone(one)
    
    dice.matchtwo(two)
    
    print(dice.list[2])

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

    def rotate(self,dir):
        if dir=="N":
            tmp=self.val[0]
            self.val[0]=self.val[1]
            self.val[1]=self.val[5]
            self.val[5]=self.val[4]
            self.val[4]=tmp
        if dir=="E":
            tmp=self.val[0]
            self.val[0]=self.val[3]
            self.val[3]=self.val[5]
            self.val[5]=self.val[2]
            self.val[2]=tmp
        if dir=="S":
            tmp=self.val[0]
            self.val[0]=self.val[4]
            self.val[4]=self.val[5]
            self.val[5]=self.val[1]
            self.val[1]=tmp
        if dir=="W":
            tmp=self.val[0]
            self.val[0]=self.val[2]
            self.val[2]=self.val[5]
            self.val[5]=self.val[3]
            self.val[3]=tmp

def int_to_dir(n):
    if n==0:
        return "N"
    if n==1:
        return "E"
    if n==2:
        return "S"
    if n==3:
        return "W"
    

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

D = Dice(x)

import random 

for i in range(q):
    a,b=map(int,input().split())
    count=0
    while True:
        D.rotate(int_to_dir(random.randint(0,4)))
        
        if D.val[0]==a and D.val[1]==b:
            
            print(D.val[2])
            break

Solution (Python):

def dice_r(u,f):
	if u == 1:
		if f == 2:
			r = 3
		if f == 3:
			r = 5
		if f == 5:
			r = 4
		if f == 4:
			r = 2
	if u == 2:
		if f == 1:
			r = 4
		if f == 4:
			r = 6
		if f == 6:
			r = 3
		if f == 3:
			r = 1
	if u == 3:
		if f == 1:
			r = 2
		if f == 2:
			r = 6
		if f == 6:
			r = 5
		if f == 5:
			r = 1
	if u == 4:
		if f == 1:
			r = 5
		if f == 5:
			r = 6
		if f == 6:
			r = 2
		if f == 2:
			r = 1
	if u == 5:
		if f == 1:
			r = 3
		if f == 3:
			r = 6
		if f == 6:
			r = 4
		if f == 4:
			r = 1
	if u == 6:
		if f == 2:
			r = 4
		if f == 4:
			r = 5
		if f == 5:
			r = 3
		if f == 3:
			r = 2
	return r
		
n = list(map(int,input().split()))
q = int(input())
for i in range(q):
	a,b = map(int,input().split())
	u = n.index(a) +1
	f = n.index(b) +1
	c = dice_r(u,f)
	ans = n[c-1]
	print(ans)

Solution (Python):
def test():

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

	for i in range(n):

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

		top = given_face[0]
		front = given_face[1]

		if (top == faces[1] and front == faces[2]) or (top == faces[2] and front == faces[4]) or (top == faces[4] and front == faces[3]) or (top == faces[3] and front == faces[1]):
			print(f"{faces[0]}")
		elif (top == faces[0] and front == faces[3]) or (top == faces[3] and front == faces[5]) or (top == faces[5] and front == faces[2]) or (top == faces[2] and front == faces[0]):
			print(f"{faces[1]}")
		elif (top == faces[0] and front == faces[1]) or (top == faces[1] and front == faces[5]) or (top == faces[5] and front == faces[4]) or (top == faces[4] and front == faces[0]):
			print(f"{faces[2]}")
		elif (top == faces[0] and front == faces[4]) or (top == faces[4] and front == faces[5]) or (top == faces[5] and front == faces[1]) or (top == faces[1] and front == faces[0]):
			print(f"{faces[3]}")
		elif (top == faces[0] and front == faces[2]) or (top == faces[2] and front == faces[5]) or (top == faces[5] and front == faces[3]) or (top == faces[3] and front == faces[0]):
			print(f"{faces[4]}")
		elif (top == faces[1] and front == faces[3]) or (top == faces[3] and front == faces[4]) or (top == faces[4] and front == faces[2]) or (top == faces[2] and front == faces[1]):
			print(f"{faces[5]}")

if __name__ == "__main__":
	test()

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)
n = int(input())
for i in range(n):
    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):

import sys
import copy
line = sys.stdin.readline() 
line = line.rstrip('\r\n')
face = line.split()

line = sys.stdin.readline() 

while(True):
    line = sys.stdin.readline() 
    if not line:
        break
    line = line.rstrip('\r\n')
    tar = line.split()
    
    for i in range(5):
        if face[1] == tar[1]:
            break
        face_old = copy.deepcopy(face)
        face[0] = face_old[1]
        face[1] = face_old[5]
        face[2] = face_old[2]
        face[3] = face_old[3]
        face[4] = face_old[0]
        face[5] = face_old[4]
    else:
        face_old = copy.deepcopy(face)
        face[0] = face_old[3]
        face[1] = face_old[1]
        face[2] = face_old[0]
        face[3] = face_old[5]
        face[4] = face_old[4]
        face[5] = face_old[2]
    for i in range(5):
        if face[1] == tar[1]:
            break
        face_old = copy.deepcopy(face)
        face[0] = face_old[1]
        face[1] = face_old[5]
        face[2] = face_old[2]
        face[3] = face_old[3]
        face[4] = face_old[0]
        face[5] = face_old[4]
    for i in range(5):
        if face[0] == tar[0]:
            break
        face_old = copy.deepcopy(face)
        face[0] = face_old[3]
        face[1] = face_old[1]
        face[2] = face_old[0]
        face[3] = face_old[5]
        face[4] = face_old[4]
        face[5] = face_old[2]
    print(face[2])

Solution (Python):


def west(d):
	tmp=d[4]
	d[4]=d[1]
	d[1]=d[3]
	d[3]=d[6]
	d[6]=tmp

def east(d):
	tmp=d[4]
	d[4]=d[6]
	d[6]=d[3]
	d[3]=d[1]
	d[1]=tmp

def north(d):
	tmp=d[1]
	d[1]=d[2]
	d[2]=d[6]
	d[6]=d[5]
	d[5]=tmp

def south(d):
	tmp=d[1]
	d[1]=d[5]
	d[5]=d[6]
	d[6]=d[2]
	d[2]=tmp

def spin(d):
	tmp=d[2]
	d[2]=d[3]
	d[3]=d[5]
	d[5]=d[4]
	d[4]=tmp

def kaiten(d, op):
	if   op == "W": west(d)
	elif op == "E": east(d)
	elif op == "N": north(d)
	elif op == "S": south(d)

def inputDice():
	return list(map(int,("-1 "+input()).split()))

def solveB(d,d1,d2):
	for op in list("NWWWN-"):
		for i in range(4):
			if(d[1]==d1 and d[2]==d2):
				return d[3]
			spin(d)
		kaiten(d,op)
	return None

d=inputDice()
solveB(d,1,2)

for i in range(int(input())):
	d1,d2=map(int,input().split())
	print(solveB(d,d1,d2))