Try this:

Code:
on ^*:open:?: {
  var %i = 5 
  if ($query(0) >= %i) {
    ignore -pu500 $mask($fulladdress,2)
    [color:red]while ($query(0) > %i) {
      close -m $query($v1)
    }[/color]
    halt
  } 
} 


This will prevent new query windows from opening once there are 5 of more opened already. It will also close the query windows until there are only 5 left.

But... since the only way to get more than 5 query windows is opening new queries yourself, you may want to omit the red part. For example: there are 5 queries open and you open a new query window yourself. While you are typing in this window, someone else sends you a private message, which gets ignored but also causes the window you're typing in to be closed.

I hope this helps in what you're trying to do.