#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <sstream>

using namespace std;
typedef long long ll;
typedef pair<int,int> PI;
const double EPS=1e-6;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define F first
#define S second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) a.rbegin(),a.rend()
#define FLL(a,b) memset((a),b,sizeof(a))
#define CLR(a) FLL(a,0)
#define declare(a,it) __typeof(a) it=(a)
#define FOR(it,a) for(declare(a.begin(),it);it!=a.end();++it)
#define FORR(it,a) for(declare(a.rbegin(),it);it!=a.rend();++it)
template<class T,class U> ostream& operator<< (ostream& o, const pair<T,U>& v){return o << "(" << v.F << ", " << v.S << ")";}
template<class T> ostream& operator<< (ostream& o, const vector<T>& v){o << "{";rep(i,SZ(v)) o << (i?", ":"") << v[i];return o << "}";}
int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,-1,1,1,-1};
int s2i(string& a){stringstream ss(a);int r;ss>>r;return r;}
int geti(){int n;scanf("%d", &n);return n;}

typedef vector<vector<unsigned short> > El;
El num[100];
string in;
int pos;
El expr();

El row(){
  El a = expr();
  while(in[pos] == ' '){
    ++pos;
    auto b = expr();
    rep(i,SZ(b))rep(j,SZ(b[0]))
      a[i].push_back(b[i][j]);
  }
  return a;
}

El matrix(){
  //cout<< "matrix " << pos << endl;
  ++pos;
  El ret = row();
  while(in[pos]==';'){
    ++pos;
    for(auto p : row())
      ret.push_back(p);
  }
  ++pos;
  return ret;
}

El primary(){
  El ret;
  if(isdigit(in[pos])){
    unsigned short a = 0;
    while(isdigit(in[pos]))
      a = a * 10 + in[pos++] - '0';
    ret =  El(1,vector<unsigned short>(1,a));
  }else  if(isalpha(in[pos])){
    ret= num[in[pos++]-'A'];
  }else if(in[pos] == '('){
    ++pos;
    ret = expr();
    ++pos;
  }else ret = matrix();

  while(true){
    if(in[pos]=='\''){
      ++pos;
      El nret(SZ(ret[0]));
      rep(i,SZ(ret[0]))rep(j,SZ(ret))
        nret[i].pb(ret[j][i]);
      ret = nret;
    }else if(in[pos]=='('){
      ++pos;
      El b = expr();
      ++pos;
      El c = expr();
      ++pos;
      // cout << b << endl;
      // cout << c << endl;
      int k = b[0].size();
      int l = c[0].size();
      El na(k,vector<unsigned short>(l));
      rep(i,k)rep(j,l)
        na[i][j] = ret[b[0][i]-1][c[0][j]-1];
      ret = na;
    }else break;
  }
  return ret;
}

El factor(){
  if(in[pos]=='-'){
    ++pos;
    El a = factor();
    for(auto & r : a)
      for(auto & p : r) p = -p;
    return a;
  }
  return primary();
}

El term(){
  El a = factor();
  while(in[pos]=='*'){
    auto op = in[pos++];
    El b = factor();

    if(a.size() == 1 && a[0].size() == 1){
      rep(i,SZ(b))rep(j,SZ(b[i])) b[i][j] *= a[0][0];
      a = b;
    }else if(b.size() == 1 && b[0].size() == 1){
      rep(i,SZ(a))rep(j,SZ(a[i])) a[i][j] *= b[0][0];
    }else{
      El na(a.size(), vector<unsigned short>(b[0].size()));
      rep(i,SZ(a))rep(j,SZ(b[0]))
        rep(k,SZ(a[0])) na[i][j] += a[i][k]*b[k][j];
      a = na;
    }
  }
  return a;
}

El expr(){
  //cout << "expr " << pos << endl;
  El a = term();
  while(in[pos]=='+' || in[pos]=='-'){
    auto op = in[pos++];
    El b = term();
    rep(i,SZ(b))rep(j,SZ(b[0]))
      a[i][j] += op=='+'?b[i][j]:-b[i][j];
  }
  return a;
}



El ass(){
  int id = in[pos++] - 'A';
  ++pos;
  return num[id] = expr();
}

int main(int argc, char *argv[])
{
  int n;
  while(cin >> n && n){
    cin.get();
    rep(i,n){
      getline(cin, in);
      pos = 0;
      auto p = ass();
      for(auto r : p){
        for(int i = 0; i < (int)r.size(); ++i){
          if(i) cout << " ";
          cout << r[i]%(1<<15);
        }
        cout << endl;
      }
    }
    cout << "-----" << endl;
  }
  return 0;
}

Measured memory_bytes: 1416