{
    ACM ICPC 2003-2004 NEERC

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

{$APPTYPE CONSOLE}

const
    inFile = 'king.in';
    outFile = 'king.out';

    maxN = 2000;

var
    n : integer;

    dx : array[1..maxN] of integer;
    dy : array[1..maxN] of integer;
    exy : array[1..maxN,1..maxN] of integer;
    eyx : array[1..maxN,1..maxN] of integer;
    pxy : array[1..maxN] of integer;
    pyx : array[1..maxN] of integer;

    curtime : integer;
    tlist : array[1..maxN*2] of integer;
    visited : array[-maxN..maxN] of boolean;
    cid : array[-maxN..maxN] of integer;

procedure DFSForward(k : integer);
var
    i, j : integer;

begin
    if not visited[k] then begin
        visited[k] := true;

        if k > 0 then begin
            for i := 1 to dx[k] do begin
                j := exy[k,i];
                if j <> pxy[k] then
                    DFSForward(-j);
            end;
        end else begin
            DFSForward(pyx[-k]);
        end;

        tlist[curtime] := k;
        inc(curtime);
    end;
end;

procedure DFSBackward(k, id : integer);
var
    i, j : integer;

begin
    if not visited[k] then begin
        visited[k] := true;
        cid[k] := id;

        if k < 0 then begin
            for i := 1 to dy[-k] do begin
                j := eyx[-k,i];
                if pxy[j] <> -k then
                    DFSBackward(j, id);
            end;
        end else begin
            DFSBackward(-pxy[k], id);
        end;
    end;
end;

procedure Solve;
var
    i : integer;

begin
    curtime := 1;
    fillchar(visited, sizeof(visited), false);
    for i := 1 to n do begin
        DFSForward(i);
        DFSForward(-i);
    end;

    fillchar(visited, sizeof(visited), false);
    for i := 2 * n downto 1 do
        DFSBackward(tlist[i], i);
end;

procedure ReadData;
var
    i, j : integer;

begin
    read(n);
    if (n < 1) or (n > maxN) then
        RunError(239);
        
    fillchar(dy, sizeof(dy), 0);
    for i := 1 to n do begin
        read(dx[i]);
        if (dx[i] < 0) or (dx[i] > n) then
            RunError(239);
        for j := 1 to dx[i] do begin
            read(exy[i,j]);
            if (exy[i,j] < 1) or (exy[i,j] > n) then
                RunError(239);
            inc(dy[exy[i,j]]);
            eyx[exy[i,j],dy[exy[i,j]]] := i;
        end;
    end;

    for i := 1 to n do begin
        read(pxy[i]);
        if (pxy[i] < 1) or (pxy[i] > n) then
            RunError(239);
        pyx[pxy[i]] := i;
    end;

    close(input);
end;

procedure WriteData;
var
    i, j, cnt : integer;

begin
    for i := 1 to n do begin
        cnt := 0;
        for j := 1 to dx[i] do
            if (exy[i,j] = pxy[i]) or (cid[i] = cid[-exy[i,j]]) then
                inc(cnt);
        write(cnt);
        for j := 1 to dx[i] do
            if (exy[i,j] = pxy[i]) or (cid[i] = cid[-exy[i,j]]) then
                write(' ', exy[i,j]);
        writeln;
    end;
    close(output);
end;

begin
    ReadData;
    Solve;
    WriteData;
end.
