#include <iostream>
#include <string>
#include <vector>

#include <ctype.h>

using namespace std;

enum token_type
{
	T_INT,
	T_TAG_S,
	T_TAG_E,
	T_END,
};

struct token
{
	token_type type;
	string tag_name;
	int value;
};

struct panel
{
	string name;
	int bounds[4];
	vector<panel> children;

	bool click(int x, int y)
	{
		bool hit = false;
		for (int i = 0; !hit && i < children.size(); i++)
		{
			hit = children[i].click(x, y);
		}
		if (!hit)
		{
			hit = contains(x, y);
			if (hit) cout << name << " " << children.size() << endl;
		}
		return hit;
	}

	bool contains(int x, int y)
	{
		return bounds[0] <= x && bounds[1] <= y && x <= bounds[2] && y <= bounds[3];
	}
};

string src;
int p;
token cur_tok;

bool end() { return p >= src.length(); }
char peek() { return src[p]; }
void next() { if (!end()) p++; }

token lex_int()
{
	token t;
	t.type = T_INT;
	t.value = 0;
	char c;
	while (isdigit(c = peek()))
	{
		t.value = 10 * t.value + (c - '0');
		next();
	}
	return t;
}

string lex_tag_name()
{
	string name = "";
	char c;
	while ((c = peek()) != '>')
	{
		name += c;
		next();
	}
	return name;
}

token lex_tag()
{
	token t;
	if (peek() == '<') next();
	if (peek() == '/')
	{
		next();
		t.type = T_TAG_E;
		t.tag_name = lex_tag_name();
	}
	else
	{
		t.type = T_TAG_S;
		t.tag_name = lex_tag_name();
	}
	if (peek() == '>') next();
	return t;
}

void lex_next()
{
	if (end()) cur_tok = token();
	char c = peek();
	if (c == '<') cur_tok = lex_tag();
	else if (c == ',') { next(); lex_next(); }
	else if (isdigit(c)) cur_tok = lex_int();
}

panel tag_structure()
{
	panel p;
	if (cur_tok.type == T_TAG_S)
	{
		p.name = cur_tok.tag_name;
		lex_next();

		for (int i = 0; i < 4; i++)
		{
			p.bounds[i] = cur_tok.value;
			lex_next();
		}

		while (cur_tok.type == T_TAG_S)
		{
			p.children.push_back(tag_structure());
		}
		if (cur_tok.type == T_TAG_E) lex_next();
	}
	return p;
}

int main()
{
	int N;
	for(;cin>>N, N;)
	{
		cin >> src;

		p = 0;
		lex_next();
		panel root = tag_structure();

		for (int i = 0; i < N; i++)
		{
			int x, y;
			cin >> x >> y;
			bool hit = root.click(x, y);
			if (!hit) cout << "OUT OF MAIN PANEL 1" << endl;
		}
	}
	return 0;
}