import scala.io.Source._
import scala.io.StdIn._

object Main {
  def main(args: Array[String]): Unit = {
    while (true) {
      val n = readInt
      if (n == 0) return

      var map = scala.collection.mutable.HashMap[Char, Char]()
      for (i <- 0 until n) {
        val s = readLine.split(' ').map(x => x.head)
        map += (s.head -> s(1))
      }

      val nn = readInt
      for (i <- 0 until nn) {
        val x = readLine.head
        print(map.get(x).getOrElse(x))
      }
      println
    }
  }
}