import scala.io.StdIn._

object Main {
  def returnConversionMap(n: Int, m: Map[String, String] = Map()): Map[String, String] = {
    if (n > 0) {
      val Array(a, b) = readLine().split(" ")
      returnConversionMap(n - 1, m + (a -> b))
    } else m
  }

  def dataConversion(n: Int, m: Map[String, String], converted: String = ""): String = {
    if (n > 0) {
      val originalText = readLine()
      val findConversion = for (k <- m.keys if k == originalText) yield k
      if (findConversion == Set()) dataConversion(n - 1, m, converted + originalText)
      else dataConversion(n - 1, m - originalText, converted + m(originalText))
    } else converted
  }

  def results(n: Int, l: Vector[String] = Vector()): Vector[String] = {
    if (n == 0) l
    else {
      val conversionMap = returnConversionMap(n)
      val result: String = dataConversion(readInt(), conversionMap)
      results(readInt(), l :+ result)
    }
  }

  def main(args: Array[String]): Unit = {
    results(readInt()).foreach(println)
  }
}