{$Q+,R+,S+,o-}
{$apptype console}
const
    max_n = 2000;
var
    i, j, k, n: longint;
    u: array [-max_n..max_n] of longint;
    s: array [1..4 * max_n] of longint;
    q: array [1..max_n, 1..max_n] of boolean;
    m: longint;
    z: array [1..max_n] of longint;

procedure dfs1(i: longint);
var
    j: longint;
begin
    u[i] := 1;
    if i > 0 then begin
        for j := 1 to n do begin
            if q[i][j] and (u[-j] = 0) then begin
                dfs1(-j);
            end;
        end;
    end else begin
        if u[z[-i]] = 0 then begin
            dfs1(z[-i]);
        end;
    end;
    inc(m);
    s[m] := i;
end;

procedure dfs2(i, k: longint);
var
    j: longint;
begin
    u[i] := k;
    if i < 0 then begin
        for j := 1 to n do begin
            if q[j][-i] and (u[j] = 0) then begin
                dfs2(j, k);
            end;
        end;
    end else begin
        for j := 1 to n do begin
            if (i = z[j]) and (u[-j] = 0) then begin
                dfs2(-j, k);
            end;
        end;
    end;
    inc(m);
    s[m] := i;
end;

var
    b: longint;
begin

    while not seekeof do begin
        fillchar(u, sizeof(u), 0);
        fillchar(s, sizeof(s), 0);
        fillchar(q, sizeof(q), 0);

        read(n);
        for i := 1 to n do begin
            read(k);
            for j := 1 to k do begin
                read(b);
                q[i][b] := true;
            end;
        end;

        for i := 1 to n do begin
            read(b);
            q[i][b] := false;
            z[b] := i;
        end;

        fillchar(u, sizeof(u), 0);
        u[0] := n + 1;
        m := 0;
        for i := 1 to n do begin
            if (u[i] = 0)then begin
                dfs1(i);
            end;
            if (u[-i] = 0)then begin
                dfs1(-i);
            end;
        end;

        k := 0;
        fillchar(u, sizeof(u), 0);
        u[0] := n + 1;
        for i := 2 * n downto 1 do begin
            if (u[s[i]] = 0)then begin
                inc(k);
                dfs2(s[i], k);
            end;
        end;

        for i := 1 to n do begin
            k := 0;
            for j := 1 to n do begin
                if q[i][j] and (u[i] = u[-j]) or (i = z[j]) then begin
                    inc(k);
                end;
            end;
            write(k);
            for j := 1 to n do begin
                if q[i][j] and (u[i] = u[-j]) or (i = z[j])  then begin
                    write(' ', j);
                end;
            end;
            writeln;
        end;
        writeln;
    end;

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