1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cmath>
6 #include <cctype>
7 #include <vector>
8 #include <list>
9 #include <deque>
10 #include <stack>
11 #include <queue>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <algorithm>
16
17
18 using namespace std;
19
20
21
22 struct Node{
23 char id;
24 int lch,rch;
25 }node[1000];
26
27 int length=1;
28
29
30 int dep;
31
32
33 int findfa(int v1,int v2,int depp)
34 {
35
36 if(v1==0)
37 return 0;
38
39 if(v1==v2)
40 {
41 dep=depp;
42 return 1;
43 }
44 else
45 {
46 findfa(node[v1].lch,v2,depp+1);
47 findfa(node[v1].rch,v2,depp+1);
48 }
49 return 0;
50 }
51
52
53
54
55
56 int main()
57 {
58
59
60 int n,m;
61
62 int i,j,k;
63
64 while(scanf("%d%d",&n,&m)!=EOF)
65 {
66 if((n==0)&&(m==0))
67 break;
68
69 getchar();
70
71 for(i=1;i<=26;i++)
72 {
73 node[i].lch=node[i].rch=0;
74 }
75
76
77
78 int ans='A'-1;
79 for(i=0;i<n;i++)
80 {
81 char s[5];
82
83 gets(s);
84
85 if(s[1]!='-')
86 {
87 node[s[0]-ans].lch=s[1]-ans;
88 }
89 else
90 node[s[0]-ans].lch=0;
91 if(s[2]!='-')
92 node[s[0]-ans].rch=s[2]-ans;
93 else
94 node[s[0]-ans].rch=0;
95
96 }
97
98
99 for(i=0;i<m;i++)
100 {
101 char a,b;
102
103 scanf("%c%c",&a,&b);
104 getchar();
105
106 int vs=a-ans;
107 int ve=b-ans;
108
109 dep=-1;
110
111
112 findfa(vs,ve,0);
113
114 if(dep==-1)
115 {
116
117 findfa(ve,vs,0);
118 if(dep==-1)
119 {
120 printf("-\n");
121
122 }
123 else
124 {
125 if(dep==1)
126 {printf("parent\n");}
127 else if(dep==2)
128 {
129 printf("grandparent\n");
130 }
131 else
132 {
133 while(dep>2)
134 {
135 printf("great-");
136 dep--;
137 }
138
139 printf("grandparent\n");
140 }
141 }
142 }
143 else
144 {
145 if(dep==1)
146 {printf("child\n");}
147 else if(dep==2)
148 {
149 printf("grandchild\n");
150 }
151 else
152 {
153 while(dep>2)
154 {
155 printf("great-");
156 dep--;
157 }
158
159 printf("grandchild\n");
160 }
161
162 }
163 }
164
165
166 }
167
168
169
170
171
172
173
174
175
176 return 0;
177 }