
=begin
        0
    3   1   2   4
        5
=end

a = gets.split(" ").map(&:to_i)
n = gets.chomp.to_i

n.times do |i|
    top, front = gets.split(" ").map(&:to_i)

    "EEEESEEEESEEEESEEEESREEERREEE".each_char do |i|
        if i.eql?("N") then
            a[0], a[1], a[5], a[4] = a[1], a[5], a[4], a[0]
        elsif i.eql?("S") then
            a[0], a[1], a[5], a[4] = a[4], a[0], a[1], a[5]
        elsif i.eql?("E") then
            a[0], a[2], a[5], a[3] = a[3], a[0], a[2], a[5]
        elsif i.eql?("W") then
            a[0], a[2], a[5], a[3] = a[2], a[5], a[3], a[0]
        elsif i.eql?("R") then
            a[3], a[1], a[2], a[4] = a[4], a[3], a[1], a[2]
        elsif i.eql?("L") then
            a[3], a[1], a[2], a[4] = a[1], a[2], a[4], a[3]
        end
        
        if a[0] == top && a[1] == front then
            break
        end
    end

    print a[2], "\n"
end

