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

    def initialize(top, front, right, left, back, bottom)
        @top, @front, @right, @left, @back, @bottom \
            = top, front, right, left, back, bottom
    end

    def south
        @top, @front, @bottom, @back \
            = @back, @top, @front, @bottom
    end

    def north
        @top, @front, @bottom, @back \
            = @front, @bottom, @back, @top
    end

    def east
        @top, @right, @bottom, @left \
            = @left, @top, @right, @bottom
    end

    def west
        @top, @right, @bottom, @left \
            = @right, @bottom, @left, @top
    end

    def spin
        @front, @right, @back, @left \
            = @right, @back, @left, @front
    end
end

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

data = gets.chomp.to_i

data.times do
    tp, fr = gets.chomp.split.map(&:to_i)

    if dice.right == tp
        dice.west
    elsif dice.left == tp
        dice.east
    else
        until dice.top == tp
            dice.south
        end
    end

    until dice.front == fr
        dice.spin
    end

    puts dice.right
end