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)

dice = STDIN.gets.chomp().split(" ")
# p dice
q = STDIN.gets.to_i
newdice = []
for i in 0..q-1 do
  line = STDIN.gets.split(" ")
  d = [line[0], line[1]]
  newdice << d
end
# p newdice
result = Array.new(q){|i| Array.new(6)}
# p result

def move(direction, dice)
  case direction
  when "S" then
    temp = dice[0]
    dice[0] = dice[4]
    dice[4] = dice[5]
    dice[5] = dice[1]
    dice[1] = temp
  when "N" then
    temp = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = temp
  when "E" then
    temp = dice[0]
    dice[0] = dice[3]
    dice[3] = dice[5]
    dice[5] = dice[2]
    dice[2] = temp
  when "W" then
    temp = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = temp
  end
  return dice
end

def getDrection(ran)
  case ran
  when 0
    return "S"
  when 1
    return "N"
  when 2
    return "E"
  when 3
    return "W"
  end
end

while true
  direction = getDrection(rand(4))
  dice = move(direction, dice)
  # p "dice = #{dice}"
  # p "newdice = #{newdice}"
  count = 0
  newdice.each do |x|
    # p "x[0] = #{x[0]}"
    # p "dice[0] = #{dice[0]}"
    # p "x[1] = #{x[1]}"
    # p "dice[1] = #{dice[1]}"
    if x[0] == dice[0] && x[1] == dice[1] then
      result[count][2] = dice[2]
    end
    count += 1
  end
  flag = true
  # puts "result = #{result}"
  result.each do |x|
    if x[2] == nil then flag = false end
  end
  if flag == true then break end
end

result.each do |x|
  puts x[2]
end