import Control.Monad

main :: IO ()
main = do
  n <- readLn
  if n == 0
    then return ()
    else do
      ls <- replicateM n getLine
      k <- getLine
      let ls' = filter (\x -> [head x] == k) (concatMap words ls)
      putStrLn $ solve ls'
      main

solve :: [String] -> String
solve ls = format (take5 (foldl getName [] (sort (countName ls))))
  where
    getName :: [String] -> (String,Int) -> [String]
    getName acc (name,i) = name:acc

    countName :: [String] -> [(String,Int)]
    countName [] = []
    countName (x:xs) = (x,(count x (x:xs))):(countName (filter (\y -> x /= y) xs))
      where
        count :: String -> [String] -> Int
        count _ [] = 0
        count a (b:bs)
          | a == b = 1 + (count a bs)
          | otherwise = count a bs

    sort :: [(String,Int)] -> [(String,Int)]
    sort [] = []
    sort ((a,b):r) = (sort lower) ++ [(a,b)] ++ (sort higher)
      where
        lower = filter (\(c,d) -> (d < b) || ((d == b) && (c > a))) r
        higher = filter (\(c,d) -> (d > b) || ((b == d) && (c <= a))) r
        
    format :: [String] -> String
    format [x] = x
    format (x:xs) = x ++ " " ++ (format xs)

    take5 :: [String] -> [String]
    take5 [] = ["NA"]
    take5 xs = take5' xs 5
      where
        take5' :: [String] -> Int -> [String]
        take5' [] _ = []
        take5' _ 0 = []
        take5' (x:xs) n = x:(take5' xs (n-1))