#include <bits/stdc++.h>
#include "testlib.h"

using namespace std;

static constexpr string_view NO = "NO";

bool check_ans_format(const string&s, int E) {
  return s == NO || std::ranges::all_of(s, [E](char ch) { return 'a' <= ch && ch < 'a' + E; });
}

int main(int argc, char *argv[]) {
  registerTestlibCmd(argc, argv);

  int n = inf.readInt();
  int E = inf.readInt(2, 26, "E");
  inf.readEoln();
  string allowed_chars = string("[a-") + static_cast<char>('a' + E - 1) + "]";
  vector<string> keys(n);
  for (auto& key : keys) {
    key = inf.readString(allowed_chars + "{1,10}");
  }
  string ja = ans.readString();
  string pa = ouf.readString("(NO|" + allowed_chars + "+)", "participantAns");
  if (!check_ans_format(ja, E)) {
    quitf(_fail, "Jury answer has bad format: %s", ja.c_str());
  }
  if (!check_ans_format(pa, E)) {
    quitf(_pe, "bad format, need \"%s\", or %s+, got: %s", NO.data(), allowed_chars.c_str(), pa.c_str());
  }

  if (pa == NO) {
    if (ja == NO) {
      quitf(_ok, "ok, can print any string");
    } else {
      quitf(_wa, "Participant says any string can be printed, but %s can't", ja.c_str());
    }
  }

  if (ja != NO && ja.length() < pa.length()) {
    quitf(_wa, "Jury has shorter answer");
  }

  vector<size_t> pos(n, 0);
  string ans_ids;
  bool pressed = true;
  for (size_t ci = 0; ci < pa.length() && pressed; ci++) {
    char ch = pa[ci];
    pressed = false;
    for (int i = 0; !pressed && i < n; i++) {
      if (keys[i][pos[i] % keys[i].length()] == ch) {
        ans_ids += to_string(i + 1) + ",";
        pos[i]++;
        pressed = true;
      }
    }
  }

  if (pressed) {
    if (!ans_ids.empty()) {
      ans_ids.pop_back(); // remove last ","
    }
    quitf(_wa, R"(pa="%s" can be typed by pressing next keys: "%s")", pa.c_str(), ans_ids.c_str());
  } else {
    if (ja == NO || pa.length() < ja.length()) {
      quitf(_fail, "len(pa) < len(ja), but checker can't prove that pa=\"%s\" can be typed", pa.c_str());
    } else {
      quitf(_ok, "Got unprintable string of length %zu", pa.length());
    }
  }
}
