class Dice
    @up = ''
    def initialize(n)
        @up = n[0]
        @front = n[1]
        @right = n[2]
        @left = n[3]
        @back = n[4]
        @down = n[5]
    end

    def move(direction)
        case direction
        when 'S'
            tmp = @up
            @up = @back
            @back = @down
            @down = @front
            @front = tmp
        when 'W'
            tmp = @up
            @up = @right
            @right = @down
            @down = @left
            @left = tmp
        when 'E'
            tmp = @up
            @up = @left
            @left = @down
            @down = @right
            @right = tmp
        when 'N'
            tmp = @up
            @up = @front
            @front = @down
            @down = @back
            @back = tmp
        end
    end
    def displayUp
        puts @up
    end
    def problem2(up,front)
        if @right == front then
            move('W')
            move('S')
        elsif @left == front then
            move('E')
            move('S')
        else
            while @front != front do
                move('S')
            end
        end
        while @up != up do
            move('W')
        end
        puts @right
    end
end

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

q = gets.to_i
q.times do |i|
    num_up,num_front = gets.split.map(&:to_i)
    dice.problem2(num_up,num_front)
end
