#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> arr;
typedef vector<arr> mat;
inline mat mul(mat a,mat b,int mod){
  if((int)a.size()==1&&(int)a[0].size()==1){
    mat res(b);
    for(int i=0;i<(int)b.size();i++)
      for(int j=0;j<(int)b[i].size();j++)
	(res[i][j]*=a[0][0])%=mod;
    return res;
  }
  if((int)b.size()==1&&(int)b[0].size()==1){
    mat res(a);
    for(int i=0;i<(int)a.size();i++)
      for(int j=0;j<(int)a[i].size();j++)
	(res[i][j]*=b[0][0])%=mod;
    return res;
  }
  mat res(a.size(),arr(b[0].size(),0));
  for(int i=0;i<(int)a.size();i++)
    for(int j=0;j<(int)b[0].size();j++)
      for(int k=0;k<(int)b.size();k++)
	(res[i][j]+=a[i][k]*b[k][j])%=mod;
  return res;
}
inline mat add(mat a,mat b,int mod){
  mat res(a);
  for(int i=0;i<(int)b.size();i++)
    for(int j=0;j<(int)b[i].size();j++)
      (res[i][j]+=b[i][j])%=mod;
  return res;
}
inline mat sub(mat a,mat b,int mod){
  mat res(a);
  for(int i=0;i<(int)b.size();i++)
    for(int j=0;j<(int)b[i].size();j++)
      (res[i][j]+=mod-b[i][j])%=mod;
  return res;
}
mat var(int &p);
mat expr(int &p);
mat term(int &p);
mat factor(int &p);
mat primary(int &p);
mat indexed_primary(mat a,int &p);
mat transposed_primary(mat a,int &p);
mat matrix(int &p);
mat row_seq(int &p);
mat row(int &p);
mat inum(int &p);
const int M=1<<15;
map<char,mat> mm;
string s;
bool dbg=0;
void print(mat a){
  for(int i=0;i<(int)a.size();i++){
    for(int j=0;j<(int)a[i].size();j++){
      if(j) cout<<" ";
      cout<<a[i][j];
    }
    cout<<endl;
  }
}
mat var(int &p){
  if(dbg) cout<<"var:"<<endl,print(mm[s[p]]);
  return mm[s[p++]];
}
mat expr(int &p){
  if(dbg) cout<<"expr:"<<endl;
  mat res=term(p);
  while(p<(int)s.size()){
    if(s[p]=='+'){
      p++;
      res=add(res,term(p),M);
      continue;
    }
    if(s[p]=='-'){
      p++;
      res=sub(res,term(p),M);
      continue;
    }
    break;
  }
  if(dbg) cout<<"expr:"<<endl,print(res);
  return res;
}
mat term(int &p){
  if(dbg) cout<<"term:"<<endl;
  mat res=factor(p);
  while(p<(int)s.size()){
    if(s[p]=='*'){
      p++;
      res=mul(res,factor(p),M);
      continue;
    }
    break;
  }
  if(dbg) cout<<"term:"<<endl,print(res);
  return res;
}
mat factor(int &p){
  if(dbg) cout<<"factor:"<<endl;
  mat res;
  if(s[p]=='-'){
    p++;
    mat tmp=factor(p);
    res=sub(mat(tmp.size(),arr(tmp[0].size(),0)),tmp,M);
  }else{
    res=primary(p);
  }
  if(dbg) cout<<"factor:"<<endl,print(res);
  return res;
}
mat primary(int &p){
  if(dbg) cout<<"primary:"<<endl;
  mat res;
  if(isupper(s[p])){
    res=var(p);
  }else if(s[p]=='('){
    p++;
    res=expr(p);
    p++;
  }else if(s[p]=='['){
    res=matrix(p);
  }else{
    res=inum(p);
  }
  while(p<(int)s.size()){
    if(s[p]=='('){
      p++;
      res=indexed_primary(res,p);
      p++;
      continue;
    }
    if(s[p]=='\''){
      p++;
      res=transposed_primary(res,p);
      continue;
    }
    break;
  }
  if(dbg) cout<<"primary:"<<endl,print(res);
  return res;
}
mat indexed_primary(mat a,int &p){
  if(dbg) cout<<"indexed_primary:"<<endl;
  mat res,b,c;
  b=expr(p);
  p++;
  c=expr(p);
  res.resize(b[0].size());
  for(int i=0;i<(int)b[0].size();i++){
    res[i].resize(c[0].size());
    for(int j=0;j<(int)c[0].size();j++)
      res[i][j]=a[b[0][i]-1][c[0][j]-1];
  }
  if(dbg) cout<<"indexed_primary:"<<endl,print(res);
  return res;
}
mat transposed_primary(mat a,int &p){
  if(dbg) cout<<"transposed_primary:"<<endl;
  mat res;
  res.resize(a[0].size());
  for(int i=0;i<(int)a[0].size();i++){
    res[i].resize(a.size());
    for(int j=0;j<(int)a.size();j++)
      res[i][j]=a[j][i];
  }
  if(dbg) cout<<"transposed_primary:"<<endl,print(res);
  return res;
}
mat matrix(int &p){
  if(dbg) cout<<"matrix:"<<endl;
  mat res;
  p++;
  res=row_seq(p);
  p++;
  if(dbg) cout<<"matrix:"<<endl,print(res);
  return res;
}
mat row_seq(int &p){
  if(dbg) cout<<"row_seq:"<<endl;
  mat res=row(p);
  while(p<(int)s.size()){
    if(s[p]==';'){
      p++;
      mat tmp=row(p);
      for(int i=0;i<(int)tmp.size();i++)
	res.push_back(tmp[i]);
      continue;
    }
    break;
  }
  if(dbg) cout<<"row_seq:"<<endl,print(res);
  return res;
}
mat row(int &p){
  if(dbg) cout<<"row:"<<endl;
  mat res=expr(p);
  while(p<(int)s.size()){
    if(s[p]==' '){
      p++;
      mat tmp=expr(p);
      for(int i=0;i<(int)tmp.size();i++)
	for(int j=0;j<(int)tmp[i].size();j++)
	  res[i].push_back(tmp[i][j]);
      continue;
    }
    break;
  }
  if(dbg) cout<<"row:"<<endl,print(res);
  return res;
}
mat inum(int &p){
  if(dbg) cout<<"inum:"<<endl;
  mat res(1,arr(1,0));
  while(p<(int)s.size()&&isdigit(s[p]))
    res[0][0]=(res[0][0]*10+s[p++]-'0')%M;
  if(dbg) cout<<"inum:"<<endl,print(res);
  return res;
}

string str[20];
signed main(){
  int n;
  while(cin>>n,n){
    cin.ignore();
    for(int i=0;i<n;i++) getline(cin,str[i]);
    //cout<<n<<endl;for(int i=0;i<n;i++) cout<<str[i]<<endl;
    mm.clear();
    for(int i=0;i<n;i++){
      int p=2;
      s=str[i];
      s.pop_back();
      mm[s[0]]=expr(p);
      for(int j=0;j<(int)mm[s[0]].size();j++){
	for(int k=0;k<(int)mm[s[0]][j].size();k++){
	  if(k) cout<<" ";
	  cout<<mm[s[0]][j][k];
	}
	cout<<endl;
      }
    }
    cout<<"-----"<<endl;
  }
  return 0;
}

Measured memory_bytes: 3536