#include <bits/stdc++.h>
using namespace std;

template <int M, bool IsPrime = false>
class modulo {
    using ll = long long;
public:
    modulo() : n(0) {}
    modulo(int m) : n(m) {
        if(n >= M) {
            n %= M;
        } else if(n < 0) {
            n = (n % M + M) % M;
        }
    }
    modulo(ll m) {
        if(m >= M) {
            m %= M;
        } else if(m < 0) {
            m = (m % M + M) % M;
        }
        n = m;
    }

    explicit operator int() const {
        return n;
    }
    explicit operator ll() const {
        return n;
    }
    bool operator==(modulo const& a) const {
        return n == a.n;
    }

    modulo& operator+=(modulo const& a) {
        n += a.n;
        if(n >= M) {
            n -= M;
        }
        return *this;
    }
    modulo& operator-=(modulo const& a) {
        n -= a.n;
        if(n < 0) {
            n += M;
        }
        return *this;
    }
    modulo& operator*=(modulo const& a) {
        n = (ll(n) * a.n) % M;
        return *this;
    }
    modulo operator^(int n) const {
        if(n == 0) {
            return modulo(1);
        }
        const modulo a = *this;
        modulo res = (a * a) ^ (n / 2);
        return n % 2 ? res * a : res;
    }

private:
    ll n;
};

template <int M, bool isPrime>
modulo<M, isPrime> operator-(modulo<M, isPrime> m) {
    modulo<M, isPrime> res(-(int)m);
    return res;
}

template <int M, bool IsPrime>
modulo<M, IsPrime> operator+(modulo<M, IsPrime> lhs, modulo<M, IsPrime> const& rhs) {
    lhs += rhs;
    return lhs;
}
template <int M, bool IsPrime>
modulo<M, IsPrime> operator-(modulo<M, IsPrime> lhs, modulo<M, IsPrime> const& rhs) {
    lhs -= rhs;
    return lhs;
}
template <int M, bool IsPrime>
modulo<M, IsPrime> operator*(modulo<M, IsPrime> lhs, modulo<M, IsPrime> const& rhs) {
    lhs *= rhs;
    return lhs;
}

const int MOD = 32768;
using mod = modulo<MOD, false>;

template<typename T>
using matrix = std::vector<std::vector<T>>;

template<typename T>
matrix<T> operator-(matrix<T> m) {
    for(int i=0; i<m.size(); ++i) {
        for(int j=0; j<m[i].size(); ++j) {
            m[i][j] = -m[i][j];
        }
    }
    return m;
}

template<typename T>
matrix<T> operator+(matrix<T> lhs, matrix<T> const& rhs) {
    assert(lhs.size() == rhs.size() && lhs[0].size() == rhs[0].size());
    for(int i=0; i<lhs.size(); ++i) {
        for(int j=0; j<lhs[i].size(); ++j) {
            lhs[i][j] += rhs[i][j];
        }
    }
    return lhs;
}

template<typename T>
matrix<T> operator-(matrix<T> lhs, matrix<T> const& rhs) {
    assert(lhs.size() == rhs.size() && lhs[0].size() == rhs[0].size());
    for(int i=0; i<lhs.size(); ++i) {
        for(int j=0; j<lhs[i].size(); ++j) {
            lhs[i][j] -= rhs[i][j];
        }
    }
    return lhs;
}

template<typename T>
matrix<T> operator*(T lhs, matrix<T> const& rhs) {
    auto res = rhs;
    for(int i=0; i<res.size(); ++i) {
        for(int j=0; j<res[i].size(); ++j) {
            res[i][j] *= lhs;
        }
    }
    return res;
}

template<typename T>
matrix<T> operator*(matrix<T> const& lhs, matrix<T> const& 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;
    }
    assert(lhs[0].size() == rhs.size());
    matrix<T> res(lhs.size(), vector<T>(rhs[0].size()));
    for(int i=0; i<lhs.size(); ++i) {
        for(int j=0; j<rhs[0].size(); ++j) {
            for(int k=0; k<rhs.size(); ++k) {
                res[i][j] += lhs[i][k] * rhs[k][j];
            }
        }
    }
    return res;
}

template <typename T>
matrix<T> transpose(matrix<T> const& m) {
    const int R = m.size(), C = m[0].size();
    matrix<T> res(C, vector<T>(R));
    for(int i=0; i<C; ++i) {
        for(int j=0; j<R; ++j) {
            res[i][j] = m[j][i];
        }
    }
    return res;
}

template<typename T>
void print(matrix<T> const& m) {
    for(int i=0; i<m.size(); ++i) {
        for(int j=0; j<m[i].size(); ++j) {
            if(j != 0) {
                cout << ' ';
            }
            cout << (int)m[i][j];
        }
        cout << endl;
    }
}


map<char, matrix<mod>> var;

matrix<mod> row(string const& s, int& p);
matrix<mod> row_seq(string const& s, int& p);
matrix<mod> matrix_(string const& s, int& p);
matrix<mod> expr(string const& s, int& p);
matrix<mod> term(string const& s, int& p);
matrix<mod> factor(string const& s, int& p);
matrix<mod> primary(string const& s, int& p);
matrix<mod> inum(string const& s, int& p);


void pushc(matrix<mod>& res, matrix<mod> const& m) {
    const int R = res.size();
    const int C = m[0].size();
    for(int i=0; i<R; ++i) {
        for(int j=0; j<C; ++j) {
            res[i].push_back(m[i][j]);
        }
    }
}

void pushr(matrix<mod>& res, matrix<mod> const& m) {
    for(int i=0; i<m.size(); ++i) {
        res.push_back(m[i]);
    }
}

matrix<mod> indexed(matrix<mod> const& m, matrix<mod> const& l, matrix<mod> const& r) {
    matrix<mod> res(l[0].size(), vector<mod>(r[0].size()));
    for(int i=0; i<l[0].size(); ++i) {
        for(int j=0; j<r[0].size(); ++j) {
            res[i][j] = m[(int)(l[0][i] - mod(1))][(int)(r[0][j] - mod(1))];
        }
    }
    return res;
}

matrix<mod> row(string const& s, int& p) {
    matrix<mod> res = expr(s, p);
    while(p < s.size() && s[p] == ' ') {
        pushc(res, row(s, ++p));
    }
    return res;
}

matrix<mod> row_seq(string const& s, int& p) {
    auto res = row(s, p);
    while(p < s.size() && s[p] == ';') {
        pushr(res, row(s, ++p));
    }
    return res;
}

matrix<mod> matrix_(string const& s, int& p) {
    assert(s[p] == '[');
    auto res = row_seq(s, ++p);
    ++p;
    return res;
}

matrix<mod> expr(string const& s, int& p) {
    matrix<mod> res = term(s, p);
    while(p < s.size() && (s[p] == '+' || s[p] == '-')) {
        char op = s[p++];
        if(op == '+') {
            res = res + term(s, p);
        } else {
            res = res - term(s, p);
        }
    }
    return res;
}

matrix<mod> term(string const& s, int& p) {
    matrix<mod> res = factor(s, p);
    while(p < s.size() && s[p] == '*') {
        res = res * factor(s, ++p);
    }
    return res;
}

matrix<mod> factor(string const& s, int& p) {
    if(s[p] == '-') {
        return -factor(s, ++p);
    }
    return primary(s, p);
}

matrix<mod> primary(string const& s, int& p) {
    matrix<mod> res;
    if(isdigit(s[p])) {
        res = inum(s, p);
    } else if(isalpha(s[p])) {
        res = var[s[p++]];
    } else if(s[p] == '(') {
        res = expr(s, ++p);
        ++p;
    } else {
        res = matrix_(s, p);
    }
    while(p < s.size()) {
        if(s[p] == '(') {
            auto l = expr(s, ++p);
            auto r = expr(s, ++p);
            res = indexed(res, l, r);
            ++p;
        } else if(s[p] == '\'') {
            res = transpose(res);
            ++p;
        } else {
            break;
        }
    }
    return res;
}

matrix<mod> inum(string const& s, int& p) {
    mod res(0);
    while(p < s.size() && isdigit(s[p])) {
        res *= 10;
        res += (int)(s[p++] - '0');
    }
    return matrix<mod>(vector<vector<mod>>(1, vector<mod>(1, res)));
}


matrix<mod> assignment(string const& s, int& p) {
    char v = s[p];
    p += 2;
    var[v] = expr(s, p);
    return var[v];
}

void solve(vector<string> const& lines) {
    for(auto& s : lines) {
        int p = 0;
        print(assignment(s, p));
    }
}

int main() {
    int n;
    while(cin >> n, n) {
        cin.ignore(10000, '\n');
        vector<string> lines(n);
        for(int i=0; i<n; ++i) {
            getline(cin, lines[i]);
        }
        solve(lines);
        cout << "-----" << endl;
    }
}
