#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;

pss split(string s) {
  ll k;
  REP(i, 0, s.size()) if(s[i] == '-') k = i;
  string fi = s.substr(0, k);
  string se = s.substr(k + 1, s.size() - k - 1);
  return pss(fi, se);
}

void dfs(ll v, vector<vector<ll>> &E, vector<bool> &used, vector<ll> &color, ll c) {
  if(used[v]) return;
  used[v] = true;

  color[v] = c;
  for(ll u : E[v]) dfs(u, E, used, color, -c);
}

bool dfs2(ll v, ll g, vector<vector<ll>> &E, vector<bool> &used) {
  if(v == g) return true;

  if(used[v]) return false;
  used[v] = true;

  bool ret = false;
  for(ll u : E[v]) ret = ret || dfs2(u, g, E, used);
  return ret;
}

int main(void) {
  ll N, M;
  while(cin >> N, N) {
    vector<string> uniq;
    vector<pss> C(N);
    REP(i, 0, N) {
      string s;
      cin >> s;
      C[i] = split(s);
      uniq.push_back(C[i].first);
      uniq.push_back(C[i].second);
    }
    cin >> M;
    vector<pss> Q(M);
    REP(i, 0, M) {
      string s;
      cin >> s;
      Q[i] = split(s);
      uniq.push_back(Q[i].first);
      uniq.push_back(Q[i].second);
    }
    sort(uniq.begin(), uniq.end());
    uniq.erase(unique(uniq.begin(), uniq.end()), uniq.end());

    vector<pll> cross(N), query(M);
    REP(i, 0, N) {
      cross[i].first = lower_bound(uniq.begin(), uniq.end(), C[i].first) - uniq.begin();
      cross[i].second = lower_bound(uniq.begin(), uniq.end(), C[i].second) - uniq.begin();
    }
    REP(i, 0, M) {
      query[i].first = lower_bound(uniq.begin(), uniq.end(), Q[i].first) - uniq.begin();
      query[i].second = lower_bound(uniq.begin(), uniq.end(), Q[i].second) - uniq.begin();
    }

    ll n = uniq.size();
    vector<vector<ll>> E(n), R(n), A(n), X(n);
    REP(i, 0, N) {
      E[cross[i].first].push_back(cross[i].second);
      R[cross[i].second].push_back(cross[i].first);
      A[cross[i].first].push_back(cross[i].second);
      A[cross[i].second].push_back(cross[i].first);
      X[cross[i].first].push_back(cross[i].second);
    }

    vector<bool> used(n, false);
    vector<ll> color(n);
    ll c = 1;
    REP(i, 0, n) if(!used[i]) dfs(i, A, used, color, c++);

    vector<vector<bool>> e(n, vector<bool>(n, false));
    REP(i, 0, N) e[cross[i].first][cross[i].second] = true;
    REP(i, 0, n) REP(j, 0, n) {
      bool c1 = false, c2 = true;
      REP(k, 0, n) {
        c1 = c1 || (e[i][k] && e[j][k]) || (e[k][i] && e[k][j]);
        c2 = c2 && !(e[i][k] && e[k][j]) && !(e[j][k] && e[k][i]);
      }
      if(c1 && c2) {
        X[i].push_back(j);
        X[j].push_back(i);
      }
    }

    set<ll> st;
    REP(i, 0, N) {
      st.insert(cross[i].first);
      st.insert(cross[i].second);
    }

    cout << st.size() << endl;
    REP(i, 0, M) {
      ll s = query[i].first, t = query[i].second;
      vector<bool> used2(n, false);
      bool ok = color[s] == -color[t] && dfs2(s, t, X, used2);
      cout << (ok ? "YES" : "NO") << endl;
    }
  }
}

