### subroutines

def min_price(id)
  stack = [id]

  while ! stack.empty?
    #p stack

    rid = stack.last

    if ! $rcache[rid].nil?
      stack.pop
      next
    end

    if $recipes[rid].nil?
      $rcache[rid] = $prices[rid]
      stack.pop
      next
    end

    need_rec = false
    rtmp = []

    for rpid in $recipes[rid]
      if $rcache[rpid].nil?
        if ! stack.include?(rpid)
          stack << rpid
          need_rec = true
        else
          rtmp[rpid] = $prices[rpid]
        end
      else
        rtmp[rpid] = $rcache[rpid]
      end
    end

    next if need_rec

    sum = 0

    for rpid in $recipes[rid]
      sum += rtmp[rpid]
    end

    $rcache[rid] = sum < $prices[rid] ? sum : $prices[rid]
    stack.pop
  end

  $rcache[id]
end

### main

while true
  n = gets.strip.to_i
  break if n == 0

  $ids = {}
  $prices = []

  for id in (0...n)
    name, pr = gets.strip.split(' ')
    $ids[name] = id
    $prices << pr.to_i
  end

  m = gets.strip.to_i

  $recipes = []

  m.times do
    name, k, *rs = gets.strip.split(' ')
    id = $ids[name]

    $recipes[id] = rs.map{|name0| $ids[name0]}
  end

  $rcache = []
  $used = []

  item = gets.strip
  min_pr = min_price($ids[item])

  puts min_pr
end