#include<bits/stdc++.h>
#define rep(i,n) for(size_t i=0;i<(size_t)(n);i++)
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

const int mod = 1<<15;
map<char,mat> vars;
string s;

//for matrix operation
mat add(mat A, mat B){
  rep(i,A.size()){
    rep(j,A[0].size()){
      (A[i][j] += B[i][j]) %= mod;
    }
  }
  return A;
}

mat mul(mat A, mat B){
  if(A.size() == 1 && A[0].size()==1){
    rep(i,B.size()){
      rep(j,B[0].size()){
	(B[i][j] *= A[0][0]) %= mod;
      }
    }
    return B;
  }

  if(B.size() == 1 && B[0].size()==1){
    return mul(B,A);
  }

  mat res(A.size(), vec(B[0].size(),0));
  rep(i,A.size()){
    rep(j,B[0].size()){
      rep(k,A[0].size()){
	res[i][j] += (A[i][k] * B[k][j]) % mod;
	res[i][j] %= mod;
      }
    }
  }

  return res;
}

mat neg(mat A){
  rep(i,A.size()){
    rep(j,A[0].size()){
      A[i][j] = (mod - A[i][j]) % mod;
    }
  }
  return A;
}

mat transpose(mat A){
  mat res(A[0].size(), vec(A.size()));
  rep(i,A.size()){
    rep(j,A[0].size()){
      res[j][i] = A[i][j];
    }
  }
  return res;
}

mat index(mat A, mat x, mat y){
  mat res(x[0].size(), vec(y[0].size()));
  rep(i,x[0].size()){
    rep(j,y[0].size()){
      res[i][j] = A[x[0][i]-1][y[0][j]-1];
    }
  }
  return res;
}

//for parsing
mat expr(size_t &p);

mat inum(size_t &p){
  int res = 0;
  while(p<s.size() && isdigit(s[p])){
    res = res*10 + (int)(s[p++]-'0');
  }
  return mat(1,vec(1,res));
}

mat row(size_t &p){
  mat res = expr(p);
  while(p<s.size() && s[p]==' '){
    mat tmp = expr(++p);
    rep(i,res.size()){
      rep(j,tmp[i].size())res[i].push_back(tmp[i][j]);
    }
  }
  return res;
}

mat row_seq(size_t &p){
  mat res = row(p);
  while(p<s.size() && s[p]==';'){
    mat tmp = row(++p);
    rep(i,tmp.size())res.push_back(tmp[i]);
  }
  return res;
}

mat matrix(size_t &p){
  mat res = row_seq(++p);
  p++;
  return res;
}

mat primary(size_t &p){
  mat res;
  if(isdigit(s[p])){
    res = inum(p);
  }else if(isupper(s[p])){
    res = vars[s[p++]];
  }else if(s[p] == '['){
    res = matrix(p);
  }else if(s[p] == '('){
    res = expr(++p);
    p++;
  }else{
    exit(0);
  }

  while(p<s.size() && (s[p]=='(' || s[p]=='\'')){
    if(s[p]=='('){
      mat a = expr(++p);
      mat b = expr(++p);
      p++;
      res = index(res,a,b);
    }else{
      res = transpose(res);
      p++;
    }
  }
  return res;
}

mat factor(size_t &p){
  if(s[p] == '-'){
    return neg(factor(++p));
  }else{
    return primary(p);
  }
}

mat term(size_t &p){
  mat res = factor(p);
  if(p==s.size() || s[p]!='*')return res;
  res = mul(res,term(++p));
  return res;
}

mat expr(size_t &p){
  mat res = term(p);
  while(p<s.size() && (s[p]=='+' || s[p]=='-')){
    if(s[p] == '+'){
      res = add(res,term(++p));
    }else{
      res = add(res,neg(term(++p)));
    }
  }
  return res;
}

int main(){
  cin.tie(); ios::sync_with_stdio(0);

  int n;
  while(cin >> n,n){
    cin.ignore();
    while(n--){
      getline(cin,s);
      char v = s[0];
      s = s.substr(2,s.size()-3);

      size_t p = 0;
      vars[v] = expr(p);

      mat& A = vars[v];
      rep(i,A.size()){
	rep(j,A[i].size()){
	  cout << A[i][j] << " \n"[j+1==A[i].size()];
	}
      }
    }
    cout << "-----" << endl;
  }
}

Measured memory_bytes: 1704