#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vv;
const int M=1<<15;
vv mat[26];
string s;
vv expr(int &it);
vv m0(int H,int W){
	return vv(H,vi(W,0));
}
vv operator+(vv x,vv y){
//	show(x.size());
//	show(y.size());
	assert(x.size()==y.size());
	assert(x[0].size()==y[0].size());
	rep(i,x.size()) rep(j,x[0].size()) x[i][j]+=y[i][j];
	rep(i,x.size()) rep(j,x[0].size()) x[i][j]%=M;
	return x;
}
vv operator-(vv x,vv y){
	assert(x.size()==y.size());
	assert(x[0].size()==y[0].size());
	rep(i,x.size()) rep(j,x[0].size()) x[i][j]-=y[i][j];
	rep(i,x.size()) rep(j,x[0].size()) x[i][j]%=M;
	return x;
}
vv operator*(vv x,vv y){
	if(x.size()==1&&x[0].size()==1) swap(x,y);
	if(y.size()==1&&y[0].size()==1){
		rep(i,x.size()) rep(j,x[0].size()) x[i][j]=x[i][j]*y[0][0]%M;
		return x;
	}
	assert(x[0].size()==y.size());
	int a=x.size(),b=x[0].size(),c=y[0].size();
	vv z=m0(a,c);
	rep(i,a) rep(j,c) rep(k,b) z[i][j]+=x[i][k]*y[k][j];
	rep(i,a) rep(j,c) z[i][j]%=M;
	return z;
}
vv operator-(vv x){
	rep(i,x.size()) rep(j,x[0].size()) x[i][j]=-x[i][j];
	return x;
}
int num(int &it){
	int x=0;
	while(isdigit(s[it])){
		x*=10;
		x+=s[it]-'0';
		it++;
	}
	return x;
}
vv row(int &it){
	vv x=expr(it);
	while(s[it]==' '){
//		puts("row");
		it++;
		vv y=expr(it);
		assert(x.size()==y.size());
		rep(i,x.size()) for(int ye:y[i]) x[i].pb(ye);
	}
	return x;
}
vv row_seq(int &it){
	vv x=row(it);
	while(s[it]==';'){
		it++;
		vv y=row(it);
		assert(x[0].size()==y[0].size());
		for(auto ye:y){
			x.pb(ye);
		}
	}
	return x;
}
vv primary0(int &it){
	if(isdigit(s[it])){//num
		return vv(1,vi(1,num(it)));
	}
	if(isupper(s[it])){//var
		return mat[s[it++]-'A'];
	}
	if(s[it]=='('){
		it++;
		vv x=expr(it);
		assert(s[it]==')');
		it++;
		return x;
	}
	if(s[it]=='['){
		it++;
		vv x=row_seq(it);
		assert(s[it]==']');
		it++;
		return x;
	}
	assert(false);
}
vv primary(int &it){		//all
//	cout<<"be";
//	show(s[it]);
	vv x=primary0(it);
//	if(s[it]=='('||s[it]=='\'') cout<<"prim begin"<<s[it]<<endl;
	while(s[it]=='('||s[it]=='\''){
		if(s[it]=='('){
			it++;
			vv y=expr(it);
			assert(s[it]==',');
			it++;
			vv z=expr(it);
			assert(s[it]==')');
			it++;
			assert(y.size()==1);
			assert(z.size()==1);
			vv nx=m0(y[0].size(),z[0].size());
			rep(i,y[0].size()) rep(j,z[0].size()){
				nx[i][j]=x[y[0][i]-1][z[0][j]-1];
			}
			x=nx;
		}else{
			it++;
			vv nx=m0(x[0].size(),x.size());
			rep(i,x[0].size()) rep(j,x.size()){
				nx[i][j]=x[j][i];
			}
			x=nx;
		}
	}
//	cout<<"en";
//	show(s[it]);
	return x;
}
vv factor(int &it){
	bool m=0;
	while(s[it]=='-') it++,m=!m;
	vv x=primary(it);
	//!!
	if(m) x=-x;
	return x;
}
vv term(int &it){
	vv x=factor(it);
	while(s[it]=='*'){
		it++;
		vv y=factor(it);
		x=x*y;
	}
	return x;
}
vv expr(int &it){
	vv x=term(it);
	while(s[it]=='+'||s[it]=='-'){
		char _=s[it];
//		show(it);
		it++;
		vv y=term(it);
		if(_=='+') x=x+y;
		else x=x-y;
	}
	return x;
}
void assignment(){
	getline(cin,s);
	int id=s[0]-'A';
	int it=2;
	mat[id]=expr(it);
	assert(s[it]=='.');
	int H=mat[id].size(),W=mat[id][0].size();
	rep(i,H){
		rep(j,W){
			cout<<(mat[id][i][j]%M+M)%M;
			if(j==W-1) puts("");
			else cout<<" ";
		}
	}
}
int main(){
	while(true){
		int N;
		cin>>N;
		if(N==0) break;
		getline(cin,s);
		rep(i,N) assignment();
		puts("-----");
	}
}

Measured memory_bytes: 1596