{$apptype console}

uses
  SysUtils, Math, Types;

var
  p, n, m: integer;
  q: TIntegerDynArray;
  s, d: array of TIntegerDynArray;

procedure Done(const Msg: string; Res: integer);
begin
  Writeln(Msg);
  Halt(Res);
end;

function Compatible(I, J: integer): boolean;
var
  k: integer;
begin
  Result := true;
  for k := 0 to p - 1 do
    if (s[J, k] <> 2) and (d[I, k] <> s[J, k]) then begin
      Result := false;
      Break;
    end;
end;

var
  fi, fo, fa: textfile;
  tfo, tfa: integer;
  isS, isD: boolean;
  i, j, k: integer;
  a, b, w: TIntegerDynArray;
  getting, producing: TIntegerDynArray;
  totalIn, totalOut: integer;
begin
  AssignFile(fi, ParamStr(1));
  AssignFile(fo, ParamStr(2));
  AssignFile(fa, ParamStr(3));

  try
    {$I-}
    Reset(fi);
    Reset(fo);
    Reset(fa);
    if (IOResult <> 0) then
      Abort;
    {$I+}

    Read(fi, p, n);
    SetLength(s, n, p);
    SetLength(d, n, p);
    SetLength(q, n);
    for i := 0 to n - 1 do begin
      Read(fi, q[i]);
      for j := 0 to p - 1 do
        Read(fi, s[i, j]);
      for j := 0 to p - 1 do
        Read(fi, d[i, j]);
    end;

    Read(fo, tfo);
    Read(fa, tfa);

    Read(fo, m);
    SetLength(a, m);
    SetLength(b, m);
    SetLength(w, m);
    for i := 0 to m - 1 do begin
      Read(fo, a[i], b[i], w[i]);
      if not InRange(a[i], 1, n) or not InRange(b[i], 1, n) then
        Done(Format('Invalid production line: %d - %d', [a[i], b[i]]), 2);
      if (w[i] <= 0) then
        Done(Format('Negative production line: %d - %d', [a[i], b[i]]), 2);
      Dec(a[i]);
      Dec(b[i]);
    end;
    if not SeekEof(fo) then
      Done('Extra characters in output file', 2);

    SetLength(getting, n);
    SetLength(producing, n);
    for i := 0 to n - 1 do begin
      getting[i] := 0;
      producing[i] := 0;
    end;

    for i := 0 to m - 1 do begin 
      if not Compatible(a[i], b[i]) then
        Done(
          Format(
            'Machines %d and %d are not compatible', [a[i] + 1, b[i] + 1]),
          1);
      Inc(getting[b[i]], w[i]);
      Inc(producing[a[i]], w[i]);
    end;

    totalIn := 0;
    totalOut := 0;
    for i := 0 to n - 1 do begin
      if producing[i] > q[i] then
        Done(Format(
          'Machine %d is overproducing (cap = %d, prod = %d)',
          [i + 1, q[i], producing[i]]), 1);
      if getting[i] > q[i] then
        Done(Format(
          'Machine %d is overconsuming (cap = %d, consume = %d)',
          [i + 1, q[i], getting[i]]), 1);

      isD := true;
      isS := true;
      for k := 0 to p - 1 do begin
        if (s[i, k] = 1) then
          isS := false;
        if (d[i, k] = 0) then
          isD := false;
      end;
      if isS and isD then begin
        getting[i] := q[i];
        producing[i] := q[i];
      end;
      if isS then
        Inc(totalIn, producing[i]);
      if isD then
        Inc(totalOut, getting[i]);

      if not isD and not isS and (producing[i] <> getting[i]) then
        Done(Format(
          'Intermediate machine %d is not balanced (consume = %d, prod = %d)',
          [i + 1, getting[i], producing[i]]), 1);
    end;
    if (totalIn <> tfo) then
      Done(Format('TotalIn (%d) <> flow (%d)', [totalIn, tfo]), 1);
    if (totalOut <> tfo) then
      Done(Format('TotalOut (%d) <> flow (%d)', [totalOut, tfo]), 1);

    if (tfa > tfo) then
      Done(Format('Production is worse, output %d, answer %d', [tfo, tfa]), 1)
    else if (tfa < tfo) then
      Done(
        Format('!!! Production is better, output %d, answer %d', [tfo, tfa]),
        3);
  except
    Halt(2);
  end;
  CloseFile(fi);
  CloseFile(fo);
  CloseFile(fa);

  Done('ok', 0);
end.
