class Dice
  attr_accessor :face

  def rotate(dir)
    case dir
    when "N"
      @face[0],@face[1],@face[5],@face[4] = @face[1],@face[5],@face[4],@face[0]
    when "E"
      @face[0],@face[2],@face[5],@face[3] = @face[3],@face[0],@face[2],@face[5]
    when "S"
      @face[0],@face[1],@face[5],@face[4] = @face[4],@face[0],@face[1],@face[5]
    when "W"
      @face[0],@face[2],@face[5],@face[3] = @face[2],@face[5],@face[3],@face[0]
    when "cw" #ClockWise rotation
      self.rotate("E").rotate("N").rotate("W")
      #@face[1],@face[2],@face[4],@face[3] = @face[2],@face[4],@face[3],@face[1]
    when "acw" #Anti ClockWise rotation
      self.rotate("W").rotate("N").rotate("E")
      #@face[1],@face[2],@face[4],@face[3] = @face[3],@face[1],@face[2],@face[4]
    end
    return self
  end

  def query(top, front)
    @face.each_with_index do |item, index|
      if item == top
        case index
        when 1
          self.rotate("N")
        when 2
          self.rotate("W")
        when 3
          self.rotate("E")
        when 4
          self.rotate("S")
        when 5
          self.rotate("N").rotate("N")
        end
        (1..4).each do |index|
          if @face[index] == front
            case index
            when 2
              self.rotate("cw")
            when 3
              self.rotate("acw")
            when 4
              self.rotate("cw").rotate("cw")
            end
            right = @face[2]
            return right
          end
        end
      end
    end
  end
end

dice = Dice.new
dice.face = gets.chomp.split.map(&:to_i)

gets.chomp.to_i.times do
  top,front = gets.chomp.split.map(&:to_i)
  puts dice.query(top, front)
end