#include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <vector>
#include <algorithm>
#include <string>
#include <map>

#define fprintf(...) void(0)

const int M=32768;

class Matrix {
  size_t r=0, c=0;
  std::vector<std::vector<int>> entity;
  
public:
  Matrix(): entity(0) {}
  Matrix(int x): r(1), c(1), entity(1, std::vector<int>(1, x)) {}
  Matrix(size_t r, size_t c): r(r), c(c), entity(r, std::vector<int>(c)) {}
  Matrix(const Matrix &oth): r(oth.r), c(oth.c), entity(oth.entity) {}
  Matrix(Matrix &&oth): r(oth.r), c(oth.c), entity(std::move(oth.entity)) {}

  Matrix &operator =(const Matrix &oth) {
    r = oth.r;
    c = oth.c;
    entity = oth.entity;
    return *this;
  }

  Matrix &operator =(Matrix &&oth) {
    r = oth.r;
    c = oth.c;
    entity = std::move(oth.entity);
    return *this;
  }

  Matrix &operator +=(const Matrix &oth) {
    assert(r == oth.r && c == oth.c);
    for (size_t i=0; i<r; ++i)
      for (size_t j=0; j<c; ++j) {
        entity[i][j] += oth[i][j];
        if (entity[i][j] >= M) entity[i][j] -= M;
      }

    return *this;
  }

  Matrix operator +(const Matrix &oth) const {
    return Matrix(*this) += oth;
  }

  Matrix &operator -=(const Matrix &oth) {
    assert(r == oth.r && c == oth.c);
    for (size_t i=0; i<r; ++i)
      for (size_t j=0; j<c; ++j) {
        entity[i][j] -= oth[i][j];
        if (entity[i][j] < 0) entity[i][j] += M;
      }

    return *this;
  }

  Matrix operator -(const Matrix &oth) const {
    return Matrix(*this) -= oth;
  }

  Matrix operator *(const Matrix &oth) const {
    if (oth.r == 1 && oth.c == 1) {
      Matrix res(*this);
      for (size_t i=0; i<r; ++i)
        for (size_t j=0; j<c; ++j)
          res[i][j] = res[i][j]*oth[0][0] % M;

      return res;
    }
    if (r == 1 && c == 1) {
      Matrix res(oth);
      for (size_t i=0; i<oth.r; ++i)
        for (size_t j=0; j<oth.c; ++j)
          res[i][j] = res[i][j]*entity[0][0] % M;

      return res;
    }
    assert(c == oth.r);
    Matrix res(r, oth.c);
    for (size_t i=0; i<r; ++i)
      for (size_t j=0; j<c; ++j)
        for (size_t k=0; k<oth.c; ++k) {
          res[i][k] += entity[i][j] * oth[j][k];
          if ((res[i][k] %= M) < 0) res[i][k] += M;
        }

    return res;
  }

  Matrix &operator *=(const Matrix &oth) {
    Matrix rhs=(*this)*oth;
    return (*this = rhs);
  }

  const std::vector<int> &operator [](size_t i) const {
    return entity[i];
  }

  std::vector<int> &operator [](size_t i) {
    return entity[i];
  }

  Matrix operator [](
      const std::pair<std::vector<int>, std::vector<int>> &ind) const {

    size_t ir=ind.first.size(), ic=ind.second.size();
    Matrix res(ir, ic);
    for (size_t i=0; i<ir; ++i)
      for (size_t j=0; j<ic; ++j)
        res[i][j] = entity[ind.first[i]-1][ind.second[j]-1];

    return res;
  }

  Matrix operator -() const {
    Matrix res(r, c);
    for (size_t i=0; i<r; ++i)
      for (size_t j=0; j<c; ++j)
        if (entity[i][j])
          res[i][j] = M-entity[i][j];

    return res;
  }

  Matrix operator ~() const {
    Matrix res(c, r);
    for (size_t i=0; i<r; ++i)
      for (size_t j=0; j<c; ++j)
        res[j][i] = entity[i][j];

    return res;
  }

  size_t row() const {
    return entity.size();
  }

  size_t col() const {
    return entity[0].size();
  }

  void add_row() {
    entity.emplace_back();
    ++r;
  }

  void add_col(size_t i, int x) {
    entity[i].push_back(x);
    if (c < entity[i].size())
      c = entity[i].size();
  }

  void output() const {
    for (size_t i=0; i<r; ++i)
      for (size_t j=0; j<c; ++j)
        printf("%d%c", entity[i][j], j+1<c? ' ':'\n');
  }
};

Matrix parse(
    const std::string &s, size_t &i, const std::map<char, Matrix> &vars,
    size_t preced=0) {

  static const std::vector<std::string> &ops={"+-", "*"};

  fprintf(stderr, "s[%zu]: %c #%zu\n", i, s[i], preced);
  if (preced == ops.size()) {
    if (s[i] == '-') {
      return -parse(s, ++i, vars, preced);
    }

    Matrix res;
    if (isdigit(s[i])) {
      int scl=s[i]-'0';
      while (++i < s.length() && isdigit(s[i]))
        scl = scl*10+s[i]-'0';
      if ((scl %= M) < 0) scl += M;
      res = Matrix(scl);
    } else if (isupper(s[i])) {
      res = vars.at(s[i++]);
    } else if (s[i] == '(') {
      res = parse(s, ++i, vars, 0);
      assert(s[i] == ')');
      ++i;
    } else if (s[i] == '[') {
      // tsurai
      ++i;
      size_t r=0, c=0;
      while (s[i] != ']') {
        Matrix tmp=parse(s, i, vars, 0);
        for (size_t ir=0; ir<tmp.row(); ++ir) {
          if (r+ir >= res.row()) res.add_row();
          for (size_t ic=0; ic<tmp.col(); ++ic) {
            // fprintf(stderr, "r+ir: %zu\n", r+ir);
            if (c+ic < res[r+ir].size()) {
              res[r+ir][c+ic] = tmp[ir][ic];
            } else {
              res.add_col(r+ir, tmp[ir][ic]);
            }
          }
        }
        // fprintf(stderr, "> s[%zu]\n", i);
        if (s[i] == ' ') {
          c += tmp.col();
          ++i;
        } else if (s[i] == ';') {
          r = res.row();
          c = 0;
          ++i;
        } else {
          assert(s[i] == ']');
        }
      }
      ++i;
    } else {
      assert(false);
    }

    while (s[i] == '\'' || s[i] == '(') {
      char ch=s[i++];
      if (ch == '\'') {
        res = ~res;
      } else /* if (ch == '(') */ {
        const std::vector<int> fst=parse(s, i, vars, 0)[0];
        assert(s[i] == ',');
        const std::vector<int> snd=parse(s, ++i, vars, 0)[0];
        assert(s[i] == ')');
        ++i;
        res = res[std::make_pair(fst, snd)];
      }
    }
    // fprintf(stderr, ">> s[%zu]\n", i);
    return res;
  }

  Matrix lhs=parse(s, i, vars, preced+1);
  while (i < s.length()) {
    char op=s[i];
    if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break;
    Matrix rhs=parse(s, ++i, vars, preced+1);
    if (op == '+') {
      lhs += rhs;
    } else if (op == '-') {
      lhs -= rhs;
    } else if (op == '*') {
      lhs *= rhs;
    }
  }
  return lhs;
}

int testcase_ends() {
  int n;
  scanf("%d", &n);
  if (n == 0) return 1;

  std::map<char, Matrix> vars;
  for (int i=0; i<n; ++i) {
    char buf[128];
    scanf(" %[^\n]", buf);
    std::string s=buf;
    fprintf(stderr, "%s\n", s.c_str());
    s.pop_back();
    char lhs=s[0];
    size_t j=2;
    Matrix rhs=parse(s, j, vars);
    //assert(s[j] == '.');
    rhs.output();
    vars[lhs] = rhs;
  }
  printf("-----\n");
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

