{fastest solution - using variable length arrays}
{$o+,q-,r-}
{$apptype console}
uses sysutils;

const MaxN=2000;
      MaxK=200000;

var l, lt:array [1..MaxN*2] of array of integer;
    g:array [1..MaxN, 1..MaxN] 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, cur:integer;
begin
  f[v]:=true;
  for i:=0 to high (l[v]) do begin
    cur:=l[v, i]; if not f[cur] then poisk (cur);
  end;
  inc (tc); t[tc]:=v;
end;


procedure ksiop (v:integer);
var i, cur:integer;
begin
  c[v]:=cc;
  for i:=0 to high (lt[v]) do begin
    cur:=lt[v, i]; if c[cur]=0 then ksiop (cur);
  end;
end;

var i, j, k, z:integer;
    ltc:array [1..MaxN] of integer;

begin
  read (n); 
  for i:=1 to n do begin
    read (k); setlength (l[i], k); for j:=0 to k-1 do begin
      read (z); l[i, j]:=n+z; g[i, z]:=true;
      inc (ltc[z]);
    end;
  end;
  for i:=1 to n do begin
    read (z); setlength (l[n+z], 1); l[n+z, 0]:=i;
    setlength (lt[i], 1); lt[i, 0]:=n+z; 
  end;
  for i:=1 to n do begin setlength (lt[n+i], ltc[i]); ltc[i]:=0 end;
  for i:=1 to n do
    for j:=0 to high (l[i]) do begin
      k:=l[i, j]; lt[k, ltc[k-n]]:=i; inc (ltc[k-n]);
    end;
  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
    k:=0;
    for j:=1 to n do if g[i, j] and (c[i]=c[n+j]) then inc (k);
    write (k);
    for j:=1 to n do if g[i, j] and (c[i]=c[n+j]) then write (' ', j);
    writeln;
  end;
end.