program check_b;

uses
  testlib, symbols;

var
  l, r, i, j, k, m, n: longint;
  a: array [0..100, 0..100] of boolean;
  u: array [0..100] of boolean;
  cubes: array [0..100] of string[6];
  ja, pa: string;
  s, name: string;

begin
  n := inf.readlongint;
  inf.seekeof;
  name := inf.readstring;
  m := length(name);
  for i := 1 to n do
  begin
    s := inf.readstring;
    cubes[i] := s;
    for j := 1 to m do
      for k := 1 to 6 do
        if s[k] = name[j] then
          a[j][i] := true;
  end;

  ja := compress(upstr(ans.readstring));
  pa := compress(upstr(ouf.readstring));
  if ja <> pa then
    quit(_wa, 'wrong: expected ' + ja + ', found ' + pa);

  if ja = 'NO' then quit(_ok, '');

  for i := 1 to m do
  begin
    k := ouf.readlongint;
    if (k < 1) or (k > n) then quit(_wa, 'invalid number: ' + str(k, 0));
    if not a[i][k] then
      quit(_wa, 'letter no ' + str(i, 0) + ' (' + name[i] + ')' +
                ' is not on cube no ' + str(k, 0) + ' (' + cubes[k] + ')');
    if u[k] then
      quit(_wa, 'cube ' + str(k, 0) + ' is already used');
    u[k] := true;
  end;

  quit(_ok, '');
end.