using System.Linq;
using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;

public class P
{
    public string name { get; set; }
    public List<int> to { get; set; }
    public int from { get; set; }
    public int x1 { get; set; }
    public int y1 { get; set; }
    public int x2 { get; set; }
    public int y2 { get; set; }
}

public class hello
{
    public static string ans1;
    public static int ans2;
    public static void Main()
    {
        while (true)
        {
            var n = int.Parse(Console.ReadLine().Trim());
            if (n == 0) break;
            var text = Console.ReadLine().Trim();
            var matches = Regex.Matches(text, @"<([^<>]+)>");
            var tag = new List<string>();
            foreach (Match x in matches)
                tag.Add(x.Groups[1].ToString());
            var ps = getList(tag);
            getXY(text, ps);
            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Trim().Split(' ');
                var x = int.Parse(line[0]);
                var y = int.Parse(line[1]);
                check(x, y, ps, 0);
                if (ans2 == -1) Console.WriteLine(ans1);
                else Console.WriteLine("{0} {1}", ans1, ans2);
            }
        }
    }
    static bool check(int x, int y, P[] ps, int c)
    {
        if (x >= ps[c].x1 && x <= ps[c].x2 &&
               y >= ps[c].y1 && y <= ps[c].y2)
        {
            if (ps[c].to.Count() > 0)
            {
                foreach (var xx in ps[c].to)
                {
                    if (check(x, y, ps, xx)) return true;
                }
                ans1 = ps[c].name;
                ans2 = ps[c].to.Count();
                return true;
            }
            else
            {
                ans1 = ps[c].name;
                ans2 = 0;
                return true;
            }
        }
        else
        {
            if (ps[c].from == -1)
            {
                ans1 = "OUT OF MAIN PANEL 1";
                ans2 = -1;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    static void getXY(string text, P[] ps)
    {
        for (int i = 0; i < ps.Length; i++)
        {
            var xL = ps[i].name.Length;
            var p1 = text.IndexOf("<" + ps[i].name + ">") + xL + 2;
            var p2 = text.IndexOf("<", p1);
            var t = text.Substring(p1, p2 - p1);
            string[] line = t.Split(',');
            ps[i].x1 = int.Parse(line[0]);
            ps[i].y1 = int.Parse(line[1]);
            ps[i].x2 = int.Parse(line[2]);
            ps[i].y2 = int.Parse(line[3]);
        }
    }
    static P[] getList(List<string> tag)
    {
        var tL = tag.Count();
        var n = tL / 2;
        var ps = new P[n];
        for (int i = 0; i < n; i++) ps[i] = new P { to = new List<int>() };
        ps[0].name = tag[0];
        ps[0].from = -1;
        var p = 1;
        var now = 0;
        for (int i = 1; i < tL; i++)
        {
            if (tag[i][0] != '/')
            {
                ps[p].name = tag[i];
                ps[p].from = now;
                ps[now].to.Add(p);
                now = p;
                p++;
            }
            else now = ps[now].from;
        }
        return ps;
    }
}



Measured memory_bytes: 19692