#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <map>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define pii pair<int,int>

#define MAXT 200

bool graph[MAXT][MAXT]={};
bool graph2[MAXT][MAXT]={};
int color[MAXT];

void dfs(int a, int base){
    rep(i,MAXT){
        if(color[i]==0 && (graph[a][i]||graph[i][a])){
            color[i]=-color[a];
            dfs(i,base);
        }
    }
}

void read(string &s1, string &s2){
    string s;
    cin>>s;
    int i=s.find_first_of('-');
    s1=s.substr(0, i);
    s2=s.substr(i+1);
}

int main(){
    while(1){
        fill_n(*graph, MAXT*MAXT, false);
        fill_n(*graph2, MAXT*MAXT, false);
        fill_n(color, MAXT, 0);

        int N,M;
        cin>>N;
        if(N==0) break;
        map<string,int> lnames;
        int lindex = 0;
        rep(ii,N){
            string s1,s2;
            read(s1,s2);
            //cout<<s1<<" "<<s2<<endl;
            if(!lnames.count(s1)){
                lnames[s1] = lindex++;
            }
            if(!lnames.count(s2)){
                lnames[s2] = lindex++;
            }
            graph[lnames[s1]][lnames[s2]]=true;
            //cout<<lnames[s1]<<" "<<lnames[s2]<<endl;
        }

        /*rep(i,lindex){
            rep(j,lindex){
                cout<<graph[i][j]<<" ";
            }
            cout<<endl;
        }*/

        int base=1;
        rep(i,lindex){
            if(color[i]!=0) continue;
            color[i]=base;
            dfs(i,base);
            base++;
        }

        rep(i,lindex){
            rep(j,lindex){
                bool fa = false,fb=true;
                rep(k,lindex){
                    if((graph[i][k] || graph[k][i]) && (graph[j][k] || graph[k][j])){
                        fa = true;
                    }
                    if((graph[k][i] && graph[j][k]) || (graph[i][k] && graph[k][j])){
                        fb=false;break;
                    }
                }
                if(fa&&fb){
                    graph2[i][j] = true;
                    graph2[j][i] = true;
                }
            }
        }
        rep(i,lindex){
            rep(j,lindex){
                graph[i][j] |= graph2[i][j];
            }
        }

        rep(k,lindex){
            rep(i,lindex){
                rep(j,lindex){
                    if(graph[i][k] && graph[k][j]){
                        graph[i][j]=true;
                    }
                }
            }
        }

        /*cout<<endl;
        rep(i,lindex){
            rep(j,lindex){
                cout<<graph[i][j]<<" ";
            }
            cout<<endl;
        }*/

        cout<<lindex<<endl;

        cin>>M;
        rep(ii,M){
            string s1,s2;
            read(s1,s2);

            if(!lnames.count(s1) || !lnames.count(s2)){
                cout<<"NO"<<endl;
            }
            else
            {
                int p1=lnames[s1], p2=lnames[s2];
                if(graph[p1][p2] && color[p1] != color[p2] && color[p1] + color[p2] == 0){
                    cout<<"YES"<<endl;
                }
                else
                {
                    cout<<"NO"<<endl;
                }
            }
        }
    }
    return 0;
}

Measured memory_bytes: 1328