#include <cctype>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using namespace std;

void my_assert(bool condition, const char *message) {
    if (!condition) {
        throw std::logic_error(message);
    }
}

void my_assert(const char *message) {
    return my_assert(false, message);
}

template <typename T>
class Matrix {
    private:
        unsigned h_, w_;
        T *a_;
    public:
        Matrix(): h_(0), w_(0), a_(new T[0]) {}
        Matrix(unsigned height, unsigned width):
        h_(height), w_(width), a_(new T[h_ * w_]) {}
        Matrix(const Matrix &m): h_(m.h_), w_(m.w_), a_(new T[h_ * w_]) {
            for (unsigned i = 0; i < h_ * w_; ++i) {
                a_[i] = m.a_[i];
            }
        }
        virtual ~Matrix() {
            delete[] a_;
        }
        unsigned height() const {
            return h_;
        }
        unsigned width() const {
            return w_;
        }
        T &operator()(unsigned i, unsigned j) {
            my_assert(i < h_ && j < w_, "cannot access");
            return a_[i * w_ + j];
        }
        T &operator()(unsigned i, unsigned j) const {
            my_assert(i < h_ && j < w_, "cannot access");
            return a_[i * w_ + j];
        }
        Matrix operator+() const {
            return *this;
        }
        Matrix operator-() const {
            Matrix retval(h_, w_);
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    retval(i, j) = -(*this)(i, j);
                }
            }
            return retval;
        }
        Matrix operator+(const Matrix &m) const {
            my_assert(h_ == m.h_ && w_ == m.w_, "two matrix sizes differ");
            Matrix retval(m.h_, m.w_);
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    retval(i, j) = (*this)(i, j) + m(i, j);
                }
            }
            return retval;
        }
        Matrix operator+=(const Matrix &m) {
            my_assert(h_ == m.h_ && w_ == m.w_, "two matrix sizes differ");
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    (*this)(i, j) += m(i, j);
                }
            }
            return *this;
        }
        Matrix operator-(const Matrix &m) const {
            my_assert(h_ == m.h_ && w_ == m.w_, "two matrix sizes differ");
            Matrix retval(m.h_, m.w_);
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    retval(i, j) = (*this)(i, j) - m(i, j);
                }
            }
            return retval;
        }
        Matrix operator-=(const Matrix &m) {
            my_assert(h_ == m.h_ && w_ == m.w_, "two matrix sizes differ");
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    (*this)(i, j) -= m(i, j);
                }
            }
            return *this;
        }
        Matrix operator*(const Matrix &m) const {
            if (h_ * w_ == 1) {
                Matrix retval(m.h_, m.w_);
                for (unsigned i = 0; i < m.h_; ++i) {
                    for (unsigned j = 0; j < m.w_; ++j) {
                        retval(i, j) = (*this)(0, 0) * m(i, j);
                    }
                }
                return retval;
            } else if (m.h_ * m.w_ == 1) {
                return m * *this;
            } else {
                my_assert(w_ == m.h_, "width of left differs from height of right");
                Matrix retval(h_, m.w_);
                for (unsigned i = 0; i < h_; ++i) {
                    for (unsigned j = 0; j < m.w_; ++j) {
                        retval(i, j) = T();
                        for (unsigned k = 0; k < w_; ++k) {
                            retval(i, j) += (*this)(i, k) * m(k, j);
                        }
                    }
                }
                return retval;
            }
        }
        Matrix operator*=(const Matrix &m) {
            *this = *this * m;
            return *this;
        }
        Matrix &operator=(const Matrix &m) {
            if (h_ != m.h_ || w_ != m.w_) {
                delete[] a_;
                a_ = new T[m.h_ * m.w_];
            }
            h_ = m.h_;
            w_ = m.w_;
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    (*this)(i, j) = m(i, j);
                }
            }
            return *this;
        }
        Matrix trans() {
            Matrix retval(w_, h_);
            for (unsigned i = 0; i < h_; ++i) {
                for (unsigned j = 0; j < w_; ++j) {
                    retval(j, i) = (*this)(i, j);
                }
            }
            return retval;
        }
};

template <unsigned M>
class Mod {
    private:
        unsigned x_;
        unsigned f(int x) const {
            return  x < 0? (M - (-x) % M) % M: x % M;
        }
    public:
        Mod(): x_(0) {}
        Mod(int x): x_(f(x)) {}
        Mod(const Mod &m): x_(m.x_) {}
        Mod operator+() {
            return *this;
        }
        Mod operator-() {
            return Mod(-int(x_));
        }
        Mod &operator=(const Mod &m) {
            x_ = m.x_;
            return *this;
        }
        Mod operator+(const Mod &m) const {
            return f(x_ + m.x_);
        }
        Mod &operator+=(const Mod &m) {
            *this = *this + m;
            return *this;
        }
        Mod operator-(const Mod &m) const {
            return f(x_ - m.x_);
        }
        Mod &operator-=(const Mod &m) {
            *this = *this - m;
            return *this;
        }
        Mod operator*(const Mod &m) const {
            return f(x_ * m.x_);
        }
        Mod &operator*=(const Mod &m) {
            *this = *this * m;
            return *this;
        }
        operator int() const {
            return x_;
        }
};

typedef Mod<32768> Int;
typedef Matrix<Int> M;

M join_top_bottom(const M &top, const M &bottom) {
    my_assert(top.width() == bottom.width(), "widths of matrices differ");
    M retval(top.height() + bottom.height(), top.width());
    for (unsigned i = 0; i < retval.height(); ++i) {
        for (unsigned j = 0; j < retval.width(); ++j) {
            retval(i, j) = (i < top.height()? top(i, j): bottom(i - top.height(), j));
        }
    }
    return retval;
}

M join_left_right(const M &left, const M &right) {
    my_assert(left.height() == right.height(), "heights of matrices differ");
    M retval(left.height(), left.width() + right.width());
    for (unsigned i = 0; i < retval.height(); ++i) {
        for (unsigned j = 0; j < retval.width(); ++j) {
            retval(i, j) = (j < left.width()? left(i, j): right(i, j - left.width()));
        }
    }
    return retval;
}

M indexed_primary(const M &m, const M &first, const M &second) {
    my_assert(first.height() == 1 && second.height() == 1, "invalid index matrix");
    M retval(first.width(), second.width());
    for (size_t i = 0; i < retval.height(); ++i) {
        for (size_t j = 0; j < retval.width(); ++j) {
            retval(i, j) = m(first(0, i) - Int(1), second(0, j) - Int(1));
        }
    }
    return retval;
}

void print_matrix(const M &m) {
    for (unsigned i = 0; i < m.height(); ++i) {
        for (unsigned j = 0; j < m.width(); ++j) {
            cout << m(i, j);
            j < m.width() - 1? cout << " ": cout << endl;
        }
    }
}

class Solver {
    private:
        M vars_[26];
        M inum(const string &line, size_t &cur, const size_t end) {
            my_assert(isdigit(line[cur]), "invalid char (inum)");
            M m(1, 1);
            m(0, 0) = 0;
            while (isdigit(line[cur])) {
                m(0, 0) = 10 * m(0, 0) + (line[cur] - '0');
                ++cur;
            }
            return m;
        }
        M row(const string &line, size_t &cur, const size_t end) {
            M m;
            m = expr(line, cur, end);
            while (cur < end) {
                if (line[cur] != ' ') {
                    break;
                }
                ++cur;
                m = join_left_right(m, expr(line, cur, end));
            }
            return m;
        }
        M matrix(const string &line, size_t &cur, const size_t end) {
            my_assert(line[cur] == '[', "invalid begin (matrix)");
            M m;
            ++cur;
            m = row(line, cur, end);
            while (cur < end) {
                if (line[cur] == ']') {
                    break;
                }
                my_assert(line[cur] == ';', "invalid row-seq end (matrix)");
                ++cur;
                m = join_top_bottom(m, row(line, cur, end));
            }
            my_assert(line[cur] == ']', "invalid end (matrix)");
            ++cur;
            return m;
        }
        M primary(const string &line, size_t &cur, const size_t end) {
            M m;
            if (isdigit(line[cur])) {
                m = inum(line, cur, end);
            } else if (isupper(line[cur])) {
                m = vars_[line[cur] - 'A'];
                ++cur;
            } else if (line[cur] == '[') {
                m = matrix(line, cur, end);
            } else if (line[cur] == '(') {
                ++cur;
                m = expr(line, cur, end);
                my_assert(line[cur] == ')', "invalid expr end (primary)");
                ++cur;
            } else {
                my_assert("invalid char (primary)");
            }
            while (cur < end) {
                M first, second;
                bool is_trans = false;
                while (line[cur] == '\'') {
                    is_trans = !is_trans;
                    ++cur;
                }
                if (is_trans) {
                    m = m.trans();
                }
                if (line[cur] == '(') {
                    ++cur;
                    first = expr(line, cur, end);
                    my_assert(line[cur] == ',', "invalid char (primary)");
                    ++cur;
                    second = expr(line, cur, end);
                    my_assert(line[cur] == ')', "invalid char (primary)");
                    m = indexed_primary(m, first, second);
                    ++cur;
                }
                if (line[cur] != '\'' && line[cur] != '(') {
                    break;
                }
            }
            return m;
        }
        M factor(const string &line, size_t &cur, const size_t end) {
            bool is_negative = false;
            while (line[cur] == '-') {
                is_negative = !is_negative;
                ++cur;
            }
            return is_negative? -primary(line, cur, end): primary(line, cur, end);
        }
        M term(const string &line, size_t &cur, const size_t end) {
            M m1, m2;
            m1 = factor(line, cur, end);
            while (cur < end) {
                if (line[cur] != '*') {
                    break;
                }
                ++cur;
                M m2 = term(line, cur, end);
                m1 *= m2;
            }
            return m1;
        }
        M expr(const string &line, size_t &cur, const size_t end) {
            M m1, m2;
            m1 = term(line, cur, end);
            while (cur < end) {
                char op = line[cur];
                if (op != '+' && op != '-' && op != '*') {
                    break;
                }
                ++cur;
                m2 = term(line, cur, end);
                switch (op) {
                    case '+':
                        m1 += m2;
                        break;
                    case '-':
                        m1 -= m2;
                        break;
                    case '*':
                        m1 *= m2;
                        break;
                }
            }
            return m1;
        }
    public:
        M &interpret(string &line) {
            size_t cur = 2;
            vars_[line[0] - 'A'] = expr(line, cur, line.length() - 1);
            return vars_[line[0] - 'A'];
        }
};

int main() {
    unsigned n;
    string line;
    Solver s;
    while (true) {
        cin >> n;
        if (n == 0) {
            return 0;
        }
        cin.ignore();
        for (unsigned i = 0; i < n; ++i) {
            getline(cin, line);
            print_matrix(s.interpret(line));
        }
        cout << "-----" << endl;
    }
}

Measured memory_bytes: 1644