{
    ACM ICPC 2003-2004 NEERC

    Problem:    King's Quest
    Author:     Andrew Stankevich
    Solution:   Andrew Stankevich
}

{$DEFINE allowmultitest}
{$O-,Q+,R+}
{$APPTYPE CONSOLE}

program kings_quest;

uses
    sysutils;

const
    maxn = 2000;
    maxsumk = 200000;

var
    u: array [1..maxn] of boolean;
    v: array [1..2, 1..maxn] of longint;
    a: array [1..maxn, 1..maxn] of boolean;
    i, j, k: longint;
    n, m, nn: longint;
    stack: array [1..2 * maxn] of longint;
    px, py: array [1..maxn] of longint;
    sumk: longint;

procedure dfs(x: longint);
var
    i: longint;
begin   
    u[x] := true;

    for i := 1 to n do  
        if not u[i] and a[py[x]][i] then
        begin
            dfs(i);
        end;

    inc(m);
    stack[m] := -py[x];
    inc(m);
    stack[m] := x;
end;

procedure dfs2y(x, nk: longint); forward;

procedure dfs2x(x, nk: longint);
begin
    v[1][x] := nk;
    if v[2][px[x]] = 0 then
        dfs2y(px[x], nk);
end;

procedure dfs2y(x, nk: longint);
var
    i: longint;
begin
    v[2][x] := nk;
    for i := 1 to n do
        if a[i][x] and (v[1][i] = 0) and (px[i] <> x) then
            dfs2x(i, nk);
end;

begin
    if eoln then runerror(239);
    read(n);
    if (n < 1) or (n > 2000) then
        runerror(239);
    if not eoln then runerror(239);
    readln;

    fillchar(a, sizeof(a), 0);
    sumk := 0;
    for i := 1 to n do
    begin
        if eof or eoln then runerror(239);

        read(m);
        if (m < 0) then runerror(239);
        sumk := sumk + m;

        for j := 1 to m do
        begin
            if eof or eoln then runerror(239);

            read(k);
            if (k < 1) or (k > n) then runerror(239);
            if a[i][k] then runerror(239);

            a[i][k] := true;
        end;
        if not eoln then runerror(239);
        readln;
    end;
    if sumk > maxsumk then
        runerror(239);

    fillchar(py, sizeof(py), 0);
    for i := 1 to n do
    begin
        if eof or eoln then runerror(239);

        read(px[i]);

        if py[px[i]] <> 0 then runerror(239);
        py[px[i]] := i;
    end;

    if eof or not eoln then runerror(239);
    readln;
    {$ifndef allowmultitest}
    if not eof then runerror(239);
    {$endif}

    m := 0;
    fillchar(u, sizeof(u), 0);
    for i := 1 to n do
        if not u[i] then
            dfs(i);

    fillchar(v, sizeof(v), 0);
    nn := 0;
    for i := m downto 1 do
    begin
        if (stack[i] > 0)  then
        begin
            if v[2][stack[i]] = 0 then
            begin
                inc(nn);
                dfs2y(stack[i], nn)
            end
        end else begin
            if v[1][-stack[i]] = 0 then
            begin
                inc(nn);
                dfs2x(-stack[i], nn);
            end;
        end;
    end;

    for i := 1 to n do
    begin
        m := 0;
        for j := 1 to n do
            if a[i][j] and ((j = px[i]) or (v[2][j] = v[1][i])) then
                inc(m);
        write(m);
        for j := 1 to n do
            if a[i][j] and ((j = px[i]) or (v[2][j] = v[1][i])) then
                write(' ', j);
        writeln;
    end;

    close(input);
    close(output);
end.