#include<cstdio>
#include<vector>
#include<string>
#include<cctype>
#include<iostream>
#include<algorithm>

using namespace std;

const int mod=32768;

typedef string::iterator State;

typedef vector<int> Vector;
typedef vector<Vector> Matrix;

string str;

//Matrix calculation

Matrix vars[30];

Matrix inum(State &begin);
Matrix row(State &begin);
Matrix row_seq(State &begin);
Matrix matrix(State &begin);
Matrix primary(State &begin);
Matrix factor(State &begin);
Matrix term(State &begin);
Matrix expr(State &begin);
Matrix assignment(State &begin);

Matrix ad(Matrix m1,Matrix m2);
Matrix red(Matrix m1,Matrix m2);
Matrix mult(Matrix m1,Matrix m2);
Matrix indexed_primary(Matrix m1,Matrix is,Matrix js);
Matrix transposed_primary(Matrix m);
Matrix yokocon(Matrix m1,Matrix m2);
Matrix tatecon(Matrix m1,Matrix m2);
Matrix negate_(Matrix m);

void print(Matrix m){
	printf("--\n");
	for(int i=0;i<m.size();i++){
		for(int j=0;j<m[i].size();j++){
			printf("%d ",m[i][j]);
		}
		printf("\n");
	}
}

Matrix inum(State &begin){
	int ret=0;
	while(isdigit(*begin)){
		ret*=10;
		ret+=(*begin)-'0';
		ret%=mod;
		begin++;
	}
	Vector a;
	a.push_back(ret);
	Matrix b;
	b.push_back(a);
//	cout<<b[0][0]<<endl;
	return b;
}

Matrix row(State &begin){
	Matrix res=expr(begin);
	if(*begin!=' '){
//		begin++;
		return res;
	}
	while(*begin==' '){
		begin++;
		Matrix cur=expr(begin);
		//print(cur);
		//cout<<"in row "<<cur[0][0]<<endl;
		res=yokocon(res,cur);
	}
	//cout<<"result of row"<<endl;
	//for(int i=0;i<res[0].size();i++) cout<<res[0][i]<<" ";
	//cout<<endl;
//	begin++;
	return res;
}

int c=0;

Matrix row_seq(State &begin){
	//cout<<"row_seq "<<*begin<<endl;
//	c++;
//	if(c==10) exit(0);
//	begin++;
	Matrix res=row(begin);
	while(*begin==';'){
		begin++;
		Matrix cur=row(begin);
		res=tatecon(res,cur);
	}
//	begin++;
	return res;
}

Matrix matrix(State &begin){
	begin++;
	//cout<<"matrix "<<*begin<<endl;
	Matrix res=row_seq(begin);
	begin++;
	return res;
}

Matrix primary(State &begin){
	Matrix res;
	if(isdigit(*begin)){
		//cout<<"digit"<<endl;
		res=inum(begin);
	}else if(isalpha(*begin)){
		char ch=*begin;
		res=vars[ch-'A'];
		begin++;
	}else if(*begin=='('){
		begin++;
		res=expr(begin);
		begin++;
	}else if(*begin=='['){
		//cout<<"primary matrix "<<*begin<<endl;
		res=matrix(begin);
	}
	while(*begin=='\''||*begin=='('){
		if(*begin=='\''){
	//		cout<<"position = "<<distance(str.begin(),begin)<<endl;
			res=transposed_primary(res);
			begin++;
		}
		if(*begin=='('){
			begin++;
			Matrix is=expr(begin);
			begin++;
			Matrix js=expr(begin);
			begin++;
			res=indexed_primary(res,is,js);
		}
	}
	return res;
}

Matrix factor(State &begin){
	//cout<<"factor "<<*begin<<endl;
	if(*begin=='-'){
		begin++;
		Matrix res=factor(begin);
		return negate_(res);
	}
	Matrix res=primary(begin);
	return res;
}

Matrix term(State &begin){
	Matrix res=factor(begin);
	while(*begin=='*'){
		begin++;
		Matrix cur=factor(begin);
		res=mult(res,cur);
	}
	return res;
}

Matrix expr(State &begin){
	//cout<<"expr "<<*begin<<endl;
	Matrix res=term(begin);
	while(*begin=='+'||*begin=='-'){
		//cout<<"in expr "<<*begin<<endl;
		char ch=*begin;
		begin++;
		Matrix cur=term(begin);
		if(ch=='+'){
			res=ad(res,cur);
		}else{
			res=red(res,cur);
		}
	}
	return res;
}

Matrix assignment(State &begin){
	char ch=*begin;
	begin++;
	begin++;
	Matrix res=expr(begin);
	vars[ch-'A']=res;
	return res;
}

Matrix ad(Matrix m1,Matrix m2){
	Matrix res=m1;
	for(int i=0;i<m1.size();i++) for(int j=0;j<m1[0].size();j++){
		res[i][j]+=m2[i][j];
		res[i][j]%=mod;
	}
	return res;
}

Matrix red(Matrix m1,Matrix m2){
	Matrix res=m1;
	for(int i=0;i<m1.size();i++) for(int j=0;j<m1[0].size();j++){
		res[i][j]+=(mod-m2[i][j]);
		res[i][j]%=mod;
	}
	return res;
}

Matrix mult(int x,Matrix m);

Matrix mult(Matrix m1,Matrix m2){
	if(m1.size()==1&&m1[0].size()==1) return mult(m1[0][0],m2);
	if(m2.size()==1&&m2[0].size()==1) return mult(m2[0][0],m1);
	int r=m1.size(),c=m2[0].size();
	Matrix res=Matrix(r,Vector(c,0));
	for(int i=0;i<r;i++){
		for(int j=0;j<m1[0].size();j++){
			for(int k=0;k<c;k++){
				res[i][k]+=m1[i][j]*m2[j][k];
				res[i][k]%=mod;
			}
		}
	}
	return res;
}

Matrix mult(int x,Matrix m){
	for(int i=0;i<m.size();i++) for(int j=0;j<m[0].size();j++) m[i][j]=(m[i][j]*x)%mod;
	return m;
}

Matrix transposed_primary(Matrix m){
	Matrix res=Matrix(m[0].size(),Vector(m.size(),0));
	for(int i=0;i<m.size();i++) for(int j=0;j<m[0].size();j++) res[j][i]=m[i][j];
	return res;
}

Matrix indexed_primary(Matrix m,Matrix is,Matrix js){
	Matrix res=Matrix(is[0].size(),Vector(js[0].size(),0));
	for(int i=0;i<is[0].size();i++) for(int j=0;j<js[0].size();j++){
		res[i][j]=m[is[0][i]-1][js[0][j]-1];
	}
	return res;
}

Matrix yokocon(Matrix m1,Matrix m2){
	Matrix res=Matrix(m1.size(),Vector(0));
	for(int i=0;i<res.size();i++){
		for(int j=0;j<m1[0].size();j++){
			res[i].push_back(m1[i][j]);
		}
		for(int j=0;j<m2[0].size();j++){
			res[i].push_back(m2[i][j]);
		}
	}
	return res;
}

Matrix tatecon(Matrix m1,Matrix m2){
	Matrix res=Matrix((m1.size()+m2.size()),Vector(0));
	for(int i=0;i<m1.size();i++) res[i]=m1[i];
	for(int i=0;i<m2.size();i++) res[i+m1.size()]=m2[i];
	return res;
}

Matrix negate_(Matrix m){
	for(int i=0;i<m.size();i++) for(int j=0;j<m[0].size();j++){
		m[i][j]=mod-m[i][j];
	}
	return m;
}

void init(){
	Matrix ini;
	for(int i=0;i<30;i++) vars[i]=ini;
}

char ch[100];

int main(){
	while(true){
		int N;
		scanf("%d",&N);
		gets(ch);
		//cout<<N<<endl;
		if(N==0) break;
		init();
		for(int i=0;i<N;i++){
			gets(ch);
			str=ch;
		//	cout<<str<<endl;
	//		return 0;
			State st=str.begin();
			Matrix res=assignment(st);
			for(int i=0;i<res.size();i++){
				for(int j=0;j<res[i].size();j++){
					printf("%d%c",res[i][j],j+1==res[i].size()?'\n':' ');
				}
			}
			//cout<<*st<<endl;
			//if(st==str.end()) st<<"end"<<endl;
		}
		printf("-----\n");
//		break;
	}
	return 0;
}

Measured memory_bytes: 1604