#include <bits/stdc++.h>

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

using namespace std;
const int mod = 32768;

struct Mod {
  int num;
  Mod () : num(0) {;}
  Mod (int n) : num(n) {;}
  operator int() { return num; }
  operator const int() const { return num; }
};

Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(Mod a) { return Mod((mod - a.num) % mod); }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod fma(Mod a, Mod b, Mod c) { return a * b + c; }

typedef Mod Data;
typedef vector<Data> Array;
typedef vector<Array> Matrix;

Matrix operator-(Matrix mat) {
  REP(i,mat.size()) REP(j,mat[0].size()) mat[i][j] = -mat[i][j];
  return mat;
}

Matrix operator+(Matrix lhs, const Matrix &rhs) {
  REP(i,lhs.size()) REP(j,lhs[0].size()) lhs[i][j] = lhs[i][j] + rhs[i][j];
  return lhs;
}

Matrix operator-(Matrix lhs, const Matrix &rhs) {
  REP(i,lhs.size()) REP(j,lhs[0].size()) lhs[i][j] = lhs[i][j] - rhs[i][j];
  return lhs;
}

Matrix operator*(const Data &lhs, const Matrix &rhs) {
  Matrix res(rhs.size(), Array(rhs[0].size(), 0));
  REP(i,rhs.size()) REP(j,rhs[0].size())
    res[i][j] = lhs * rhs[i][j];
  return res;
}

Matrix operator*(const Matrix &lhs, const Matrix &rhs) {
  if (lhs.size() == 1 && lhs[0].size() == 1) return lhs[0][0] * rhs;
  if (rhs.size() == 1 && rhs[0].size() == 1) return rhs[0][0] * lhs;
  Matrix res(lhs.size(), Array(rhs[0].size(), 0));
  REP(i,lhs.size()) REP(j,rhs[0].size()) REP(k,rhs.size())
    res[i][j] = fma(lhs[i][k], rhs[k][j], res[i][j]);
  return res;
}

void out(const Matrix &mat) {
  REP(i, mat.size()) {
    REP(j, mat[0].size()) {
      if (j) cout << ' ';
      cout << mat[i][j];
    }
    cout << endl;
  }
}

map<char, Matrix> vals;

pair<Matrix, int> expr(const string &s, int i);

pair<Matrix, int> inum(const string &s, int i) {
  Mod v = 0;
  while(i < s.size() && '0' <= s[i] && s[i] <= '9') {
    v *= 10;
    v += s[i] - '0';
    ++i;
  }
  return make_pair(Matrix(1, Array(1, v)), i);
}

void apush(Matrix &mat, const Matrix &val) {
  REP(i,mat.size()) {
    REP(j,val[i].size())
      mat[i].push_back(val[i][j]);
  }
}

pair<Matrix, int> row(const string &s, int i) {
  Matrix res;
  tie(res, i) = expr(s, i);
  while(s[i] == ' ') {
    Matrix val;
    tie(val, i) = expr(s, i+1);
    apush(res, val);
  }
  return make_pair(res, i);
}

void mpush(Matrix &mat, const Matrix &val) {
  REP(i,val.size()) mat.push_back(val[i]);
}

pair<Matrix, int> matrix(const string &s, int i) {
  ++i;
  Matrix res;
  tie(res, i) = row(s, i);
  while(s[i] == ';') {
    Matrix line;
    tie(line, i) = row(s, i+1);
    mpush(res, line);
  }
  return make_pair(res, i+1);
}

Matrix trans(const Matrix &mat) {
  Matrix res(mat[0].size(), Array(mat.size()));
  REP(i,mat.size())REP(j,mat[0].size())
    res[j][i] = mat[i][j];
  return res;
}

Matrix calc_ip(const Matrix &mat, const Matrix &lhs, const Matrix &rhs) {
  Matrix res(lhs[0].size(), Array(rhs[0].size()));
  REP(i,lhs[0].size())REP(j,rhs[0].size())
    res[i][j] = mat[lhs[0][i]-Mod(1)][rhs[0][j]-Mod(1)];
  return res;
}

pair<Matrix, int> primary(const string &s, int i) {
  Matrix res;
  if ('0' <= s[i] && s[i] <= '9') {
    tie(res, i) = inum(s, i);
  } else if ('A' <= s[i] && s[i] <= 'Z') {
    tie(res, i) = make_pair(vals[s[i]], i+1);
  } else if (s[i] == '(') {
    tie(res, i) = expr(s, i+1);
    ++i;
  } else {
    tie(res, i) = matrix(s, i);
  }
  while(i < s.size()) {
    if (s[i] == '(') {
      auto lhs = expr(s, i+1);
      auto rhs = expr(s, lhs.second + 1);
      res = calc_ip(res, lhs.first, rhs.first);
      i = rhs.second + 1;
    } else if (s[i] == '\'') {
      res = trans(res);
      ++i;
    } else {
      break;
    }
  }
  return make_pair(res, i);
}

pair<Matrix, int> factor(const string &s, int i) {
  if (s[i] == '-') {
    auto res = factor(s, i+1);
    res.first = -res.first;
    return res;
  } else {
    return primary(s, i);
  }
}

pair<Matrix, int> term(const string &s, int i) {
  Matrix mat;
  tie(mat, i) = factor(s, i);
  while(i < s.size() && s[i] == '*') {
    auto rhs = factor(s, i+1);
    mat = mat * rhs.first;
    i = rhs.second;
  }
  return make_pair(mat, i);
}

pair<Matrix, int> expr(const string &s, int i) {
  //cout << "expr: " << i << endl;
  Matrix mat;
  tie(mat, i) = term(s, i);
  while(i < s.size() && (s[i] == '+' || s[i] == '-')) {
    auto rhs = term(s, i+1);
    if (s[i] == '+') mat = mat + rhs.first;
    else mat = mat - rhs.first;
    i = rhs.second;
  }
  return make_pair(mat, i);
}

int main() {
  while(1) {
    int n;
    cin>>n;
    if(!n) break;
    vals.clear();
    string dmy;
    getline(cin,dmy);
    REP(i,n) {
      string line;
      getline(cin,line);
      vals[line[0]] = expr(line, 2).first;
      out(vals[line[0]]);
    }
    cout<<"-----"<<endl;
  }
  return 0;
}

Measured memory_bytes: 1460