I'd stick to the idea of > 5 (or > 4 in this case, since ^ triggers the event before the current window is added to the $query() count), because if you open several queries manually (/query nick) and have more than 5 windows open, the event will not close further windows anymore.