class Dice
    # constructor
    def initialize(line)
        @figures = line.chomp.split(" ").map(&:to_i)
    end
    
    # Rotate in any direction
    def rotate(direction)
        array = Array.new(6)
        
        case direction
        when "N"
            array[0] = @figures[1]
            array[1] = @figures[5]
            array[2] = @figures[2]
            array[3] = @figures[3]
            array[4] = @figures[0]
            array[5] = @figures[4]
        when "S"
            array[0] = @figures[4]
            array[1] = @figures[0]
            array[2] = @figures[2]
            array[3] = @figures[3]
            array[4] = @figures[5]
            array[5] = @figures[1]
        when "W"
            array[0] = @figures[2]
            array[1] = @figures[1]
            array[2] = @figures[5]
            array[3] = @figures[0]
            array[4] = @figures[4]
            array[5] = @figures[3]
        when "E"
            array[0] = @figures[3]
            array[1] = @figures[1]
            array[2] = @figures[0]
            array[3] = @figures[5]
            array[4] = @figures[4]
            array[5] = @figures[2]
        end
        @figures = Marshal.load(Marshal.dump(array))
    end
    
    # Rotate horizontally
    def turnClockwise()
        array = Array.new(6)
        array[0] = @figures[0]
        array[1] = @figures[2]
        array[2] = @figures[4]
        array[3] = @figures[1]
        array[4] = @figures[3]
        array[5] = @figures[5]
        @figures = Marshal.load(Marshal.dump(array))
    end
    
    def showTop()
        return @figures[0]
    end
    
    def showFront()
        return @figures[1]
    end
    
    def showRight()
        return @figures[2]
    end
end

dice = Dice.new(gets.chomp)
n = gets.to_i

for i in 1..n
    input = gets.chomp.split(" ").map(&:to_i)
    
    # rotate North-South direction
    for j in 1..3
        if dice.showTop() == input[0]
            break
        else
            dice.rotate("N")
        end
    end
    # in case it exists on the side
    while dice.showTop() != input[0]
        dice.rotate("W")
    end
    
    # match the front
    while dice.showFront() != input[1]
        dice.turnClockwise()
    end
    puts dice.showRight()
end
