const MaxN=2000;
      MaxK=200000;

var g:array [1..MaxN*2, 1..MaxN*2] of boolean;
    f:array [1..MaxN*2] of boolean;
    t, c:array [1..MaxN*2] of integer;
    n, tc, cc:integer;

procedure poisk (v:integer);
var i:integer;
begin
  f[v]:=true;
  for i:=1 to n+n do
    if not f[i] and g[v, i] then poisk (i);
  inc (tc); t[tc]:=v;
end;


procedure ksiop (v:integer);
var i:integer;
begin
  c[v]:=cc;
  for i:=1 to n+n do
    if (c[i]=0) and g[i, v] then ksiop (i);
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, n+tmp]);
      g[i, n+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;
    assert (g[i, n+a[i]]); g[i, n+a[i]]:=false; g[n+a[i], i]:=true;
  end;
  assert (seekeof);
  tmp:=0; for i:=1 to n do inc (tmp, k[i]); assert ((tmp>=n) and (tmp<=MaxK));
  fillchar (f, sizeof (f), 0); tc:=0;
  for i:=1 to n+n do if not f[i] then poisk (i);
  fillchar (c, sizeof (c), 0); cc:=0;
  for i:=n+n downto 1 do if c[t[i]]=0 then begin
    inc (cc); ksiop (t[i])
  end;
  for i:=1 to n do begin
    tmp:=0;
    for j:=1 to n do if (g[i, n+j] and (c[i]=c[n+j])) or (j=a[i]) then inc (tmp);
    write (tmp);
    for j:=1 to n do if (g[i, n+j] and (c[i]=c[n+j])) or (j=a[i]) then write (' ', j);
    writeln;
  end;
end.