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(i1, i2, i3, i4, i5, i6)
    @nums = [i1, i2, i3, i4, i5, i6]
  end
  def get_top
    @nums[0]
  end
  def get_right
    @nums[2]
  end
  def move_n
    move(1, 5, 2, 3, 0, 4)
  end
  def move_s
    move(4, 0, 2, 3, 5, 1)
  end
  def move_w
    move(2, 1, 5, 0, 4, 3)
  end
  def move_e
    move(3, 1, 0, 5, 4, 2)
  end
  def normalize(top, front)
    i = @nums.index(top)
    case i
    when 1 then move_n
    when 2 then move_w
    when 3 then move_e
    when 4 then move_s
    when 5 then move_n; move_n
    end
    i = @nums.index(front)
    case i
    when 0 then return false
    when 1 then return true
    when 2 then move_s; move_e; move_n; return true
    when 3 then move_s; move_w; move_n; return true
    when 4 then move_e; move_n; move_w; move_s; move_e; move_n; return true
    when 5 then return false
    else return false
    end
  end
  private
  def move(i1, i2, i3, i4, i5, i6)
    @nums[0], @nums[1], @nums[2], @nums[3], @nums[4], @nums[5] =
      @nums[i1], @nums[i2], @nums[i3], @nums[i4], @nums[i5], @nums[i6]
  end
end

nums = STDIN.gets.chomp.split(' ').map(&:to_i)
dice = Dice.new(*nums)

count = STDIN.gets.chomp.to_i

(1..count).each do |i|
  top, front = STDIN.gets.chomp.split(' ').map(&:to_i)
  dice.normalize(top, front)
  puts dice.get_right
end