import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._
  import scala.math._
  solve
  def solve:Unit = {
    val n = readLine.trim.toInt
    if (n == 0) return
    val stores = Array.tabulate(n) { _ ⇒
      val goods = readLine.trim.split(' ')
      (1 until goods.size by 2).map { it ⇒ (goods(it), goods(it + 1).toInt) }.toMap
    }
    val q = readLine.trim.toInt
    val needs = Array.tabulate(q) { _ ⇒ readLine.trim }
    val storesWithCanBuy = Array.tabulate(n + 1) { _ ⇒ 0 }
    var sum: Option[Int] = Some(0)
    for ((name, i) ← needs.zipWithIndex) {
      val prices = stores.zipWithIndex.flatMap { case (m, id) ⇒ m.get(name).map{(_, id)}}
      if (sum.isEmpty || prices.length == 0) {
        sum = None
      } else {
        val min = prices.map(_._1).min
        sum = sum.map {
          _ + min
        }
        for (s ← prices.withFilter(_._1 == min).map(_._2)) {
          storesWithCanBuy(s + 1) += 1 << i
        }
      }
    }
    val minRoute = Array.tabulate(n + 1) { f ⇒ Array.tabulate(n + 1) { t ⇒ if (f != t) Int.MaxValue else 0: Int } }
    for (_ ← 0 until readLine.trim.toInt) {
      val Array(s, t, d) = readLine.trim.split(' ').map(_.toInt)
      minRoute(s)(t) = d
      minRoute(t)(s) = d
    }
    for {k ← 0 to n
         i ← 0 to n
         j ← 0 to n} {
      if (minRoute(i)(j) - minRoute(i)(k) > minRoute(k)(j)) {
        minRoute(i)(j) = minRoute(i)(k) + minRoute(k)(j)
      }
    }
    val minMemo = Array.tabulate(n + 1) { _ ⇒ Array.tabulate(1 << q) { _ ⇒ Int.MaxValue } }.also(a ⇒ a(0)(0) = 0)
    val queue = mutable.PriorityQueue()(Ordering.by[(Int, Int, Int), Int](_._3).reverse).also(a ⇒ a.enqueue((0, 0, 0)))
    while (queue.nonEmpty) {
      val (current, goods, distance) = queue.dequeue()
      if (minMemo(current)(goods) == distance) {
        for (i ← 0 to n) {
          if (minMemo(i)(goods | storesWithCanBuy(i)) - distance > minRoute(i)(current)) {
            minMemo(i)(goods | storesWithCanBuy(i)) = minRoute(i)(current) + distance
            queue.enqueue((i, goods | storesWithCanBuy(i), minMemo(i)(goods | storesWithCanBuy(i))))
          }
        }
      }
    }
    println(
      sum match {
        case Some(s) ⇒ s"$s ${ minMemo(0).last }"
        case None ⇒ "impossible"
      }
    )
    solve
  }
  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
