
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
import std.array;
import std.conv;
import std.typecons;

alias Tuple!(int, "count", string, "name") Word;

Word[] search_words(string[] lines, char prefix) {
  string[] res;
  Word[string] rank;
  foreach(line; lines) {
    auto words = line.split(" ");
    foreach(word; words) {
      if (word[0] == prefix) {
        if (word in rank) rank[word].count++;
        else {
          rank[word] = Word(1, word);
        }
      }
    }
  }
  Word[] words = rank.values;
  return sort!((a, b) {
      if (a.count != b.count) return a.count > b.count;
      return a.name < b.name;
    })(words).take(5).array;
}

void main() {
  while(true) {
    int times = readln.chomp.to!(int);
    if (times == 0) break;
    string[] lines;
    foreach(t; 0..times) lines ~= readln.chomp;
    char prefix = readln.chomp[0];
    Word[] words = search_words(lines, prefix);
    if (words == []) writeln("NA");
    foreach(word; words) {
      write(word.name, " ");
    }
    writeln;
  }
}