### global variables

$mccache = {}

### subroutines

def min_cost(item, prices, recipes)
  return $mccache[item] if ! $mccache[item].nil?

  recipe = recipes[item]
  return ($mccache[item] = prices[item]) if recipe.nil?

  sum = recipe.inject(0){|s, ri| s + min_cost(ri, prices, recipes)}

  mc = (prices[item] < sum) ? prices[item] : sum
  $mccache[item] = mc
end

### main

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

  prices = {}

  n.times.each do
    item, price = gets.strip.split(' ')
    prices[item] = price.to_i
  end

  m = gets.strip.to_i

  recipes = {}

  m.times.each do
    rs = gets.strip.split(' ')
    item = rs.shift
    k = rs.shift

    recipes[item] = rs
  end

  target = gets.strip

  $mccache = {}

  puts min_cost(target, prices, recipes)
end