object Main extends App {
    val in = io.Source.stdin.getLines
    for (n <- in if n != "0") {
        val dic = in.take(n.toInt).map{r => val Array(k, v) = r.split(" "); (k, v)}.toMap
        println(in.take(in.next.toInt).map(k => dic.get(k) match {
            case Some(v) => v
            case _ => k
        }).mkString)
    }
}