### main

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

  ids = {}
  prices = []

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

  m = gets.strip.to_i

  unresolved = []
  deps = n.times.map{[]}

  m.times.each do
    rs = gets.strip.split(' ')
    id = ids[rs.shift]
    k = rs.shift

    rcp = rs.map{|i| ids[i]}
    unresolved[id] = k.to_i

    for id0 in rcp
      deps[id0] << id
    end
  end

  target = ids[gets.strip]

  sums = n.times.map{0}
  mcs = []
  q = []

  for id in (0...n)
    if unresolved[id].nil?
      mcs[id] = prices[id]
      for id0 in deps[id]
        sums[id0] += mcs[id]
        unresolved[id0] -= 1
      end
    else
      q << id
    end
  end

  cont = true

  while cont && ! q.empty?
    q0 = q.select{|id| unresolved[id] == 0}
    q -= q0

    for id in q0
      mcs[id] = (prices[id] < sums[id]) ? prices[id] : sums[id]
      if id == target
        cont = false
        break
      end
      for id0 in deps[id]
        sums[id0] += mcs[id]
        unresolved[id0] -= 1
      end
    end
  end

  puts mcs[target]
end