import scala.io.StdIn.readLine
import scala.collection.mutable.ArrayBuffer

object Main extends App {
  var n = 0
  while( {n=readLine.trim.toInt;n} != 0 ) {
    val b = new ArrayBuffer[String]
    var convert = for(i<-1 to n) yield readLine.trim.split(" ").toList
    val m = readLine.trim.toInt
    for(i<-1 to m) {
      val s = readLine.trim
      b += convert.find{ _(0) == s }.map{ _(1) }.getOrElse(s)
    }
    println(b.mkString)
  }
}