
type
  TSoleCond = record
    table, col: Byte;
    value: char;
  end;
  TPairCond = record
    table1, col1, ncol1: Byte;
    table2, col2, ncol2: Byte;
  end;
  TRow = String;
  TTable = record
    rc: Integer;
    r: array [1..250] of TRow;
  end;
  PTable = ^TTable;

var
  tableCount, soleCondCount, pairCondCount: Integer;
  soleCond: array [1..50] of TSoleCond;
  pairCond: array [1..50] of TPairCond;
  tableCols: array [0..26] of Integer;
  tt: array [1..2] of PTable;
  curT: Integer;

function CheckSoleRow(tn: Integer; row: TRow): Boolean;
var
  i: Integer;
begin
  CheckSoleRow := true;
  for i := 1 to soleCondCount do with soleCond[i] do
    if (table = tn) and (row[col] <> value) then begin
      CheckSoleRow := false;
      break;
    end;
end;

function CheckPairs(tn: Integer; row: TRow): Boolean;
var
  i: Integer;
begin
  CheckPairs := true;
  for i := 1 to pairCondCount do with pairCond[i] do begin
    if (table1 > tn) or (table2 > tn) then continue;
    if row[tableCols[table1 - 1] + col1] <> row[tableCols[table2 - 1] + col2]
    then begin
      CheckPairs := false;
      break;
    end;
  end;
end;

function Code(ch, base: Char): Byte;
begin
  Code := ord(ch) - ord(base) + 1;
end;

var
  f: Text;
  i, j, k, D, CN, RN: Integer;
  S, oldRow: String;

begin
  Assign(f, 'input.txt');

  Reset(f); {first pass}
  Readln(f, tableCount, D);
  tableCols[0] := 0;
  for i := 1 to tableCount do begin
    Readln(f, RN, CN);
    tableCols[i] := tableCols[i - 1] + CN;
    for j := 1 to RN do Readln(f, S); {skip table}
  end;
  soleCondCount := 0; pairCondCount := 0;
  for i := 1 to D do begin
    Readln(f, S);
    if Length(S) = 4 then begin
      Inc(soleCondCount);
      with soleCond[soleCondCount] do begin
        table := Code(S[1], 'a'); col := Code(S[2], '1');
        value := S[4];
      end;
    end else begin
      Inc(pairCondCount);
      with pairCond[pairCondCount] do begin
        table1 := Code(S[1], 'a'); col1 := Code(S[2], '1');
        table2 := Code(S[4], 'a'); col2 := Code(S[5], '1');
      end;
    end;
  end;
  Close(f);

  Reset(f);
  for i := Low(tt) to High(tt) do New(tt[i]);
  Readln(RN, CN); {skip T, D}
  Readln(RN, CN); {first table - no join}
  curT := 1; tt[curT]^.rc := 0;
  for j := 1 to RN do with tt[curT]^ do begin
    Readln(f, S);
    if not CheckSoleRow(1, S) then continue;
    Inc(rc); r[rc] := S;
  end;

  for i := 2 to tableCount do with tt[3 - curT]^ do begin
    rc := 0;
    Readln(RN, CN);
    for j := 1 to RN do begin
      Readln(oldRow);
      if not CheckSoleRow(i, oldRow) then continue;
      for k := 1 to tt[curT]^.rc do begin
        S := tt[curT]^.r[k] + oldRow;
        if not CheckPairs(i, S) then continue;
        Inc(rc); r[rc] := S;
      end;
    end;
    curT := 3 - curT;
  end;

  with tt[curT]^ do begin
    for i := 1 to rc - 1 do
      for j := i + 1 to rc do
        if r[i] > r[j] then begin
          S := r[i]; r[i] := r[j]; r[j] := S;
        end;
    for i := 1 to rc do Writeln(r[i]);
  end;
end.
