#include #include #include #include #include #include #include #include using namespace std; set street; map > str_g; map > str_l; map > str_s; set check; int n, m; bool Run(const string& a, const string& b, bool diff) { // cout << a << " " << b << " " << str_g.size() << endl; if(a == b) return !diff; set& s = str_g[a]; for(set::iterator i = s.begin(); i != s.end(); ++i) { if(check.find(*i) != check.end()) continue; check.insert(*i); if(Run(*i, b, !diff)) return true; } set& t = str_s[a]; for(set::iterator i = t.begin(); i != t.end(); ++i) { if(check.find(*i) != check.end()) continue; check.insert(*i); if(Run(*i, b, diff)) return true; } return false; } bool IsEmpty(set& a, set& b) { vector res; set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res.empty(); } void Same() { set::iterator i, j; for(i = street.begin(); i != street.end(); ++i) { j = i; for(++j; j != street.end(); ++j) { set& a = str_g[*i]; set& b = str_l[*i]; set& c = str_g[*j]; set& d = str_l[*j]; if(IsEmpty(a,d) && IsEmpty(b,c)) if(!IsEmpty(a,c) || !IsEmpty(b,d)) { //cout << *j << " " << *i << endl; str_s[*i].insert(*j); str_s[*j].insert(*i); } } } } int main() { while(cin >> n) { if(n == 0) break; street.clear(); str_g.clear(); str_l.clear(); str_s.clear(); check.clear(); string str; for(int i = 0; i < n; ++i) { cin >> str; int c = str.find('-'); string a = str.substr(0, c); string b = str.substr(c+1); str_g[a].insert(b); str_l[b].insert(a); street.insert(a); street.insert(b); } Same(); cout << street.size() << endl; cin >> m; for(int i = 0; i < m; ++i) { cin >> str; int c = str.find('-'); string a = str.substr(0, c); string b = str.substr(c+1); check.clear(); if(Run(a, b, true)) cout << "YES" << endl; else cout << "NO" << endl; } } }