require 'rexml/document'

class Panel
    def initialize(xml_node)
        @name = xml_node.name
        @x1, @y1, @x2, @y2 = xml_node.text.split(?,).map(&:to_i)
        @children = xml_node.elements.map {|c| Panel.new(c)}
    end

    def check(x, y)
        return nil if x < @x1 || @x2 < x || y < @y1 || @y2 < y

        if !@children.any? {|c| c.check(x, y)}
            puts "#{@name} #{@children.size}"
        end

        return true
    end
end

loop do
    n = gets.to_i
    break if n == 0
    xml = REXML::Document.new(gets.chomp)
    main_panel = Panel.new(xml.elements['/main'])

    n.times do
        x, y = gets.split.map(&:to_i)
        puts "OUT OF MAIN PANEL 1" if !main_panel.check(x, y)
    end
end