var
A:array [1..9] of char; {加法表字母}
B:array [A..Z,A..Z] of string[2]; {加法表字串}
C:array [A..Z] of integer; {存放字母所代表的数字}
i,j,k,n:integer;
c1,c2:char;
fd:boolean;
procedure enter_data;{读加法表过程}
var
i,j:integer;
c1,c2:char;
begin
j:=0;
repeat {读第一行字串}
read(c1);
if (c1>=A) and (c1<=Z) then {大写字母}
begin
inc(j);
A[j]:=c1;
end; {读入字母表}
until j=n-1;
for i:=1 to n-1 do
for j:=1 to n-1 do B[A[i],A[j]]:=;{加法表字串初始化}
for i:=2 to n do
begin
repeat
read(c2) {读每行的第一个大写字母}
until (c2>=A)and(c2<=Z);
read(c1); {读空格符}
j:=0;
repeat
inc(j);
read(c1);
while (c1>=A) and (c1<=Z) do {读连续的大写字母串}
begin
B[c2,A[j]]:=B[c2,A[j]]+c1; {加法表字串}
read(c1); {读下一个字符}
end;
until j=n-1;
if c1= then readln;
end;{for i}
end;{enter_data}
function success:boolean;{检查加法表规则}
var
i,j:integer;
fd:boolean;
begin
fd:=true;
i:=1;
while (i<=n-1) and fd do
begin
j:=1;
while (j<=n-1) and fd do
begin
if length(B[A[i],A[j]])<=1 then {加法表字串只是一位字母时}
fd:=C[B[A[i],A[j]][1]]=C[A[i]]+C[A[j]]
else
fd:=C[B[A[i],A[j]][1]]*(n-1)+C[B[A[i],A[j]][2]]=C[A[i]]+C[A[j]];
j:=j+1;
end;{while j}
i:=i+1;
end;{while i}
success:=fd;
end;{success}
begin
readln(n);
if (n<=1)or(n>9) then
begin
writeln(Error!);
halt;
end;
enter_data; {调用读加法表过程}
for i:=1 to n-1 do C[A[i]]:=-1;{数码表初始化}
fd:=false;
for i:=1 to n-1 do {求对角字串}
if B[A[i],A[i]]=A[i] then C[A[i]]:=0 {找代表数字0的字母}
else
if length(B[A[i],A[i]])>1 then {对角字串的长度超过1}
begin
c1:=B[A[i],A[i]][1]; c2:=c1;
C[c1]:=1;{长度为2的对角字串的第一位字母代表的数字是1}
fd:=true;{fd为true时表示已找到数字1}
end;
if fd then {已找到数字1}
for j:=2 to n-2 do {找代表数字2至n-2的字母}
begin
C[B[c1,c2][1]]:=1+C[c2];{c1表示数字1,则1+c1代表的数字是1+C[c1]}
c2:=B[c1,c2][1]; {递推求代表下一个数字的字母}
end;
if success then {调用过程success,检查加法表规则}
begin
for i:=1 to n-1 do
write(A[i],=,C[A[i]], );{输出字母及所其代表的数字}
writeln;write(n-1);{输出加法运算的进位制}
end
else
begin
writeln(ERROR!);
halt;
end;{不能组成加法表}
end.