题目1035:找出直系亲属
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
Scanner cin = new Scanner(new BufferedInputStream(System.in)) ;
PrintWriter cout = new PrintWriter(System.out) ;
int n , m ;
while(true){
n = cin.nextInt() ;
m = cin.nextInt() ;
if(n == 0 && m == 0) break ;
new Task().solve(n , m , cin , cout);
}
cout.flush() ;
}
}
class Task{
Person[] person = new Person[26] ;
Task() {
for(int i = 0 ; i < 26 ; i++)
person[i] = new Person() ;
}
void solve(int n , int m , Scanner cin , PrintWriter cout){
while(n-- > 0){
String s = cin.next() ;
int son = s.charAt(0) - 'A' ;
if(s.charAt(1) != '-') person[son].father = s.charAt(1) - 'A' ;
if(s.charAt(2) != '-') person[son].mother = s.charAt(2) - 'A' ;
}
while(m-- > 0){
String s = cin.next() ;
int u = s.charAt(0) - 'A' ;
int v = s.charAt(1) - 'A' ;
int utov = dfs(u, v) ;
int vtou = dfs(v, u) ;
if(utov > 0) cout.println(getNameUtoV(utov)) ;
else if(vtou > 0) cout.println(getNameVtoU(vtou)) ;
else cout.println("-") ;
}
// cout.flush() ;
}
int dfs(int u , int v){
if(v == u) return 0 ;
if(person[u].father != null){
int dep = dfs(person[u].father , v ) ;
if(dep != -1) return dep + 1 ;
}
if(person[u].mother != null){
int dep = dfs(person[u].mother , v ) ;
if(dep != -1) return dep + 1 ;
}
return -1 ;
}
String getNameVtoU(int dep){
if(dep == 1) return "parent" ;
else if(dep == 2) return "grandparent" ;
else{
String name = "" ;
for(int i = 3 ; i <= dep ; i++) name += "great-" ;
name += "grandparent" ;
return name ;
}
}
String getNameUtoV(int dep){
if(dep == 1) return "child" ;
else if(dep == 2) return "grandchild" ;
else{
String name = "" ;
for(int i = 3 ; i <= dep ; i++) name += "great-" ;
name += "grandchild" ;
return name ;
}
}
}
class Person{
Integer father ;
Integer mother ;
public Person() {
father = mother = null ;
}
}