#include <bits/stdc++.h>
using namespace std;
string S,ans;
int pos,acnt,d;
int X,Y;


string get_str(){
  string res;
  while(S[pos]!='>') res+=S[pos++];
  pos++;
  return res;
}

int get_xy(){
  int res=0;
  while(S[pos]!=','&&S[pos]!='<')res=res*10+S[pos++]-'0';
  if(S[pos]==',')pos++;
  return res;
}

void bnf(string str){
  string name;
  int cnt=0,f=0;
  
  while(S[pos]=='<'&&S[pos+1]!='/'){
    pos++;
    name=get_str();
    int x1=get_xy();
    int y1=get_xy();
    int x2=get_xy();
    int y2=get_xy();
    if(x1<=X&&X<=x2&&y1<=Y&&Y<=y2)f=1,bnf(name);
    else cnt++;
    string res;
    while(res!=name){
      if(S[pos+1]=='/')pos+=2;
      else pos++;
      res=get_str();
    }
  }
  if(!f)ans=str,acnt=cnt;
}

int main(){
  int n;
  while(cin>>n,n){
    cin>>S;
    S+="</OUT OF MAIN PANEL>";
      while(n--){
	cin>>X>>Y;
	pos=0;
	acnt=0;
	bnf("OUT OF MAIN PANEL");
	cout <<ans<<" "<<acnt<<endl;
      }
  }
  return 0;
}