Cat and dog queue problem