#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

struct Panel{
	string name;
	int x1,y1,x2,y2;
	Panel(string n,int x1,int y1,int x2,int y2):name(n),x1(x1),y1(y1),x2(x2),y2(y2){}
};
ostream& operator<<(ostream& os,const Panel& p){
	return os<<'('<<p.name<<','<<p.x1<<','<<p.y1<<','<<p.x2<<','<<p.y2<<')';
}

int parse(string& s,int i,int prev,vector<Panel>& panels,vi& parent)
{
	i++;
	int j=s.find('>',i);
	string name=s.substr(i,j-i);
	j++;
	int x1,y1,x2,y2; sscanf(s.c_str()+j,"%d,%d,%d,%d",&x1,&y1,&x2,&y2);
	int cur=panels.size();
	panels.emplace_back(name,x1,y1,x2,y2);
	parent.push_back(prev);
	for(j=s.find('<',j);;){
		if(s[j+1]=='/')
			break;
		j=parse(s,j,cur,panels,parent);
	}
	j=s.find('>',j);
	return j+1;
}

int main()
{
	for(int n;cin>>n,n;){
		string s; cin>>s;
		
		vector<Panel> panels;
		vi parent;
		parse(s,0,-1,panels,parent);
		
		vi child(panels.size());
		peri(i,1,parent.size())
			child[parent[i]]++;
		
		rep(_,n){
			int x,y; cin>>x>>y;
			int i=-1;
			per(j,panels.size()){
				Panel p=panels[j];
				if(p.x1<=x && x<=p.x2 && p.y1<=y && y<=p.y2){
					i=j;
					break;
				}
			}
			if(i==-1)
				cout<<"OUT OF MAIN PANEL 1"<<endl;
			else
				cout<<panels[i].name<<' '<<child[i]<<endl;
		}
	}
	
	return 0;
}