
class Dice

  def initialize(dice)
    @top  = dice[0]
    @side   = [ dice[2], dice[1], dice[3], dice[4] ]
    @bottom = dice[5]
  end

  def roll(action)
    action.chars do |str|
      b = @bottom
      u = @top
      case str
      when "N"
        @top   = @side[2]
        @bottom  = @side[0]
        @side[2] = b
        @side[0] = u
      when "E"
        @top   = @side[3]
        @bottom  = @side[1]
        @side[3] = b
        @side[1] = u
      when "S"
        @top   = @side[0]
        @bottom  = @side[2]
        @side[0] = b
        @side[2] = u
      when "W"
        @top   = @side[1]
        @bottom  = @side[3]
        @side[1] = b
        @side[3] = u
      end
    end
  end

  def zzz(top_south)
    i = 0
    loop do
      if top_south[0] == @top
        loop do
          if top_south[1] == @side[1]
            break
          else
            z = @side.shift
            @side.push(z)
          end
        end
      end
      if top_south[1] == @side[1]
        break
      end
      if i < 3
        b = @bottom
        u = @top
        @top = @side[2]
        @bottom = @side[0]
        @side[2] = b
        @side[0] = u
      elsif i > 2
        b = @bottom
        u = @top
        @top = @side[1]
        @bottom = @side[3]
        @side[1] = b
        @side[3] = u
      end
      i += 1
    end
  end

  def top
    @top
  end

  def bottom
    @bottom
  end

  def north
    @side[0]
  end

  def east
    @side[1]
  end

  def south
    @side[2]
  end

  def west
    @side[3]
  end
end

d = Dice.new(gets.split.map(&:to_i))
gets

ARGF.each_line do |line|
  d.zzz(line.split.map(&:to_i))
  puts d.north
end