#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
using namespace std;
vector<vector<char> >tree(26);
map<char,int>mp={{'A',0},{'B',1},{'C',2},
{'D',3},{'E',4},{'F',5},
{'G',6},{'H',7},{'I',8},
{'J',9},{'K',10},{'L',11},
{'M',12},{'N',13},{'O',14},
{'P',15},{'Q',16},{'R',17},{'S',18},
{'T',19},{'U',20},{'V',21},{'W',22},
{'X',23},{'Y',24},{'Z',25}};
int flag=-1;
bool isok=false;
void find(char a,char b,int depth){
if(tree[mp[a]].size()==0||isok){
return;
}
for(int i=0;i<tree[mp[a]].size();i++){
char key=tree[mp[a]][i];
if(key==b){
flag=depth;
isok=true;
break;
}
else{
find(key,b,depth+1);
}
}
}
int main(){
int n,m;
int i;
char a,b,c;
char s,t;
cin>>n>>m;
// node tree[26];
for(i=0;i<25;i++){
tree[i].clear();
}
for(i=0;i<n;i++){
getchar();
cin>>a>>b>>c;
if(b!='-') {
tree[mp[a]].push_back(b);
}
if(c!='-'){
tree[mp[a]].push_back(c);
}
}
for(i=0;i<m;i++){
getchar();
cin>>s>>t;
find(s,t,0);
if(flag!=-1){
if(flag==0){
cout<<"child"<<endl;
}
else if(flag==1){
cout<<"grandchild"<<endl;
}
else{
for(int j=0;j<flag-1;j++){
cout<<"great-";
}
cout<<"grandchild"<<endl;
}
}
else{
find(t,s,0);
if(flag!=-1){
if(flag==0){
cout<<"parent"<<endl;
}
else if(flag==1){
cout<<"grandparent"<<endl;
}
else{
for(int j=0;j<flag-1;j++){
cout<<"great-";
}
cout<<"grandparent"<<endl;
}
}
else{
cout<<"-"<<endl;
}
}
flag=-1;
isok=false;
}
return 0;
}