program checker;

const
  MaxL = 10000;

var
  sol : Integer;
  correct : Boolean;
  i, o : array[0..MaxL-1] of Char;
  s : array[0..MaxL, 0..MaxL] of Boolean;


  procedure ReadInput;
  var
    f : Text;
    n, k, j, m, l : Integer;
    ch : Char;
  begin
    Assign(f, ParamStr(1));
    Reset(f);

    Readln(f, n);

    for m := 0 to n - 1 do
    begin
      Read(f, l, ch);
      for j := 0 to l - 1 do
        Read(f, i[j]);
      Readln(f);

      s[0, 0] := true;

      for j := 1 to l do
        s[j, 0] := false;

      for k := 1 to sol do
        s[0, k] := s[0, k - 1] and (i[k - 1] = '*');

      for j := 1 to sol do
        for k := 1 to l do
          case i[k - 1] of
            '*' : s[j, k] := s[j, k - 1] or s[j - 1, k];
            '?' : s[j, k] := s[j - 1, k - 1];
          else
            s[j, k] := (i[k - 1] = o[j - 1]) and s[j - 1, k - 1];
          end;

      correct := correct and s[sol, l];
    end;

    Close(f);
  end;


  procedure ReadSolution;
  var
    f : Text;
  begin
    Assign(f, ParamStr(3));
    Reset(f);
    Readln(f);
    Readln(f, sol);
    Close(f);
  end;


  procedure ReadOutput;
  var
    f : Text;
    t, j : Integer;
  begin
    Assign(f, ParamStr(2));
    Reset(f);

    Readln(f);     // # sef, nn
    Readln(f, t);
    if t <= sol then
    begin
      sol := t;
      for j := 0 to t - 1 do
        Read(f, o[j]);
    end
    else
      correct := false;

    Close(f);
  end;


begin
  correct := true;
  ReadSolution;
  ReadOutput;
  if correct then ReadInput;

  if correct then
    Writeln(10)
  else
    Writeln(0);
end.