import std.algorithm : map, group, filter, sort;
import std.array : array;
import std.conv : to;
import std.range : take, empty, iota;
import std.stdio : readln, writeln;
import std.string : split, chomp, join;

void main()
{
  for(int n; (n = readln.chomp.to!int) != 0; ){
    string[] words;

    foreach(i; iota(n))
      words ~= readln.chomp.split;

    words.sort;
    auto wc = group(words);
    auto c = readln[0];

    auto wcf = wc.filter!(a => a[0][0] == c).array;
    sort!"a[1] == b[1] ? a[0] < b[0] : a[1] > b[1]"(wcf);

    string output = wcf.map!"a[0]".take(5).array.join(" ");

    writeln(output.length ? output : "NA");
  }
}