{$R+,Q+}
{$apptype console}
uses
	Testlib, Sysutils;

const
	maxN = 24;

var
	n : integer;
	a : array[1..maxN,'A'..'Z'] of boolean;
	b : array['A'..'Z'] of boolean;

	s : string;
	i, j, last : integer;
	ch : char;

	outL, ansL : integer;

begin
	n := inf.ReadInteger;
	if (n < 1) or (n > maxN) then
		Quit(_Fail, 'N is invalid');

	fillchar(a, sizeof(a), false);
	for i := 1 to n do begin
		s := inf.ReadWord(eolnChar, eolnChar);
		for j := 1 to length(s) do begin
			ch := s[j];
			if (ch < 'A') or (ch > 'Z') then
				Quit(_Fail, 'Invalid char in input');
			a[i,ch] := true;
		end;
	end;


	outL := ouf.ReadInteger;
	if (outL < 0) or (outL > n) then
		Quit(_WA, 'L is invalid: ' + inttostr(outL));

	fillchar(b, sizeof(b), false);
	last := 0;
	for i := 1 to outL do begin
		j := ouf.ReadInteger;
		if (j < 1) or (j > n) then
			Quit(_WA, 'Bone number #' + inttostr(i) + ' is invalid: ' + inttostr(j));
		if j <= last then
			Quit(_WA, 'Bone number #' + inttostr(i) + ' is invalid: sequence is not strictly ascending');
		for ch := 'A' to 'Z' do
			b[ch] := b[ch] xor a[j,ch];
		last := j;
	end;

	j := 0;
	for ch := 'A' to 'Z' do
		if b[ch] then
			inc(j);

	if j <> 0 then
		Quit(_WA, 'Not all equations are satisfied');

	ansL := ans.ReadInteger;
	if (ansL < 0) or (ansL > n) then
		Quit(_Fail, 'L is invalid');

	if outL < ansL then
		Quit(_WA, 'Not optimial: ' + inttostr(outL) + ' instead of ' + inttostr(ansL));

	if outL > ansL then
		Quit(_Fail, 'Jury''s answer is ' + inttostr(ansL) + ', participant''s is ' + inttostr(outL));

	Quit(_OK, '');
end.
