uses
    testlib, SysUtils;

const
  MAX_T = 100000;

var
  n, p, i, j, pa, ja, tt: integer;
  l, s, d, t, zz, v: array[1..100] of integer;
  a: array[1..100, 0..10] of integer;
  z: array[1..100] of boolean;
      

begin
    n := inf.readlongint;
    for i := 1 to n do begin
        l[i] := inf.readlongint;
        for j := 0 to l[i] - 1 do begin
            a[i, j] := inf.readlongint;
        end;
    end;

    p := inf.readlongint;
    for i := 1 to p do begin
        s[i] := inf.readlongint;
        d[i] := inf.readlongint;
        t[i] := inf.readlongint;
    end;

    pa := ouf.readlongint;
    ja := ans.readlongint;

    if (pa < 0) then quit(_wa, 'answer < 0');
    for i := 1 to pa do begin
        j := ouf.readlongint;
        if (j < 1) or (j > p) then quit(_wa, 'invalid packet number: ' + inttostr(j));
        if (z[j]) then quit(_wa, 'duplicate packet number: ' + inttostr(j));
        z[j] := true;
    end;

    for i := 1 to p do v[i] := -1;

    for tt := 0 to MAX_T do begin
        for i := 1 to p do
            if z[i] and (t[i] = tt) then v[i] := s[i];
        
        for i := 1 to n do zz[i] := -1;
        for i := 1 to p do 
            if (v[i] >= 0) then begin
                if (zz[v[i]] >= 0) then
                    quit(_wa, format('packets %d and %d are in node %d at time %d', [i, zz[v[i]], v[i], tt]));

                zz[v[i]] := i;
            end;

        for i := 1 to p do
            if v[i] >= 0 then begin
                if d[i] = v[i] then
                    v[i] := -1
                else 
                    v[i] := a[v[i]][tt mod l[v[i]]];
            end;            
    end;

    for i := 1 to p do
        if v[i] >= 0 then quit(_wa, format('packet %d cannot reach destination', [i]));

    if (pa < ja) then quit(_wa, format('not optimal solution: ja=%d, pa=%d', [ja, pa]));
    if (pa > ja) then quit(_fail, format('not optimal jury solution: ja=%d, pa=%d', [ja, pa]));

    quit(_ok, '');
end.