#include <iostream>
using namespace std;
const int N = 30;
int son[N];
int find(int a, int b)
{
    int cnt = 0;
    while(a != son[a] && a != b)
    {
        a = son[a];
        cnt ++;
    }
    if(a == b) return cnt;
    return 0;
}
int main() {
    string s3 = "great-", s2 = "grand", s11 = "parent", s12 = "child", s1;
    int n, m;
    while(cin >> n >> m)
    {
        for(int i = 0; i < N; i ++) son[i] = i;
        while(n --)
        {
            char a, b, c;
            cin >> a >> b >> c;
            if(b != '-') son[b - 'A'] = a - 'A';
            if(c != '-') son[c - 'A'] = a - 'A';
        }
        while(m --)
        {
            char a, b;
            cin >> a >> b;
            int len = find(a - 'A', b - 'A');
            if(len) s1 = s11;
            else 
            {
                len = find(b - 'A', a - 'A');
                s1 = s12;
            }
            if(!len)
            {
                cout << '-' << endl;
                continue;
            }
            while(len)
            {
                if(len >= 3) cout << s3;
                else if(len == 2) cout << s2;
                else cout << s1 << endl;
                len --;
            }
        }

    }
    return 0;
    
}
// 64 位输出请用 printf("%lld")