#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
//#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define MOD 32768
typedef vector<int> V;
typedef vector<V> MATRIX;

int N;
int global_index;
char buf[105];
MATRIX TABLE[26];

MATRIX expr();

MATRIX operator+(const MATRIX &A,const MATRIX &B){

	int num_row = A.size();
	int num_col = A[0].size();

	MATRIX RET(num_row,V(num_col));

	for(int row = 0; row < num_row; row++){
		for(int col = 0; col < num_col; col++){
			RET[row][col] = A[row][col]+B[row][col];
			RET[row][col] %= MOD;
		}
	}

	return RET;
}

MATRIX operator-(const MATRIX &A,const MATRIX &B){

	int num_row = A.size();
	int num_col = A[0].size();

	MATRIX RET(num_row,V(num_col));

	for(int row = 0; row < num_row; row++){
		for(int col = 0; col < num_col; col++){
			RET[row][col] = A[row][col]-B[row][col];
			RET[row][col] = (RET[row][col]+MOD)%MOD;
		}
	}

	return RET;
}

MATRIX operator*(const MATRIX &A, const MATRIX &B){

	int num_rowA = A.size();
	int num_colA = A[0].size();

	int num_rowB = B.size();
	int num_colB = B[0].size();

	if(num_rowA == 1 && num_colA == 1){ //Aがスカラー

		MATRIX RET(num_rowB,V(num_colB));

		for(int row = 0; row < num_rowB; row++){
			for(int col = 0; col < num_colB; col++){
				RET[row][col] = B[row][col]*A[0][0];
				RET[row][col] %= MOD;
			}
		}

		return RET;

	}else if(num_rowB == 1 && num_colB == 1){ //Bがスカラー

		MATRIX RET(num_rowA,V(num_colA));

		for(int row = 0; row < num_rowA; row++){
			for(int col = 0; col < num_colA; col++){
				RET[row][col] = A[row][col]*B[0][0];
				RET[row][col] %= MOD;
			}
		}

		return RET;

	}else{

		MATRIX RET(num_rowA,V(num_colB));

		for(int row = 0; row < num_rowA; row++){
			for(int col = 0; col < num_colB; col++){
				RET[row][col] = 0;
				for(int a = 0; a < num_colA; a++){
					RET[row][col] += A[row][a]*B[a][col];
					RET[row][col] %= MOD;
				}
			}
		}

		return RET;
	}
}

MATRIX operator-(const MATRIX &A){


	int num_rowA = A.size();
	int num_colA = A[0].size();
	MATRIX RET(num_rowA,V(num_colA));

	for(int row = 0; row < num_rowA; row++){
		for(int col = 0; col < num_colA; col++){
			RET[row][col] = (-A[row][col]+MOD)%MOD;
		}
	}
	return RET;
}

MATRIX transpose(MATRIX A){

	int num_row = A[0].size();
	int num_col = A.size();

	MATRIX RET(num_row,V(num_col));

	for(int row = 0; row < A.size(); row++){
		for(int col = 0; col < A[0].size(); col++){
			RET[col][row] = A[row][col];
		}
	}

	return RET;
}

MATRIX get_row(){

	MATRIX A = expr();

	while(buf[global_index] == ' '){
		global_index++;
		MATRIX B = expr();
		for(int a = 0; a < A.size(); a++){
			A[a].insert(A[a].end(),B[a].begin(),B[a].end());
		}
	}
	return A;
}


MATRIX row_seq(){
	MATRIX A = get_row();

	while(buf[global_index] == ';'){
		global_index++;
		MATRIX B = get_row();
		A.insert(A.end(),B.begin(),B.end());
	}

	return A;
}

MATRIX matrix(){

	global_index++;
	MATRIX A = row_seq();
	global_index++;

	return A;
}

MATRIX get_var(){

	MATRIX RET = TABLE[buf[global_index]-'A'];
	global_index++;

	return RET;
}

MATRIX inum(){

	int tmp = 0;

	while(buf[global_index] >= '0' && buf[global_index] <= '9'){
		tmp = 10*tmp+(buf[global_index]-'0');
		tmp %= MOD;
		global_index++;
	}

	MATRIX RET(1,V(1));

	RET[0][0] = tmp;

	return RET;
}



MATRIX primary(){

	MATRIX A;

	if(buf[global_index] >= '0' && buf[global_index] <= '9'){

		A = inum();

	}else if(buf[global_index] >= 'A' && buf[global_index] <= 'Z'){

		A = get_var();

	}else if(buf[global_index] == '['){

		A = matrix();

	}else if(buf[global_index] == '('){
		global_index++;
		A = expr();
		global_index++;
	}

	bool FLG;

	while(true){
		FLG = false;

		if(buf[global_index] == '('){

			FLG = true;
			global_index++;
			MATRIX B = expr();
			global_index++;
			MATRIX C = expr();
			global_index++;

			int num_row = B[0].size();
			int num_col = C[0].size();

			MATRIX TMP(num_row,V(num_col));

			for(int row = 0; row < num_row; row++){
				for(int col = 0; col < num_col; col++){
					TMP[row][col] = A[B[0][row]-1][C[0][col]-1];
				}
			}
			A = TMP;

		}else if(buf[global_index] == '\''){
			FLG = true;
			global_index++;
			A = transpose(A);
		}

		if(!FLG)break;
	}
	return A;
}


MATRIX factor(){

	if(buf[global_index] == '-'){

		global_index++;
		return -factor();

	}else{

		return primary();
	}
}

MATRIX term(){

	MATRIX A = factor();

	while(buf[global_index] == '*'){
		global_index++;
		A = A*factor();
	}
	return A;
}

MATRIX expr(){

	MATRIX A = term();
	char op;

	while(buf[global_index] == '+' || buf[global_index] == '-'){

		op = buf[global_index++];

		if(op == '+'){

			A = A+term();

		}else{

			A = A-term();
		}
	}

	return A;
}

void assignment(){

	int type = buf[0]-'A';
	global_index = 2;
	TABLE[type] = expr();

	global_index++;
}

void func(){

	getchar();
	for(int loop = 0; loop < N; loop++){
		fgets(buf,100,stdin);

		assignment();

		MATRIX A = TABLE[buf[0]-'A'];
		int num_row = A.size();
		int num_col = A[0].size();

		for(int row = 0; row < num_row; row++){
			printf("%d",A[row][0]);
			for(int col = 1; col < num_col; col++){
				printf(" %d",A[row][col]);
			}
			printf("\n");
		}
	}
	printf("-----\n");
}


int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}



Measured memory_bytes: 3380