#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>

using namespace std;

struct Panel {
	int x1, y1, x2, y2;
	int level;
	string name;
	vector<Panel*> children;
};

void skip(const string &str, int &pos, char c) {
	//assert(str[pos] == c);
	if(str[pos] != c) {
		cerr << pos << ": " << "expected " << c << " but " << (char)str[pos] << endl;
	}
	++pos;
}


string inittag(const string &str, int &pos) {
	skip(str, pos, '<');
	int end = str.find_first_of('>', pos);
	string name = str.substr(pos, end-pos);
	pos = end;
	skip(str, pos, '>');
	return name;
}

void endtag(const string &str, int &pos, const string &name) {
	pos = str.find_first_of('>', pos);
	skip(str, pos, '>');
}


int readInt(const string &str, int &pos) {
	int n = 0;
	while(isdigit(str[pos])) n = n*10+str[pos++]-'0';
	return n;
}

vector<Panel*> allpanel;

Panel* panel(const string &str, int &pos, int level) {
	string name = inittag(str, pos);

	Panel *p = new Panel;
	p->name = name;
	p->level = level;
	p->x1 = readInt(str, pos);
	skip(str, pos, ',');
	p->y1 = readInt(str, pos);
	skip(str, pos, ',');
	p->x2 = readInt(str, pos);
	skip(str, pos, ',');
	p->y2 = readInt(str, pos);
	while(str[pos+1] != '/') {
		Panel *c = panel(str, pos, level+1);
		p->children.push_back(c);
	}
	allpanel.push_back(p);
	endtag(str, pos, name);

	return p;
}

int main() {
	while(true) {
		int n;
		cin >> n;
		if(n == 0) break;

		string tag;
		cin.ignore();
		getline(cin, tag);
		//cout << tag << endl;

		allpanel.clear();
		int pos = 0;
		Panel *parent = panel(tag, pos, 0);

		for(int i = 0; i < n; ++i) {
			int x, y;
			cin >> x >> y;
			Panel *ans=  NULL;
			int level = -1;
			for(int j = 0; j < allpanel.size(); ++j) {
				Panel *p = allpanel[j];
				if(p->x1 <= x && x <= p->x2 && p->y1 <= y && y <= p->y2) {
					if(p->level > level) {
						ans = p;
						level = p->level;
					}
				}
			}
			if(ans) {
				cout << ans->name << " " << ans->children.size() << endl;
			}
			else {
				cout << "OUT OF MAIN PANEL 1" << endl;
			}
		}
	}
	return 0;
}