#include <bits/stdc++.h>
using namespace std;
int relations_num, questions_num;
int child, mom, papa;
char c, m, p;
char pp1, pp2;
int p1, p2;
int myChild[100];
int main() {
    while (cin >> relations_num >> questions_num) {
        for (int i = 0; i < 100; i++) myChild[i] = -1;
        for (int i = 0; i < relations_num; i++) {
            cin >> c >> m >>
                p; //形式如ABC的字符串,表示A的父母亲分别是B和C
            child = c - 'A';
            if (myChild[child] == -1)
                myChild[child] = child;
            if (m != '-') {
                mom = m - 'A';
                myChild[mom] = child;
            }
            if (p != '-') {
                papa = p - 'A';
                myChild[papa] = child;
            }
        }
        for (int i = 0; i < questions_num; i++) {
            cin >> pp1 >> pp2; //形式如FA的字符串,表示询问F和A的关系
            p1 = pp1 - 'A';
            p2 = pp2 - 'A';
            /*找一下p1p2的关系
            因为不知道p1p2的关系,所以分类讨论一下
            1.假设p1是长辈*/
            bool p1_is_old = 1; //假设p1是长辈
            bool p2_is_old = 1; //假设p2是长辈
            char p = p1; //保存一下p1,p2
            char q = p2;
            int levelp = 0; //记录一下隔了几代
            int levelq = 0;
            //将p1作为长辈,开始找晚辈
            while (p != q && myChild[p] != p) {
                p = myChild[p];
                levelp++;//记录找了几代
            }
            //判读一下p1是长辈这个假设成立与否
            if (p != q) {
                p1_is_old = 0; //p1不是长辈
            }
            //恢复原位
            p = p1;
            q = p2;
            while (q != p && myChild[q] != q) {
                q = myChild[q];
                levelq++;
            }
            //判读一下p2是长辈这个假设成立与否
            if (q != p) {
                p2_is_old = 0; //p2不是长辈
            }

            if ((p1_is_old && p2_is_old) || (!p1_is_old && !p2_is_old)) {
                cout << "-" << endl;
            } else if (p1_is_old) {
                if (levelp == 1) cout << "parent" << endl;
                if (levelp > 1) {
                    for (int i = 0; i < levelp - 2; i++) {
                        cout << "great-";
                    }
                    cout << "grandparent" << endl;
                }
            } else if (p2_is_old) {
                if (levelq == 1) cout << "child" << endl;
                if (levelq > 1) {
                    for (int i = 0; i < levelq - 2; i++) {
                        cout << "great-";
                    }
                    cout << "grandchild" << endl;
                }
            }
        }
    }
}