import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

/* 驥阪∩縺、縺攻nionFind譛ィ */
class UnionFindW {
    alias int INT;
    static const INT INF = int.max / 4;
    struct Pair {
        int index;
        INT value;
    }
    int[] parent;
    INT[] value;
    this(int N) {
        parent = new int[N + 1]; parent[] = -1;
        value = new INT[N + 1];  value[] = 0;
    }
    /* 
     * root :: 繧、繝ウ繝?ャ繧ッ繧ケ -> (繝ォ繝シ繝医?繧、繝ウ繝?ャ繧ッ繧ケ, 繝ォ繝シ繝医→縺ョ霍晞屬)
     */
    Pair root(int x) {
        if (parent[x] == -1) return Pair(x, 0);
        int p = parent[x];
        auto v = value[x];
        auto proot = root(p);
        parent[x] = proot.index;
        value[x] = v + proot.value;
        return Pair(parent[x], value[x]);
    }
    /* 
     * query :: (繧、繝ウ繝?ャ繧ッ繧ケx, 繧、繝ウ繝?ャ繧ッ繧ケy) -> y縺ィx縺ョ蟾ョ
     */
    INT query(int x, int y) {
        auto xroot = root(x),
             yroot = root(y);
        if (xroot.index != yroot.index) return INF;
        return yroot.value - xroot.value;
    }
    /*
     * merge :: (繧、繝ウ繝?ャ繧ッ繧ケx, 繧、繝ウ繝?ャ繧ッ繧ケy, y縺ィx縺ョ蟾ョw) -> ()
     *
     * x縺ィy縺後☆縺ァ縺ォ隕ェ蟄宣未菫ゅｒ謖√▲縺ヲ縺?※,縺昴?蟾ョ縺詣縺ァ縺ェ縺代ｌ縺ー逡ー蟶ク邨ゆコ?     */
    void merge(int x, int y, INT w) {
        auto xroot = root(x),
             yroot = root(y);
        if (xroot.index == yroot.index) {
            //assert(query(x, y) == w);
            return;
        }
        auto px = xroot.index, py = yroot.index;
        auto dx = xroot.value, dy = yroot.value;
        /* value[y] - value[x] = w
         * value[x] - value[px] = dx
         * value[y] - value[py] = dy
         * value[py] - value[px] = (value[y] - dy) - (value[x] - dx)
         *                       = (value[y] - value[x]) - (dy - dx)
         *                       = w - dy + dx
         */
        parent[py] = px;
        value[py] = w - dy + dx;
    }
}

void main() {
    int N;
    int[string] M;
    int C;

    int[][] G;
    int[][] rG;
    UnionFindW uf;

    bool input() {
        scanf("%d\n", &N);
        if (N == 0) return false;
        M.destroy; C = 0;
        G = new int[][201];
        rG = new int[][201];
        uf = new UnionFindW(201);
        foreach (i; 0 .. N) {
            auto ss = readln.chomp.split("-");
            auto s = ss[0],
                 t = ss[1];
            if (s !in M) { M[ s ] = C++; }
            if (t !in M) { M[ t ] = C++; }
            G[ M[s] ] ~= M[t];
            rG[ M[t] ] ~= M[s];
            uf.merge(M[s], M[t], 1);
        }
        return true;
    }

    bool reachable(int u, int v) {
        auto used = new bool[C];
        bool dfs(int prev, int cur) {
            if (cur == v) return true;
            if (prev >= 0 && used[cur]) return false;
            used[cur] = true;
            foreach (next; G[cur]) {
                if (next == prev) continue;
                if (dfs(cur, next)) return true;
            }
            return false;
        }
        return dfs(-1, u);
    }

    int[] dist;
    void calcDist(int root) {
        struct S { int v, cost; }
        dist = new int[C];
        dist[] = -1;
        DList!(S) Q;
        Q.insert(S(root, 0));
        while (!Q.empty) {
            auto c = Q.front; Q.removeFront;
            dist[c.v] = c.cost;
            foreach (next; rG[c.v]) {
                if (dist[next] >= 0) continue;
                Q.insert(S(next, c.cost + 1));
            }
        }
    }

    void solve() {
        writeln(C);
        foreach (int i; 0 .. C) {
            calcDist(i);
            foreach (int x; 0 .. C) {
                foreach (int y; 0 .. C) {
                    if (dist[x] < 0 || dist[y] < 0) continue;
                    if (dist[x] == dist[y]) {
                        G[x] ~= y;
                        G[y] ~= x;
                    }
                }
            }
        }
        int Q; scanf("%d\n", &Q);
        foreach (q; 0 .. Q) {
            auto ss = readln.chomp.split("-");
            auto s = ss[0],
                 t = ss[1];
            void doit() {
                if (s !in M || t !in M) {
                    writeln("NO");
                    return;
                }
                if (uf.query(M[s], M[t]) % 2 == 0) {
                    writeln("NO");
                    return;
                }
                if (reachable(M[s], M[t])) {
                    writeln("YES");
                    return;
                }
                writeln("NO");
            }
            doit;
        }
    }

    while (input) solve;

}