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;

function UpStr (s: string): string;
var i: integer; c: char;
begin
   for i := 1 to length (s) do
   begin
      c := s [i];
        if (c >= 'a') and (c <= 'z') then s [i] := chr (ord(c)-ord('a')+ord('A'));
   end;
   UpStr := s
end;



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 := upstr(ans.readstring);
  pa := 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: ');
    if not a[i][k] then
      quit(_wa, 'letter no  (' + name[i] + ')' +
                ' is not on cube no  (' + cubes[k] + ')');
    if u[k] then
      quit(_wa, 'cube is already used');
    u[k] := true;
  end;

  quit(_ok, '');
end.
