uses
	testlib;

var
	txt, enc, pen: string;
	code: array ['a'..'z'] of string;
	c, ci, cj: char;
	i: longint;

begin
	txt := inf.readword(blanks, blanks);
	enc := inf.readword(blanks, blanks);

	while not ouf.seekeof do begin
		c := ouf.nextchar;
		if (pos(c, txt) = 0) then
			quit(_wa, 'character ' + c + ' is assigned code, but it doesn''t occur in text');

		code[c] := ouf.readword(blanks, blanks);
		if (length(code[c]) > length(enc))  then
			quit(_wa, 'too long code for ' + c);
		for i := 1 to length(code[c]) do begin
			if not (code[c][i] in ['0', '1']) then
				quit(_wa, 'invalid character in the code word for ' + c);
		end;
	end;

	for ci := 'a' to 'z' do begin
		for cj := 'a' to 'z' do begin
			if (ci <> cj) and (code[ci] <> '') and (code[cj] <> '') then begin
				if pos(code[ci], code[cj]) = 1 then
					quit(_wa, 'code is not prefix free');
			end;
		end;
	end;

	pen := '';
	for i := 1 to length(txt) do begin
		pen := pen + code[txt[i]];
		if length(pen) > length(enc) then
			quit(_wa, 'wrong encoded word');
		if copy(enc, 1, length(pen)) <> pen then
			quit(_wa, 'wrong encoded word');
	end;

	if pen <> enc then quit(_wa, 'wrong encoded word');

	quit(_ok, '');
end.