#include <cstring>
#include <iostream>
using namespace std;
const int maxn=100;
int child[maxn];
void outp(int gap,int flag){
    string x;
    if(flag==1) x="parent";
    else x="child";
    if(gap==1) {
        cout<<x<<endl;
        return;
    }
    else if(gap==2){
        cout<<"grand"<<x<<endl;
    }
    else if(gap>2){
        int a=gap-2;
        while(a>0){
            cout<<"great-";
            a--;
        }
        cout<<"grand"<<x<<endl;
    }

}
void init(){
    for(int i=0;i<maxn;i++) child[i]=i;
}
void findkinship(string x){
    int c=x[0]-'A';
    if(x[1]!='-' && x[2]!='-'){
        child[x[1]-'A']=c;
        child[x[2]-'A']=c;
        return;
    }
    else if(x[1]=='-' && x[2]=='-'){
        return;
    }
    else if(x[1]=='-'){
        child[x[2]-'A']=c;
    }
    else child[x[1]-'A']=c;
}
void lookUPK(string x){
    int n1=x[0]-'A';
    int n2=x[1]-'A';
    int gap=1;
    while(child[n1]!=n1 && child[n1]!=n2){
        n1=child[n1];
        gap++;
    }
    if(child[n1]==n2) {
        outp(gap,1);
        return;}
    else{
        n1=x[0]-'A';
        gap=1;
        while(child[n2]!=n2 && child[n2]!=n1){
            n2=child[n2];
            gap++;
        }
        if(child[n2]==n2) {
            cout<<"-"<<endl;
            return;
            }
        else {
            outp(gap,2);
            return;
        }
    }
}
int main() {
    int n,m;
    while(cin>>n>>m){
        init();
        for(int i=0;i<n;i++){
            string kinship;
            cin>>kinship;
            findkinship(kinship);
        }
        for(int j=0;j<m;j++){
            string l;
            cin>>l;
            lookUPK(l);
        }
    }
}