import scala.io.StdIn.{readInt,readLine}

object Main {
  def main(args:Array[String]) = {
    var n = 0
    while({n=readInt;n} != 0) {
      val s = for(i<-1 to n) yield readLine.split(" ")
      val m = readLine.trim
      val r = s.flatMap{ case a:Array[String] => a}
               .filter(e=>e(0) == m(0))
               .toSet
               .toArray
               .sorted.take(5)
               .mkString(" ")
      println(if(r=="") "NA" else r)
    }
  }
}