#!/usr/bin/ruby
#
# 1031.rb: Simple GUI Application
#

### subroutines

# tagst := stag tagvalue {tagst}* etag
# stag  := <tagname>
# etag  := </tagname>
# tagname := string
# tagvalue := num,num,num,num

def parse_tagst(gui)
  tagname = parse_stag(gui)
  vals = parse_tagvalue(gui)

  tagsts = []
  while gui[$pos + 1] != '/'
    tagsts << parse_tagst(gui)
  end

  parse_etag(gui)

  [tagname, vals, tagsts]
end

def parse_stag(gui)
  return nil if gui[$pos] != '<'
  $pos += 1

  pos0 = $pos
  while gui[pos0] != '>'
    pos0 += 1
  end

  tagname = gui[$pos...pos0]
  $pos = pos0 + 1
  tagname
end

def parse_etag(gui)
  return nil if gui[$pos] != '<'
  $pos += 1
  return nil if gui[$pos] != '/'
  $pos += 1

  pos0 = $pos
  while gui[pos0] != '>'
    pos0 += 1
  end

  tagname = gui[$pos...pos0]
  $pos = pos0 + 1
  tagname
end

def parse_tagvalue(gui)
  pos0 = $pos
  while gui[pos0] != '<'
    pos0 += 1
  end

  vals = gui[$pos...pos0].split(',').map(&:to_i)
  $pos = pos0
  vals
end

### main

loop do
  n = gets.to_i
  break if n == 0

  gui = gets.strip

  $pos = 0
  main = parse_tagst(gui)

  n.times do
    x, y = gets.split.map(&:to_i)

    name = 'OUT OF MAIN PANEL'
    cnum = 1
    panels = [main]

    while ! panels.empty?
      panel = panels.shift

      pname, pvals, pchld = panel
      x0, y0, x1, y1 = pvals

      if x >= x0 && x <= x1 && y >= y0 && y <= y1
        name = pname
        cnum = pchld.length
        panels = pchld.clone
      end
    end

    puts [name, cnum].join(' ')
  end
end