{very stupid exponential solution}
const MaxN=2000;

var p:array [1..MaxN] of integer;
    g, h:array [1..MaxN, 1..MaxN] of boolean;
    f:array [1..MaxN] of boolean;
    n:integer;

procedure rec (k:integer);
var i:integer;
begin
  if k>n then begin
    for i:=1 to n do h[i, p[i]]:=true;
    exit;
  end;
  for i:=1 to n do
    if not f[i] and g[k, i] then begin
      f[i]:=true; p[k]:=i;
      rec (k+1);
      f[i]:=false;
    end;
end;

var a, k:array [1..MaxN] of integer;
    i, j, tmp:integer;

begin
  assert (not seekeoln); read (n); assert ((n>=1) and (n<=MaxN)); 
  assert (seekeoln); readln;
  for i:=1 to n do begin
    assert (not seekeoln); read (k[i]); assert ((k[i]>=1) and (k[i]<=n));
    for j:=1 to k[i] do begin
      assert (not seekeoln); read (tmp); 
      assert ((tmp>=1) and (tmp<=n) and not g[i, tmp]);
      g[i, tmp]:=true;
    end;
    assert (seekeoln); readln;
  end;
  for i:=1 to n do begin
    assert (not seekeoln); read (a[i]); assert ((a[i]>=1) and (a[i]<=n));
    assert (not f[a[i]]); f[a[i]]:=true;
  end;
  assert (seekeof);
  fillchar (f, sizeof (f), 0);
  rec (1);
  for i:=1 to n do begin
    tmp:=0;
    for j:=1 to n do if h[i, j] then inc (tmp);
    write (tmp);
    for j:=1 to n do if h[i, j] then write (' ', j);
    writeln;
  end;
end.