### subroutines

def min_price(id)
  $used[id] = 1

  return $rcache[id] if ! $rcache[id].nil?

  return ($rcache[id] = $prices[id]) if $recipes[id].nil?

  sum = 0

  for rpid in $recipes[id]
    sum += $used[rpid] ? $prices[rpid] : min_price(rpid)
  end

  $rcache[id] = sum < $prices[id] ? sum : $prices[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