1 #include <iostream> //1035
2 #include <algorithm>
3 #include <set>
4 #include <map>
5 #include <list>
6 #include <deque>
7 #include <queue>
8 #include <stack>
9 #include <vector>
10 #include <string>
11 #include <fstream>
12 #include <cmath>
13 #include <ctime>
14 #include <cstdio>
15 #include <cctype>
16 #include <cstring>
17 #include <sstream>
18 #include <cstdlib>
19 #include <cassert>
20
21 using namespace std;
22
23 map<char, char> mp;
24
25 int find(char from, char to){
26 int sum = 0;
27 char cur = from;
28 while(cur != to && mp.count(cur) > 0){
29 cur = mp[cur];
30 sum++;
31 }
32 if(cur != to) return -1;
33 return sum;
34 }
35
36 int main(){
37 int n, m, i, sum1, sum2;
38 string s;
39
40 while(cin >> n >> m, m+n){
41 mp.clear();
42 for(i = 0; i < n; i++){
43 cin >> s;
44 mp[s[1]] = s[0];
45 mp[s[2]] = s[0];
46 }
47 for(i = 0; i < m; i++){
48 cin >> s;
49 sum1 = find(s[0], s[1]);
50 sum2 = find(s[1], s[0]);
51 if(sum1 == -1 && sum2 == -1)
52 cout << "-" << endl;
53 else if(sum1 > 0){
54 if(sum1 == 1) cout << "parent" << endl;
55 else {
56 while(sum1 > 2){
57 cout << "great-";
58 sum1--;
59 }
60 cout << "grandparent" << endl;
61 }
62 }else{
63 if(sum2 == 1) cout << "child" << endl;
64 else {
65 while(sum2 > 2){
66 cout << "great-";
67 sum2--;
68 }
69 cout << "grandchild" << endl;
70 }
71 }
72 }
73 }
74 return 0;
75 }