import scala.annotation.tailrec
object Main extends App {

  import scala.io.StdIn._

  solve
  def solve: Unit = {
    val Array(n, m, p) = readLine.trim.split(' ').map(_.toInt)
    if (n == 0) return
    val units = Array.tabulate(m){_ ⇒
      val name = readLine.trim
      val dimension = UnitDimension(readLine.trim.split(' ').map(_.toInt).toList)
      name → dimension
    }
    val expression = readLine.trim
    implicit val map = Array.tabulate(p){_ ⇒
      val Array(variable, unit) = readLine.trim.split(' ')
      variable → units.find(_._1 == unit).get._2
    }.toMap
    Formula.calDimension(expression) match {
      case Some(dimension) ⇒ units.filter(_._2 == dimension).lastOption match {
        case Some((name, _)) ⇒ println(name)
        case None ⇒ println("undefined")
      }
      case None ⇒ println("error")
    }
    solve
  }
  case class UnitDimension(dimension: List[Int]) extends AnyVal {
    def *(that: UnitDimension): UnitDimension = UnitDimension(dimension.zip(that.dimension).map{case (a, b) ⇒ a + b})
    def /(that: UnitDimension): UnitDimension = UnitDimension(dimension.zip(that.dimension).map{case (a, b) ⇒ a - b})
  }
  object Variable{
    def unapply(arg: List[Char])(implicit map: String => UnitDimension): Option[(UnitDimension, List[Char])] = {
      arg.span(_.isLetter) match {
        case (Nil, _) ⇒ None
        case (string, t) ⇒ Some(map(string.mkString), t)
      }
    }
  }
  object Formula {
    def calDimension(express: String)(implicit map: String ⇒ UnitDimension): Option[UnitDimension] = {
      express.toList match {
        case f Formula Nil ⇒ Some(f)
        case _ ⇒ None
      }
    }
    def unapply(arg: List[Char])(implicit map: String => UnitDimension): Option[(UnitDimension, List[Char])] = {
      arg match {
        case term Term t ⇒ parseAll(t, term)
        case _ ⇒ None
      }
    }
    @tailrec
    private def parseAll(arg: List[Char], dimension: UnitDimension)(implicit map: String => UnitDimension): Option[(UnitDimension, List[Char])] = {
      arg match {
        case o::(term Term t) if o == '+' || o == '-' ⇒ if (term == dimension) parseAll(t, dimension) else None
        case _ ⇒ Some(dimension, arg)
      }
    }
  }
  object Factor{
    def unapply(arg: List[Char])(implicit map: String ⇒ UnitDimension): Option[(UnitDimension, List[Char])] = {
      arg match {
        case variable Variable t ⇒ Some(variable, t)
        case '('::(formula Formula ')'::t) ⇒ Some(formula, t)
        case _ ⇒ None
      }
    }
  }
  object Term {
    def unapply(arg: List[Char])(implicit map: String => UnitDimension): Option[(UnitDimension, List[Char])] = {
      arg match {
        case factor Factor t ⇒ Some(parseAll(t, factor))
        case _ ⇒ None
      }
    }
    @tailrec
    private def parseAll(arg: List[Char], dimension: UnitDimension)(implicit map: String => UnitDimension): (UnitDimension, List[Char]) = {
      arg match{
        case '*'::(factor Factor t) ⇒ parseAll(t, dimension * factor)
        case '/'::(factor Factor t) ⇒ parseAll(t, dimension / factor)
        case _ ⇒ (dimension, arg)
      }
    }
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
