import java.util.*

fun main(args:Array<String>):Unit {
    while (true) {
        val n = readLine()!!.trim().toInt()
        if (n == 0) return
        val (tag, _) = parseTagStructure(readLine()!!.trim())
        repeat(n){
            val (x, y) = readLine()!!.trim().split(' ').map(String::toInt)
            val t = tag.touchedTag(Point(x, y))
            println(
                    when(t){
                        null -> "OUT OF MAIN PANEL 1"
                        else -> "${t.name} ${t.childSize()}"
                    }
            )
        }
    }
}
class Tag(val name:String, val leftTop:Point, val rightBottom:Point, private val mChildren: Iterable<Tag>){
    private fun isTouched(touchPoint:Point):Boolean {
        return touchPoint.x in leftTop.x .. rightBottom.x && touchPoint.y in leftTop.y .. rightBottom.y
    }
    fun childSize():Int = mChildren.count()
    fun touchedTag(touchPoint:Point):Tag? {
        return if (isTouched(touchPoint)){
            val child = mChildren.mapNotNull{it.touchedTag(touchPoint)}.firstOrNull()
            when(child){
                null -> this
                else -> child
            }
        }else null
    }
}
fun parsePoints(str:String):Pair<Pair<Point, Point>, String> {
    val pattern = """^(\d+),(\d+),(\d+),(\d+)(.*)""".toRegex()
    val matched = pattern.matchEntire(str)!!
    val (a, b, c, d) = matched.groupValues.drop(1).take(4).map(String::toInt)
    val rest = matched.groupValues.last()
    return Pair(Pair(Point(a, b), Point(c, d)), rest)
}
fun parseStartTag(str:String):Pair<String, String> {
    val pattern = """^<(\w+)>(.*)""".toRegex()
    val matched = pattern.matchEntire(str)!!
    return Pair(matched.groupValues[1], matched.groupValues[2])
}
fun parseEndTag(str:String):Pair<String, String>{
    val pattern = """^</(\w+)>(.*)""".toRegex()
    val matched = pattern.matchEntire(str)!!
    return Pair(matched.groupValues[1], matched.groupValues[2])
}
fun parseTagStructure(str:String):Pair<Tag, String>{
    val (name, rest) = parseStartTag(str)
    val (p, rest2) = parsePoints(rest)
    val (tags, rest3) = parseTagSequence(rest2)
    val (endName, r) = parseEndTag(rest3)
    assert(name == endName)
    return Pair(Tag(name, p.first, p.second, tags), r)
}
fun parseTagSequence(str:String):Pair<Collection<Tag>, String> {
    val stack = Stack<Tag>()
    val startTag = """^<(\w+)>(.*)""".toRegex()
    var s = str
    while(true){
        if (startTag.matches(s)){
            val (t, r) = parseTagStructure(s)
            stack.push(t)
            s = r
        }else return Pair(stack, s)
    }
}
data class Point(val x:Int, val y:Int)
