#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

typedef vector< vector<int> > Matrix;

const int MOD = 1<<15;

Matrix mem[26];

Matrix parseExpr(const string &str, int &idx);
Matrix parseTerm(const string &str, int &idx);
Matrix parseFactor(const string &str, int &idx);
Matrix parsePrimary(const string &str, int &idx);
Matrix parseRowseq(const string &str, int &idx);

void printMatrix(const Matrix &A){
	int n = A.size(), m = A[0].size();
	for(int i=0;i<n;i++){
		for(int j=0;j+1<m;j++)
			cout << A[i][j] << " ";
		cout << A[i].back() << endl;
	}
}

Matrix add(const Matrix &A, const Matrix &B){
	int n = A.size(), m = A[0].size();
	Matrix res(n, vector<int>(m, 0));
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			res[i][j] = (A[i][j] + B[i][j])%MOD;
	return res;
}

Matrix sub(const Matrix &A, const Matrix &B){
	int n = A.size(), m = A[0].size();
	Matrix res(n, vector<int>(m, 0));
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			res[i][j] = (A[i][j] - B[i][j] + MOD)%MOD;
	return res;
}

Matrix mul(const Matrix &A, const Matrix &B){
	int nA = A.size(), mA = A[0].size(), nB = B.size(), mB = B[0].size();
	if(nA == 1 && mA == 1){
		Matrix res = B;
		for(int i=0;i<nB;i++)
			for(int j=0;j<mB;j++)
				res[i][j] = (B[i][j]*((A[0][0]+MOD)%MOD))%MOD;
		return res;
	}
	if(nB == 1 && mB == 1){
		Matrix res = A;
		for(int i=0;i<nA;i++)
			for(int j=0;j<mA;j++)
				res[i][j] = (A[i][j]*((B[0][0]+MOD)%MOD))%MOD;
		return res;
	}
	Matrix res(nA, vector<int>(mB,0));
	for(int i=0;i<nA;i++){
		for(int j=0;j<mB;j++){
			for(int k=0;k<mA;k++)
				res[i][j] = (res[i][j]+(A[i][k]*B[k][j])%MOD)%MOD;
		}
	}
	return res;
}

Matrix trans(const Matrix &A){
	int n = A.size(), m = A[0].size();
	Matrix res(m, vector<int>(n,0));
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			res[j][i] = A[i][j];
	return res;
}

Matrix primary(const Matrix &A, const Matrix &P1, const Matrix &P2){
	int n = P1[0].size(), m = P2[0].size();
	Matrix res(n, vector<int>(m,0));
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			res[i][j] = A[P1[0][i]-1][P2[0][j]-1];
	return res;
}

void pushMatrix(Matrix &A, const Matrix &B, int x){
	int n = B.size(), m = B[0].size();
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(x+i >= A.size()) A.push_back(vector<int>(0));
			A[x+i].push_back(B[i][j]);
		}
	}
}

Matrix parseExpr(const string &str, int &idx){
	Matrix res = parseTerm(str, idx);
	while(str[idx] == '+'||str[idx] == '-'){
		char op = str[idx++];
		Matrix tmp = parseTerm(str, idx);
		if(op == '+') res = add(res, tmp);
		if(op == '-') res = sub(res, tmp);
	}
	return res;
}

Matrix parseTerm(const string &str, int &idx){
	Matrix res = parseFactor(str, idx);
	while(str[idx] == '*'){
		idx++;
		Matrix tmp = parseFactor(str, idx);
		res = mul(res, tmp);
	}
	return res;
}

Matrix parseFactor(const string &str, int &idx){
	if(str[idx] == '-'){
		idx++;
		return mul(parseFactor(str, idx), Matrix(1,vector<int>(1,MOD-1)));
	}
	return parsePrimary(str, idx);
}

Matrix parsePrimary(const string &str, int &idx){
	Matrix res; res.clear();
	if(isdigit(str[idx])){
		int val = 0;
		while(isdigit(str[idx]))
			val = 10*val+str[idx++]-'0';
		res.push_back(vector<int>(1,val));
	} else if(isupper(str[idx])){
		res = mem[str[idx++]-'A'];
	} else if(str[idx]=='['){
		idx++;
		res = parseRowseq(str, idx);
		idx++;
	} else if(str[idx]=='('){
		idx++;
		res = parseExpr(str, idx);
		idx++;
	}
	while(true){
		if(str[idx]=='\''){
			idx++;
			res = trans(res);
		}
		else if(str[idx]=='('){
			idx++;
			Matrix P1 = parseExpr(str, idx);
			idx++;
			Matrix P2 = parseExpr(str, idx);
			idx++;
			res = primary(res, P1, P2);
		}
		else break;
	}
	return res;
}

Matrix parseRowseq(const string &str, int &idx){
	Matrix res; int x = 0;
	while(true){
		Matrix tmp = parseExpr(str, idx);
		pushMatrix(res, tmp, x);
		if(str[idx]==';') x = res.size();
		if(str[idx]==']') break;
		idx++;
	}
	return res;
}

int main(){
	int n;
	while(cin >> n, n){
		string str; getline(cin, str);
		for(int i=0;i<n;i++){
			getline(cin, str);
			int idx = 2;
			mem[str[0]-'A'] = parseExpr(str, idx);
			printMatrix(mem[str[0]-'A']);
		}
		puts("-----");
	}
}

Measured memory_bytes: 1284