### constant

MAX_N = 200

D_NS = 0
D_EW = 1

### main

loop do
  n = gets.to_i
  break if n == 0

  nm2id = {}
  nid = 0
  cpairs = []

  n.times do
    st0, st1 = gets.strip.split('-')

    if nm2id[st0].nil?
      nm2id[st0] = nid
      nid += 1
    end
    if nm2id[st1].nil?
      nm2id[st1] = nid
      nid += 1
    end
    cpairs << [nm2id[st0], nm2id[st1]]
  end
  #p nm2id
  #p cpairs

  cmat = nid.times.map{nid.times.map{nil}}
  nbrs = nid.times.map{[]}

  cpairs.each do |st0, st1|
    cmat[st0][st1] = 1
    cmat[st1][st0] = -1
    nbrs[st0] << st1
    nbrs[st1] << st0
  end

  cgrps = []
  ncgrp = 0
  drcs = []

  for id in (0...nid)
    next if drcs[id]

    cgrps[id] = ncgrp
    drcs[id] = D_NS
    q = [id]

    while ! q.empty?
      u = q.shift
      ud = drcs[u]

      for v in nbrs[u]
        if ! drcs[v]
          cgrps[v] = ncgrp
          drcs[v] = 1 - ud
          q << v
        end
      end
    end
    #p drcs

    ncgrp += 1
  end

  dsts = ncgrp.times.map{2.times.map{[]}}
  (0...nid).each{|id| dsts[cgrps[id]][drcs[id]] << id}
  #p dsts

  for i in (0...nid)
    cgrp = cgrps[i]
    d = drcs[i]

    for j in dsts[cgrp][d]
      next if i >= j

      cnd1 = false
      cnd23 = true

      for k in dsts[cgrp][1 - d]
        cik = cmat[i][k]
        cjk = cmat[j][k]
        if cik && cjk
          cnd1 = true
          if cik != cjk
            cnd23 = false
            break
          end
        end
      end

      if cnd1 && cnd23
        cmat[i][j] = cmat[j][i] = 0
        nbrs[i] << j
        nbrs[j] << i
      end
    end
  end
  #p cmat
  #p nbrs

  puts nid

  m = gets.to_i

  m.times do
    q0, q1 = gets.strip.split('-').map{|st| nm2id[st]}

    yorn = false
    if q0 && q1 && drcs[q0] && drcs[q1] && drcs[q0] != drcs[q1]
      visited = nid.times.map{false}
      visited[q0] = true
      q = [q0]

      while ! q.empty?
        u = q.shift
        if u == q1
          yorn = true
          break
        end

        for v in nbrs[u]
          next if visited[v]
          cuv = cmat[u][v]
          if cuv && cuv >= 0
            visited[v] = true
            q << v
          end
        end
      end
    end

    puts (yorn ? "YES" : "NO")
  end
end