#include<iostream>
#include<cassert>
#include<algorithm>
#include<vector>
#include<cmath>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)

using namespace std;

struct P
{
  string tag;
  int x1,y1,x2,y2,depth,cnt;
  P(string tag="$",int x1=inf,int y1=inf,int x2=inf,int y2=inf,int depth=inf,int cnt=inf):tag(tag),x1(x1),y1(y1),x2(x2),y2(y2),depth(depth),cnt(cnt){}
};

string par;
vector<P> info;
int pos;

string getTag()
{
  assert(par[pos] == '<');
  pos++;
  string tag = "";
  while(par[pos] != '>')
    {
      tag += par[pos];
      pos++;
    }
  assert(par[pos] == '>');
  pos++;
  if(tag[0] == '/')tag = tag.substr(1,tag.size()-1);
  return tag;
}

bool exp(int depth)
{
  if(par[pos+1] == '/')
    {
      return false;
    }

  string tag = getTag();

  vector<int> coor;
  int value = 0;
  while(par[pos] != '<')
    {

      value *= 10;
      value += (par[pos]-'0');
      pos++;
      if(par[pos] == ',')
	{
	  coor.push_back(value);
	  value = 0;
	  pos++;
	}

    }

  coor.push_back(value);

  assert((int)coor.size() == 4);

  int cnt = 0;
  while(exp(depth+1))
    {
      cnt++;
    }

  assert(pos+1 < par.size() && par[pos+1] == '/');

  info.push_back(P(tag,coor[0],coor[1],coor[2],coor[3],depth,cnt));

  string tag2 = getTag();
  assert(tag == tag2);

  return true;

}

int main()
{
  int n;
  while(cin >> n,n)
    {
      info.clear();
      cin >> par;


      pos = 0;
      exp(0);
      /*
      cout << "INFO" << endl;
      rep(i,info.size())
	{
	  cout << info[i].tag << " (" << info[i].x1 << "," << info[i].y1 << ") (" << info[i].x2 << "," << info[i].y2 << ") depth = " << info[i].depth << " cnt = " << info[i].cnt << endl; 
	}
      */


      int p;
      rep(i,n)
	{
	  int x,y;
	  cin >> x >> y;
	  p = inf;
	  rep(j,info.size())
	    {
	      int x1 = info[j].x1, x2 = info[j].x2;
	      int y1 = info[j].y1, y2 = info[j].y2;
	      if(x1 <= x && x <= x2 && y1 <= y && y <= y2)
		{

		  if(p == inf)p = j;
		  else if(info[p].depth < info[j].depth)
		    {
		      assert(info[p].depth != info[j].depth);
		      p = j;
		    }
		}
	    }
	  if(p == inf)cout << "OUT OF MAIN PANEL 1" << endl;
	  else cout << info[p].tag << " " << info[p].cnt << endl;
	}
    }
  return 0;
}