{$o-,q+,r+}
{$APPTYPE console}

uses
    math, testlib, sysutils;

type
    int = longint;

const
    maxn = 200000;

var
    n, m, a, b, pa, pb, i, j, pi: longint;
    jans, pans: string;
    es, et, ea, p, r: array[1 .. maxn] of int;
    
function nextInt(): int;
const
    BEFORE = 1;
    FIRST = 2;
    INNER = 3;
var
    c: char;
    state: int;
begin
    result := 0;
    state := BEFORE;
    while (true) do begin
        if (pi > length(pans)) then begin
            if (state = BEFORE) then begin
                quit(_pe, 'End of file reached while integer expected');
            end else begin
                exit;
            end;
        end;
        c := pans[pi];
        inc(pi);
        if (not (c in [' ', '0' .. '9'])) then begin
            quit(_pe, 'Illegal character in input file.');
        end;
        case state of
            BEFORE: begin
                if (c <> ' ') then begin
                    state := FIRST;
                end else begin
                    continue;
                end;
            end;
            FIRST: begin
                if (c = ' ') then begin
                    exit;
                end;
                if (result = 0) then begin
                    quit(_pe, 'String 0.. that is not an integer number found.');
                end;

                state := INNER;
            end;
            INNER: begin
                if (c = ' ') then begin
                    exit;
                end;
            end;
        end;

        result := 10 * result + ord(c) - ord('0');
        if (result > maxn) then begin
            quit(_wa, 'Too big integer found');
        end;
    end;
end;
function lineEnd(): boolean;
begin
    while (pi <= length(pans)) do begin
        if (pans[pi] <> ' ') then begin
            result := false;
            exit;
        end;
    end;
    result := true;
end;

function get(i: int): int;
begin
    if (p[i] <> i) then
        p[i] := get(p[i]);
    result := p[i];
end;

procedure join(i, j: int);
begin
    i := get(i);
    j := get(j);
    if (r[i] >= r[j]) then begin
        inc(r[i], r[j]);
        p[j] := i;
    end else begin
        join(j, i);
    end;
end;

begin
    jans := ans.ReadString;
    pans := ouf.ReadString;
    if (pans = 'Impossible') then begin
        if (jans = 'no') then begin
            quit(_ok, 'Impossible');
        end else begin
            quit(_wa, 'Jury has found a solution but participant hasn''t');
        end;
    end else begin
        n := inf.ReadInteger;
        m := inf.ReadInteger;
        a := inf.ReadInteger;
        b := inf.ReadInteger;
        for i := 1 to m do begin
            es[i] := inf.ReadInteger;
            et[i] := inf.ReadInteger;
            ea[i] := inf.ReadInteger;
        end;

        for i := 1 to n do begin
            p[i] := i;
            r[i] := 1;
        end;

        pa := 0;
        pb := 0;
        pi := 1;
        ouf.reset;
        for i := 1 to n - 1 do begin
            j := ouf.readInteger;
            if (j < 1) or (m < j) then begin
                quit(_pe, 'Illegal road number.');
            end;
            if (ea[j] = 0) then
                inc(pa)
            else
                inc(pb);
            if (get(es[j]) = get(et[j])) then begin
               quit(_wa, format('Cycle finished on edge %d ' +
               '(%d, %d)', [i, es[j], et[j]]));
            end;
            join(es[j], et[j]);
        end;
//        if (not lineEnd()) or (not ouf.SeekEof) then begin
//            quit(_pe, 'Extra information in output file');
//        end;
        if (pa <> a) or (pb <> b) then begin
            quit(_wa, format('Wrong number of roads of corresponding ' +
            'type: (%d, %d) instead of (%d, %d).', [pa, pb, a, b]));
        end;
        if (r[get(1)] <> n) then begin
            quit(_wa, 'Roads don''t connect all the cities');
        end;
        if (jans = 'no') then begin
            quit(_fail, 'Participant has found a solution, but jury hasn''t!');
        end;
        quit(_ok, inttostr(n) + ' cities');
    end;
    quit(_fail, 'This line of code in the checker is unreachable.');
end.