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