### constant

MAX_INT = 1 << 30

### main

loop do
  n = gets.to_i
  break if n == 0

  mkts = {}
  for i in (1..n)
    strs = gets.strip.split
    k = strs.shift.to_i

    k.times do
      name = strs.shift
      val = strs.shift.to_i

      mkts[name] = [] if mkts[name].nil?
      mkts[name] << [i, val]
    end
  end
  #p mkts

  goods = []
  ng = false

  q = gets.to_i
  q.times do
    name = gets.strip
    goods << name

    ng = true if mkts[name].nil?
  end
  #p goods

  edges = (n + 1).times.map{[]}
  nbrs = (n + 1).times.map{[]}

  m = gets.to_i
  m.times do
    s, t, d = gets.split.map(&:to_i)
    edges[s][t] = edges[t][s] = d
    nbrs[s] << t
    nbrs[t] << s
  end
  #p edges
  #p nbrs

  if ng
    puts 'impossible'
    next
  end

  pbits = (n + 1).times.map{0}
  allbits = (1 << q) - 1
  sumval = 0

  for i in (0...q)
    name = goods[i]
    min_val = mkts[name].map{|m| m[1]}.min
    sumval += min_val
    for mkt in mkts[name]
      k, val = mkt
      if val == min_val
        pbits[k] |= (1 << i)
      end
    end
  end
  #p pbits
  #p allbits

  dists = (n + 1).times.map{(allbits + 1).times.map{MAX_INT}}
  dists[0][0] = 0

  q = [[0, 0]]

  while ! q.empty?
    u = q.min{|a, b| dists[a[0]][a[1]] <=> dists[b[0]][b[1]]}
    q.delete(u)

    um, ubits = u
    break if um == 0 && ubits == allbits

    ud = dists[um][ubits]

    for vm in nbrs[um]
      vbits = ubits | pbits[vm]
      vd = dists[vm][vbits]
      nd = ud + edges[um][vm]
      if vd > nd
        dists[vm][vbits] = nd
        if vd >= MAX_INT
          q << [vm, vbits]
        end
      end
    end
  end

  puts [sumval, dists[0][allbits]].join(' ')
end