# ITP1_11_B: Dice2

class Dice
  module Pip
    TOP    = 0
    FRONT  = 1
    RIGHT  = 2
    LEFT   = 3
    BACK   = 4
    BOTTOM = 5
  end

  def initialize(labels)
    @labels = labels
  end
 
  def top
    @labels[Pip::TOP]
  end

  def front
    @labels[Pip::FRONT]
  end

  def right
    @labels[Pip::RIGHT]
  end
 
  def rotate(p1, p2, p3, p4)
    src = @labels[p4], @labels[p1], @labels[p2], @labels[p3]
    @labels[p1], @labels[p2], @labels[p3], @labels[p4] = src
  end
 
  def roll_to(direction)
    rotate *case direction
    when 'N'
      [Pip::TOP, Pip::FRONT, Pip::BOTTOM, Pip::BACK]
    when 'E'
      [Pip::TOP, Pip::LEFT, Pip::BOTTOM, Pip::RIGHT]
    when 'S'
      [Pip::TOP, Pip::BACK, Pip::BOTTOM, Pip::FRONT]
    when 'W'
      [Pip::TOP, Pip::RIGHT, Pip::BOTTOM, Pip::LEFT]
    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