class Dice      
    @face = nil         
                        
    def initialize ary   
        @face = ary        
    end                     

    def top
        @face[0]
    end

    def rotate news     
        f = @face.dup
        case news
        when "N"
            @face = [f[1],f[5],f[2],f[3],f[0],f[4]]
        when "E"
            @face = [f[3],f[1],f[0],f[5],f[4],f[2]]
        when "W"
            @face = [f[2],f[1],f[5],f[0],f[4],f[3]]
        when "S"
            @face = [f[4],f[0],f[2],f[3],f[5],f[1]]
        end
    end

    def right top,front
        "SWSWS".each_char do |c|
            break if @face[0] == top
            rotate c
        end
        rotate "N"
        "WWW".each_char do |c|
            break if @face[0] == front
            rotate c
        end
    @face[2]
    end
end

numbers = gets.chomp.split
dice = Dice.new numbers
gets.to_i.times do 
    top,front = gets.chomp.split
    puts dice.right top,front
end
