#include<cctype>
#include<cstdio>
#include<vector>
#include<cassert>
#include<cstring>
#include<cstdlib>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef vector< vector<int> > matrix_t;

const int M=32768;

matrix_t operator+(const matrix_t &A,const matrix_t &B){
	int m=A.size(),n=A[0].size();
	matrix_t C=A;
	rep(i,m) rep(j,n) C[i][j]=(C[i][j]+B[i][j])%M;
	return C;
}

matrix_t operator-(const matrix_t &A,const matrix_t &B){
	int m=A.size(),n=A[0].size();
	matrix_t C=A;
	rep(i,m) rep(j,n) C[i][j]=(C[i][j]-B[i][j]+M)%M;
	return C;
}

matrix_t operator*(const matrix_t &A,const matrix_t &B){
	if(A.size()==1 && A[0].size()==1){ // A is scalar
		matrix_t C=B;
		rep(i,B.size()) rep(j,B[0].size()) C[i][j]=B[i][j]*A[0][0]%M;
		return C;
	}
	else if(B.size()==1 && B[0].size()==1){ // B is scalar
		matrix_t C=A;
		rep(i,A.size()) rep(j,A[0].size()) C[i][j]=A[i][j]*B[0][0]%M;
		return C;
	}
	else{
		int m=A.size(),n=A[0].size(),l=B[0].size();
		matrix_t C(m,vector<int>(l));
		rep(i,m) rep(j,l) rep(k,n) C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
		return C;
	}
}

matrix_t operator-(const matrix_t &A){
	int m=A.size(),n=A[0].size();
	matrix_t B=A;
	rep(i,m) rep(j,n) B[i][j]=(M-A[i][j])%M;
	return B;
}

matrix_t transpose(const matrix_t &A){
	int m=A.size(),n=A[0].size();
	matrix_t B(n,vector<int>(m));
	rep(i,m) rep(j,n) B[j][i]=A[i][j];
	return B;
}

matrix_t V[26];

int idx;
char s[128];

matrix_t expr();

matrix_t inum(){
	int num=0;
	for(;isdigit(s[idx]);idx++) num=10*num+(s[idx]-'0');
	return matrix_t(1,vector<int>(1,num));
}

matrix_t var(){
	return V[s[idx++]-'A'];
}

matrix_t row(){
	matrix_t A=expr();
	while(s[idx]==' '){
		idx++;
		matrix_t B=expr();
		rep(i,A.size()) A[i].insert(A[i].end(),B[i].begin(),B[i].end());
	}
	return A;
}

matrix_t row_seq(){
	matrix_t A=row();
	while(s[idx]==';'){
		idx++;
		matrix_t B=row();
		A.insert(A.end(),B.begin(),B.end());
	}
	return A;
}

matrix_t matrix(){
	assert(s[idx++]=='[');
	matrix_t A=row_seq();
	assert(s[idx++]==']');
	return A;
}

matrix_t primary(){
	matrix_t A;
	if     (isdigit(s[idx])) A=inum();
	else if(isalpha(s[idx])) A=var();
	else if(s[idx]=='[')     A=matrix();
	else if(s[idx]=='('){
		idx++;
		A=expr();
		assert(s[idx++]==')');
	}
	else assert(0);

	while(1){
		bool flag=false;
		if(s[idx]=='('){ // indexed-primary
			idx++;
			matrix_t v1=expr();
			assert(s[idx++]==',');
			matrix_t v2=expr();
			assert(s[idx++]==')');

			int m=v1[0].size(),n=v2[0].size();
			matrix_t B(m,vector<int>(n));
			rep(i,m) rep(j,n) B[i][j]=A[v1[0][i]-1][v2[0][j]-1];
			A=B;

			flag=true;
		}
		else if(s[idx]=='\''){ // transposed-primary
			idx++;
			A=transpose(A);
			flag=true;
		}
		if(!flag) break;
	}
	return A;
}

matrix_t factor(){
	if(s[idx]!='-'){
		return primary();
	}
	else{
		idx++;
		return -factor();
	}
}

matrix_t term(){
	matrix_t A=factor();
	while(s[idx]=='*'){
		idx++;
		A=A*factor();
	}
	return A;
}

matrix_t expr(){
	matrix_t A=term();
	while(s[idx]=='+' || s[idx]=='-'){
		char op=s[idx++];
		if(op=='+') A=A+term();
		else        A=A-term();
	}
	return A;
}

void assignment(){
	idx=2;
	V[s[0]-'A']=expr();
	assert(s[idx++]=='.');
}

int main(){
	for(int n;scanf("%d ",&n),n;){
		rep(_,n){
			fgets(s,128,stdin);
			assignment();
			char c=s[0]-'A';
			rep(i,V[c].size()) rep(j,V[c][i].size()) printf("%d%c",V[c][i][j],j+1<V[c][i].size()?' ':'\n');
		}
		puts("-----");
	}
	return 0;
}

Measured memory_bytes: 1396