### 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

  drcs = []
  drcs[0] = D_NS
  q = [0]

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

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

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

  for d in [D_NS, D_EW]
    sts0 = dsts[d]
    sts1 = dsts[1 - d]
    nsts0 = sts0.length
    nsts1 = sts1.length

    for i in (0...nsts0)
      sti = sts0[i]
      for j in ((i + 1)...nsts0)
        stj = sts0[j]
        cnd1 = false
        cnd23 = true

        for k in (0...nsts1)
          stk = sts1[k]

          cik = cmat[sti][stk]
          cjk = cmat[stj][stk]
          if cik && cjk
            cnd1 = true
            if cik != cjk
              cnd23 = false
              break
            end
          end
        end

        if cnd1 && cnd23
          cmat[sti][stj] = cmat[stj][sti] = 0
          nbrs[sti] << stj
          nbrs[stj] << sti
        end
      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