const MaxN=2000;

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 i, j, k, l:integer;

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