link
#include <bits/stdc++.h>
using namespace std;
typedef pair<string, int> PII;
map<string, PII> mp;
const int MAXN = 100010;
int n;
string check(string s1, string s2){
int cnt1 = 0, cnt2 = 0;
while(s1 != ""){
cnt2 = 0;
string s = s2;
while(s != ""){
if(s1 == s && (cnt1 < 4 || cnt2 < 4)) return ("No\n");
if(cnt1 >= 4 && cnt2 >= 4) return("Yes\n");
s = mp[s].first;
cnt2++;
}
s1 = mp[s1].first;
cnt1++;
}
return ("Yes\n");
}
int main(){
scanf("%d", &n);
string m, x;
while(n--){
cin >> m >> x;
if(x.back() == 'm'){
mp[m].second = 1;
}else if(x.back() == 'n'){
mp[m] = {x.substr(0, x.size() - 4) ,1};
}else if(x.back() == 'f'){
mp[m].second = 0;
}else{
mp[m] = {x.substr(0, x.size() - 7) ,0};
}
}
int M;
scanf("%d", &M);
while(M--){
string m1, x1, m2, x2;
cin >> m1 >> x1 >> m2 >> x2;
if(!mp.count(m1) || !mp.count(m2)) puts("NA");
else if(mp[m1].second == mp[m2].second) puts("Whatever");
else{
cout << check(m1, m2);
}
}
return 0;
}