# ITP1_11_B: Dice2

class Dice
  attr_reader :top, :front, :right, :left, :back, :bottom

  def initialize(labels)
    @top, @front, @right, @left, @back, @bottom = labels
  end
 
  def roll_to(direction)
    case direction
    when 'N'
      @top, @front, @bottom, @back = front, bottom, back, top
    when 'E'
      @top, @left, @bottom, @right = left, bottom, right, top
    when 'S'
      @top, @back, @bottom, @front = back, bottom, front, top
    when 'W'
      @top, @right, @bottom, @left = right, bottom, left, top
    end
  end
end
 
labels     = gets.split.map &:to_i
n_changes  = gets.to_i
directions = %w(N E S W)
dice       = Dice.new labels

n_changes.times do
  top, front = gets.split.map &:to_i
  while true
    dice.roll_to directions.sample
    if dice.top == top && dice.front == front
      puts dice.right
      break
    end
  end
end