#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;
typedef unsigned int uint;
typedef vector<uint> Vec;
typedef vector<Vec> Mat;

const int    INF = 1000000000;
const int    MOD = 1000000007;
const double EPS = 1e-8;
Mat mul(uint k, Mat A){
    for(int i = 0; i < A.size(); i++)
    for(int j = 0; j < A[i].size(); j++)
        A[i][j] *= k;
    return A;
}
Mat add(Mat A, Mat& B){
    assert(A.size() == B.size());
    assert(A[0].size() == B[0].size());
    for(int i = 0; i < A.size(); i++)
    for(int j = 0; j < A[i].size(); j++)
        A[i][j] += B[i][j];
    return A;
}
Mat mul(Mat& A, Mat& B){
    if(A.size() == 1 && A[0].size() == 1) return mul(A[0][0], B);
    if(B.size() == 1 && B[0].size() == 1) return mul(B[0][0], A);
    int H = A.size();
    int W = B[0].size();
    int N = A[0].size();
    assert(N == B.size());
    Mat res(H, Vec(W, 0));
    for(int i = 0; i < H; i++)
    for(int k = 0; k < N; k++)
    for(int j = 0; j < W; j++)
        res[i][j] += A[i][k] * B[k][j];
    return res;
}
// 2^15 - 1 = 0x7fff
struct Parser{
    typedef pair<Mat, int> Result;
    map<char, Mat> index;
    Mat parse(string& s){
        char var = s[0];
        string t = s.substr(2, s.size() - 3);
        Mat rval = expr(t, 0).first;
        return index[var] = rval;
    }
    Result expr(string& s, int k){
        Result r = term(s, k);
        Mat A = r.first;
        k = r.second;
        while(s[k] == '+' || s[k] == '-'){
            Result r_ = term(s, k + 1);
            if(s[k] == '-') r_.first = mul(-1, r_.first);
            A = add(A, r_.first);
            k = r_.second;
        }
        return Result(A, k);
    }
    Result term(string& s, int k){
        Result r = factor(s, k);
        Mat A = r.first;
        k = r.second;
        while(s[k] == '*'){
            Result r_ = term(s, k + 1);
            A = mul(A, r_.first);
            k = r_.second;
        }
        return Result(A, k);
    }
    Result factor(string& s, int k){
        bool minus = false;
        while(s[k] == '-'){
            k++;
            minus ^= 1;
        }
        Result r = primary(s, k);
        if(minus){ r.first = mul(-1, r.first); }
        return r;
    }
    Result primary(string& s, int k){
        //printf("primary(%s, %d)\n", s.c_str(), k);
        Mat A;
        // (expr)
        if(s[k] == '('){
            assert(s[k++] == '(');
            Result r = expr(s, k);
            A = r.first;
            k = r.second;
            assert(s[k++] == ')');
        }
        // matrix
        if(s[k] == '['){
            assert(s[k++] == '[');
            // [row; row; row]
            // row := expr expr expr
            Result r = row(s, k);
            A = r.first;
            k = r.second;
            while(s[k] == ';'){
                assert(s[k++] == ';');
                Result r_ = row(s, k);
                Mat B = r_.first;
                k = r_.second;
                A.insert(A.end(), B.begin(), B.end());
            }
            assert(s[k++] == ']');
        }
        // num
        if(isdigit(s[k])){
            uint num = 0;
            while(isdigit(s[k])){
                num = num * 10 + (s[k++] - '0');
            }
            A.push_back(Vec(1, num));
        }
        // var
        if(isalpha(s[k])){
            A = index[s[k++]];
        }

        // --- primary * ---
        while(s[k] == '(' || s[k] == '\''){
            if(s[k] == '('){
                assert(s[k++] == '(');
                Result r1 = expr(s, k);
                k = r1.second;
                assert(s[k++] == ',');
                Result r2 = expr(s, k);
                k = r2.second;
                assert(s[k++] == ')');
                Vec Y = r1.first[0];
                Vec X = r2.first[0];
                Mat B = A;
                A = Mat(Y.size(), Vec(X.size()));
                for(int yi = 0; yi < Y.size(); yi++)
                    for(int xi = 0; xi < X.size(); xi++)
                        A[yi][xi] = B[ Y[yi] - 1 ][ X[xi] - 1 ];
            }
            if(s[k] == '\''){
                assert(s[k++] == '\'');
                Mat B = A;
                A = Mat(B[0].size(), Vec(B.size()));
                for(int i = 0; i < A.size(); i++)
                    for(int j = 0; j < A[i].size(); j++)
                        A[i][j] = B[j][i];
            }
        }

        return Result(A, k);
    }
    Result row(string& s, int k){
        Result r = expr(s, k);
        Mat A = r.first;
        k = r.second;
        while(s[k] == ' '){
            assert(s[k++] == ' ');
            Result r_ = expr(s, k);
            Mat B = r_.first;
            k = r_.second;
            for(int i = 0; i < A.size(); i++){
                A[i].insert(A[i].end(), B[i].begin(), B[i].end());
            }
        }
        return Result(A, k);
    }
};

void print(Mat& A){
    for(int i = 0; i < A.size(); i++){
        for(int j = 0; j < A[i].size(); j++){
            printf("%u", A[i][j] & 0x7fff);
            if(j == A[i].size() - 1) putchar('\n');
            else putchar(' ');
        }
    }
}

int main(){
    int N;
    while(cin >> N && N){
        cin.ignore();
        Parser p;
        for(int i = 0; i < N; i++){
            string line;
            getline(cin, line);
            Mat res = p.parse(line);
            print(res);
        }
        cout << "-----" << endl;
    }

    return 0;
}

Measured memory_bytes: 1720