#!/usr/bin/ruby -Ku

require 'pp'

def check_lowest(item, shop, recipe)
  if recipe.include?(item) then
    sum = 0
    recipe[item].each{ |it|
      tmp = check_lowest(it, shop, recipe)
      sum += tmp
    }
    return [sum, shop[item]].min
  else
    return shop[item]
  end
end

loop do
  n = gets.to_i
  break if n == 0
  
  shop = Hash.new
  n.times{
    data = gets.split(nil)
    shop[data[0]] = data[1].to_i
  }
  
  m = gets.to_i  
  
  recipe = Hash.new
  m.times{
    data = gets.split(nil)
    item, n = data
    recipe[item] = data.slice(2, n.to_i)
  }
  
  item = gets.split(nil)[0]
  
  puts check_lowest(item, shop, recipe)
end