@n = gets.split(' ').map(&:to_i)
q = gets.chomp.to_i

def throw(a, b)
    temp = @n[0]
    @n[0] = @n[a]
    @n[a] = @n[5]
    @n[5] = @n[b]
    @n[b] = temp
end

q.times{|i|
    face = gets.split(' ').map(&:to_i)
    while (face[1] != @n[0])
        throw(4, 1)
        break if face[1] == @n[0]
        throw(2, 3)
    end
    throw(4, 1)
    while (face[0] != @n[0])
        throw(2, 3)
    end
    puts @n[2]
}