loop do
    n = gets.to_i
    break if n == 0
    s = (1..n).map { gets }.join
    k = gets.chomp
    a = s.scan(/\b#{k}\w*/)
    if a.empty?
        puts 'NA'
    else
        h = Hash.new(0)
        a.each {|w| h[w] += 1}
        puts h.sort_by {|w, c| [-c, w]}.map(&:first)[0..4] * ' '
    end
end
