Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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 in Ruby (judge verdict: Accepted)

class Dice
	def initialize(array)
		@data = array
	end
	
	def move(dir)
		case dir.downcase
		when "e"
			tmp=@data[0]
			@data[0]=@data[3]
			@data[3]=@data[5]
			@data[5]=@data[2]
			@data[2]=tmp
		when "n"
			tmp=@data[0]
			@data[0]=@data[1]
			@data[1]=@data[5]
			@data[5]=@data[4]
			@data[4]=tmp
		when "s"
			tmp=@data[0]
			@data[0]=@data[4]
			@data[4]=@data[5]
			@data[5]=@data[1]
			@data[1]=tmp
		when "w"
			tmp=@data[0]
			@data[0]=@data[2]
			@data[2]=@data[5]
			@data[5]=@data[3]
			@data[3]=tmp
		end
	end
	
	def rotate
		tmp=@data[1]
		@data[1]=@data[2]
		@data[2]=@data[4]
		@data[4]=@data[3]
		@data[3]=tmp
	end
	
	def top
		return @data[0]
	end
	
	def front
		return @data[1]
	end
	
	def left
		return @data[3]
	end
	
	def right
		return @data[2]
	end
	
	def back
		return @data[4]
	end
	
	def bottom
		return @data[5]
	end
end

array = gets.chomp.split(" ").map(&:to_i)
dice = Dice.new(array)
q = gets.chomp.to_i

q.times{
    data = gets.chomp.split(" ").map(&:to_i)
	if dice.top == data[0]
	elsif dice.front == data[0]
		dice.move("N")
	elsif dice.left == data[0]
		dice.move("E")
	elsif dice.right == data[0]
		dice.move("W")
	elsif dice.back == data[0]
		dice.move("S")
	elsif dice.bottom == data[0]
		dice.move("N")
		dice.move("N")
	end
	
	while dice.front != data[1]
		dice.rotate
	end
	
	puts dice.right
}