{$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;
    fg, tg: array [1..max_n, 1..max_n] of longint;
    cfg, ctg: array [1..max_n] of longint;
    fm, tm: 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 cfg[i] do begin
            if (u[-fg[i][j]] = 0) then begin
                dfs1(-fg[i][j]);
            end;
        end;
    end else begin
        if u[tm[-i]] = 0 then begin
            dfs1(tm[-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 ctg[-i] do begin
            if (u[tg[-i][j]] = 0) then begin
                dfs2(tg[-i][j], k);
            end;
        end;
    end else begin
        if u[-fm[i]] = 0 then begin
            dfs2(-fm[i], k);
        end;
    end;
    inc(m);
    s[m] := i;
end;

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(cfg[i]);
            for j := 1 to cfg[i] do begin
                read(fg[i][j]);
                inc(ctg[fg[i][j]]);
                tg[fg[i][j]][ctg[fg[i][j]]] := i;
                q[i][fg[i][j]] := true;
            end;
        end;

        for i := 1 to n do begin
            read(fm[i]);
            q[i][fm[i]] := false;
            tm[fm[i]] := i;
            for j := 1 to cfg[i] do begin
                if fg[i][j] = fm[i] then begin
                    fg[i][j] := 0;
                end;
            end;
            for j := 1 to ctg[fm[i]] do begin
                if tg[fm[i]][j] = i then begin
                    tg[fm[i]][j] := 0;
                end;
            end;
        end;

        fillchar(u, sizeof(u), 0);
        u[0] := 2 * 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] := 2 * 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 := 1;
            for j := 1 to cfg[i] do begin
                if (u[i] = u[-fg[i][j]]) and (fm[i] <> fg[i][j]) then begin
                    inc(k);
                end;
            end;
            write(k, ' ', fm[i]);
            for j := 1 to cfg[i] do begin
                if (u[i] = u[-fg[i][j]]) and (fm[i] <> fg[i][j]) then begin
                    write(' ', fg[i][j]);
                end;
            end;
            {
            for j := 1 to n do begin
                if q[i][j] and (u[i] = u[-j]) or (i = tm[j])  then begin
                    write(' ', j);
                end;
            end;
            }
            writeln;
        end;
        writeln;
    end;

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