{.$define checkme}
uses
	testlib, SysUtils;

const
	maxn = 100000;	

var
	n, jn, pn, i, jx, jy, jc, px, py, pc: longint;
	a: array [1..maxn] of longint;
begin
	n := inf.readlongint;
	{$ifdef checkme}
	for i := 1 to n do begin
		a[i] := inf.readlongint;
	end;
	{$endif}

	jn := ans.readlongint;
	pn := ouf.readlongint;
	if jn <> pn then
		quit(_wa, format('wrong count, expected: %d, found: %d', [jn, pn]));

	for i := 1 to jn do begin
		jx := ans.readlongint;
		jy := ans.readlongint;
		jc := ans.readlongint;
		px := ouf.readlongint;
		py := ouf.readlongint;
		pc := ouf.readlongint;

		if (jx <> px) or (jy <> py) or (jc <> pc) then
			quit(_wa, format('wrong potion %d, expected: (%d, %d) cnt = %d, found: (%d, %d) cnt = %d', [i, jx, jy, jc, px, py, pc]));
         
        {$ifdef checkme}
        a[jx] := a[jx] - jc;
        a[jy] := a[jy] - jc;
		{$endif}
	end;

	{$ifdef checkme}
	for i := 1 to n do
		if a[i] < 0 then
			quit(_fail, 'potions checksum wrong');
	{$endif}

	quit(_ok, format('%d substances, %d potions', [n, pn]));
end.