//L2-030 冰岛人
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
static const int MAX_N = 1e5 + 5;
struct Per {
bool sex;
int id;
};
map<string, Per>mps1;
int pre[MAX_N];
string s1, s2, s3, s4;
void init() {
for (int i = 1; i < MAX_N; ++i)pre[i] = i;
}
int Find(int x) {
int rt = pre[x];
if (pre[x] != x) rt = Find(pre[x]);
return rt;
}
bool judge(int x, int y) {
/*
两人存在公共祖先的情况下
判断这个共同祖先是否在五代以内
*/int ret = 1;
while (x != pre[x] && y != pre[y]) {
x = pre[x];
y = pre[y];
++ret;
if (ret >= 5) return true; //不在五代以内
if (x == y) return false; //五代以内存在共同祖先
}
return false;
/*
能够跳出while循环
说明某个人无五代祖先
但是已经判断出两人存在公共祖先
说明两人五代以内存在公共祖先,返回false
*/
}
int main() {
ios::sync_with_stdio(false);
init();
int n;
cin >> n;
int cnt = 0;
vector<vector<string> >in(n);
for (int i = 0; i < n; ++i) {
cin >> s1 >> s2;
mps1[s1].id = ++cnt;
in[i].push_back(s1);
in[i].push_back(s2);
}
for (int i = 0; i < n; ++i) {
s1 = in[i][0], s2 = in[i][1];
if (s2.length() > 4 && s2.substr(s2.length() - 4, 4) == "sson") {
pre[mps1[s1].id] = mps1[s2.substr(0, s2.length() - 4)].id;
mps1[s1].sex = true;
}
else if (s2.length() > 7 && s2.substr(s2.length() - 7, 7) == "sdottir") {
pre[mps1[s1].id] = mps1[s2.substr(0, s2.length() - 7)].id;
mps1[s1].sex = false;
}
else {
if (s2.back() == 'm') mps1[s1].sex = true;
else mps1[s1].sex = false;
}
}
int m;
cin >> m;
while (m--) {
cin >> s1 >> s2 >> s3 >> s4;
//map和vector在越界情况都会给出此类型的默认值,如果是基本类型,则返回零值;
//如果是struct或class,如果里面的变量定义了默认值,则返回定义的默认值,如果没有,返回零值。
if (!mps1[s1].id || !mps1[s3].id) printf("NA\n");
else if (mps1[s1].sex == mps1[s3].sex) printf("Whatever\n");
else if (Find(mps1[s1].id) != Find(mps1[s3].id)) printf("Yes\n");
else if (judge(mps1[s1].id, mps1[s3].id)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl '\n'
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f,N=1e5+10;
int n,m;
struct node
{
int id;
string mi,xi;
string x;
}a[N];
map<string,string>p;
map<string,int>mp;
map<int,string>mp1;
// bool check(string x,string y,int cnt)
// {
// if(cnt>5||x==""||y=="")return true;
// if(x==y)return false;
// return check(p[x],p[y],cnt+1);
// }
bool check(string a, string b)//检查是否在五代之内,a,b表示最后需要判断的人名
{
map<string,int>num;//表示经过了几代人
for(int i=1; a != ""; i++)//找a的最大祖先,确定有几代人
{
num[a] = i;
a = p[a];
}
for(int i=1; b != ""; i++)
{
if(num[b])//如果存在b的代数就证明a和b有共同祖先,
//因为我们只求了a的祖先有几代人,并没有求b的,a和b如果没有共同祖先不会进入判断
{
if(num[b] >= 5 && i >= 5)
return true;
else
return false;
}
b = p[b];
}
return true;
}
void solve()
{
cin>>n;
//memset(p,-1,sizeof p);
for(int i=1;i<=n;i++)a[i].id=i;
for(int i=1;i<=n;i++)
{
string mi,xi,x;
cin>>mi>>xi;
//string s=mi+xi;
mp[mi]=i;
mp1[i]=mi;
string fa="";
if(xi.back()!='r'&&xi.back()!='n')
{
x=xi.back();
}
else
{
if(xi.back()=='n')
{
fa=xi.substr(0,xi.size()-4);
p[mi]=fa;
//cout<<fa<<endl;
x="m";
}
else
{
fa=xi.substr(0,xi.size()-7);
p[mi]=fa;
//cout<<fa<<endl;
x="f";
}
}
a[i]={i,mi,xi,x};
}
cin>>m;
while(m--)
{
string mi1,xi1,mi2,xi2;
cin>>mi1>>xi1>>mi2>>xi2;
if(mp.find(mi1)==mp.end()||mp.find(mi2)==mp.end())
{
cout<<"NA"<<endl;
continue;
}
int id1=mp[mi1],id2=mp[mi2];
if(a[id1].x==a[id2].x)
{
cout<<"Whatever"<<endl;
}
else
{
if(check(mi1,mi2))cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t=1;
while(t--)
{
solve();
}
return 0;
}