//土尔逊Torson 编写于2023/06/20
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

const int MAXN12901 = 50;

int son12901[MAXN12901];      //父节点是儿子,所以以son命名
int height12901[MAXN12901];   

void Initial12901() {
	for (int i = 0; i < MAXN12901; i++) {
		son12901[i] = i;
		height12901[i] = 0;
	}
}

int Find12901(int x, int y, int count) {
	if (son12901[x] == son12901[y] && x != y && son12901[x] != x && son12901[y] != y) return -1;
	if (height12901[x] < height12901[y]) {
		y = son12901[y];
		count++;
		return Find12901(x, y, count);
	}
	else if (height12901[x] > height12901[y]) {
		x = son12901[x];
		count++;
		return Find12901(x, y, count);
	}
	else {
		return count;
	}
	return -1;
}

void Expo12901(int a, int b) {
	if (height12901[a] >= height12901[b]) {
		int answer = Find12901(a, b, 0);
		string str1 = "";
		if (answer == -1) {
			str1 += "-";
			cout << str1 << endl;
		}
		else if (answer == 1) {
			str1 += "parent";
			cout << str1 << endl;
		}
		else if (answer == 2) {
			str1 += "grandparent";
			cout << str1 << endl;
		}
		else if (answer > 2) {
			str1 += "grandparent";
			while (answer > 2) {
				str1 = "great-" + str1;
				answer--;
			}
			cout << str1 << endl;
		}
	}
	else if (height12901[a] < height12901[b]) {
		int answer = Find12901(a, b, 0);
		string str1 = "";
		if (answer == -1) {
			str1 += "-";
			cout << str1 << endl;
		}
		else if (answer == 1) {
			str1 += "child";
			cout << str1 << endl;
		}
		else if (answer == 2) {
			str1 += "grandchild";
			cout << str1 << endl;
		}
		else if (answer > 2) {
			str1 += "grandchild";
			while (answer > 2) {
				str1 = "great-" + str1;
				answer--;
			}
			cout << str1 << endl;
		}
	}
}

int main() {
	int n, m;
	while (cin >> n >> m) {
		Initial12901();
		for (int i = 0; i < n; ++i) {
			string str;
			cin >> str;
			int a = str[0] - 'A';
			if (str[1] != '-') {
				int b = str[1] - 'A';
				son12901[b] = a;
				height12901[b] = 1 + height12901[a];
			}
			if (str[2] != '-') {
				int c = str[2] - 'A';
				son12901[c] = a;
				height12901[c] = 1 + height12901[a];
			}
		}
		for (int i = 0; i < m; ++i) {
			string str;
			cin >> str;
			int a = str[0] - 'A';
			int b = str[1] - 'A';

			Expo12901(a, b);
		}
	}
	system("pause");
	return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")