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

using namespace std;

const int MODMASK = 0x7fff;

struct Matrix {
	int N, M;
	vector< vector<int> > data;

	Matrix() : N(0), M(0) { }
	Matrix(int s) : N(1), M(1), data(1, vector<int>(1, s)) { }
	Matrix(int n, int m) : N(n), M(m), data(n, vector<int>(m)) { }

	bool isScalar() const { return N == 1 && M == 1; }

	Matrix &operator+=(const Matrix &a){
		for(int i = 0; i < N; ++i){
			for(int j = 0; j < M; ++j){
				data[i][j] += a.data[i][j];
				data[i][j] &= MODMASK;
			}
		}
		return *this;
	}
	Matrix &operator-=(const Matrix &a){
		for(int i = 0; i < N; ++i){
			for(int j = 0; j < M; ++j){
				data[i][j] -= a.data[i][j];
				data[i][j] &= MODMASK;
			}
		}
		return *this;
	}
	Matrix &operator*=(const Matrix &a){
		if(a.isScalar()){
			for(int i = 0; i < N; ++i){
				for(int j = 0; j < M; ++j){
					data[i][j] *= a.data[0][0];
					data[i][j] &= MODMASK;
				}
			}
		}else if(isScalar()){
			int scalar = data[0][0];
			*this = a;
			for(int i = 0; i < N; ++i){
				for(int j = 0; j < M; ++j){
					data[i][j] *= scalar;
					data[i][j] &= MODMASK;
				}
			}
		}else{
			Matrix b(N, a.M);
			for(int i = 0; i < N; ++i){
				for(int j = 0; j < a.M; ++j){
					for(int k = 0; k < M; ++k){
						b.data[i][j] += data[i][k] * a.data[k][j];
						b.data[i][j] &= MODMASK;
					}
				}
			}
			*this = b;
		}
		return *this;
	}

	Matrix operator-() const {
		Matrix a(N, M);
		for(int i = 0; i < N; ++i){
			for(int j = 0; j < M; ++j){ a.data[i][j] = -data[i][j] & MODMASK; }
		}
		return a;
	}

	Matrix &transpose(){
		Matrix a(M, N);
		for(int i = 0; i < N; ++i){
			for(int j = 0; j < M; ++j){ a.data[j][i] = data[i][j]; }
		}
		*this = a;
		return *this;
	}
	Matrix &select(const Matrix &a, const Matrix &b){
		Matrix c(a.M, b.M);
		for(int i = 0; i < a.M; ++i){
			for(int j = 0; j < b.M; ++j){
				c.data[i][j] = data[a.data[0][i] - 1][b.data[0][j] - 1];
			}
		}
		*this = c;
		return *this;
	}

	Matrix &appendRow(const Matrix &a){
		N += a.N;
		for(int i = 0; i < a.N; ++i){
			data.push_back(a.data[i]);
		}
		return *this;
	}
	Matrix &appendCol(const Matrix &a){
		M += a.M;
		for(int i = 0; i < N; ++i){
			for(int j = 0; j < a.M; ++j){
				data[i].push_back(a.data[i][j]);
			}
		}
		return *this;
	}
};

Matrix g_variables[26];

struct Result {
	int p;
	Matrix x;
};
#define PARSER_DECL(name) Result name (const string &s, int p)

PARSER_DECL(var);
PARSER_DECL(expr);
PARSER_DECL(term);
PARSER_DECL(factor);
PARSER_DECL(primary);
PARSER_DECL(matrix);
PARSER_DECL(row_seq);
PARSER_DECL(row);
PARSER_DECL(inum);

PARSER_DECL(var){
	Result r;
	r.x = g_variables[s[p] - 'A'];
	r.p = p + 1;
	return r;
}

PARSER_DECL(expr){
	Result r = term(s, p);
	while(s[r.p] == '+' || s[r.p] == '-'){
		char op = s[r.p++];
		Result a = term(s, r.p);
		if(op == '+'){
			r.x += a.x;
		}else{
			r.x -= a.x;
		}
		r.p = a.p;
	}
	return r;
}

PARSER_DECL(term){
	Result r = factor(s, p);
	while(s[r.p] == '*'){
		Result a = factor(s, ++r.p);
		r.x *= a.x;
		r.p = a.p;
	}
	return r;
}

PARSER_DECL(factor){
	int negNum = 0;
	while(s[p] == '-'){ ++p; ++negNum; }
	Result r = primary(s, p);
	if(negNum % 2 == 1){ r.x = -r.x; }
	return r;
}

PARSER_DECL(primary){
	Result r;
	if(isdigit(s[p])){
		r = inum(s, p);
	}else if(isupper(s[p])){
		r = var(s, p);
	}else if(s[p] == '['){
		r = matrix(s, p);
	}else if(s[p] == '('){
		r = expr(s, ++p);
		++r.p;
	}
	while(s[r.p] == '\'' || s[r.p] == '('){
		if(s[r.p] == '\''){
			r.x.transpose();
			++r.p;
		}else{
			Result first = expr(s, ++r.p);
			Result second = expr(s, first.p + 1);
			r.x.select(first.x, second.x);
			r.p = second.p + 1;
		}
	}
	return r;
}

PARSER_DECL(matrix){
	Result r = row_seq(s, ++p);
	++r.p;
	return r;
}

PARSER_DECL(row_seq){
	Result r = row(s, p);
	while(s[r.p] == ';'){
		Result a = row(s, ++r.p);
		r.x.appendRow(a.x);
		r.p = a.p;
	}
	return r;
}

PARSER_DECL(row){
	Result r = expr(s, p);
	while(s[r.p] == ' '){
		Result a = row(s, ++r.p);
		r.x.appendCol(a.x);
		r.p = a.p;
	}
	return r;
}

PARSER_DECL(inum){
	int x = 0;
	while(isdigit(s[p])){
		x = (x * 10) + s[p] - '0';
		++p;
	}
	Result r;
	r.x = Matrix(x & MODMASK);
	r.p = p;
	return r;
}

int main(){
	while(true){
		int N;
		cin >> N;
		if(N == 0){ break; }
		string input;
		getline(cin, input);
		while(N--){
			getline(cin, input);
			Matrix &lref = g_variables[input[0] - 'A'];
			Result r = expr(input, 2);
			lref = r.x;
			for(int i = 0; i < lref.N; ++i){
				for(int j = 0; j < lref.M; ++j){
					cout << lref.data[i][j] << (j == lref.M - 1 ? '\n' : ' ');
				}
			}
		}
		cout << "-----" << endl;
	}
	return 0;
}

Measured memory_bytes: 1240