#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstring>
using namespace std;

const int MOD = 32768;

string str;
int p;

class Matrix
{
public:
	int W,H;
	int v[105][105];
	Matrix()
	{
		memset(v,0,sizeof(v));
	}

	Matrix(int W, int H)
		:W(W),H(H)
	{
		memset(v,0,sizeof(v));
	}

	bool isaaa()
	{
		return (W==1&&H==1);
	}
};

map<char, Matrix> dic;

Matrix expr();
Matrix term();
Matrix factor();
Matrix primary();
Matrix inum();
Matrix row_seq();
Matrix row();



Matrix add(Matrix a, Matrix b)
{
	Matrix res(a.W, a.H);
	for(int i=0; i<a.H; i++)
	for(int j=0; j<a.W; j++) {
		res.v[j][i] = (a.v[j][i] + b.v[j][i]) % MOD;
	}

	return res;
}

Matrix sub(Matrix a, Matrix b)
{
	Matrix res(a.W, a.H);
	for(int i=0; i<a.H; i++)
	for(int j=0; j<a.W; j++) 
		res.v[j][i] = (a.v[j][i] - b.v[j][i] + MOD) % MOD;

	return res;
}

Matrix trans(Matrix a)
{
	Matrix res(a.H, a.W);
	for(int i=0; i<a.H; i++)
	for(int j=0; j<a.W; j++)
		res.v[i][j] = a.v[j][i];

	return res;
}

Matrix mul(Matrix a, Matrix b)
{
	if(b.isaaa()) swap(a,b);

	if(a.isaaa()) {
		for(int i=0; i<b.H; i++)
		for(int j=0; j<b.W; j++) {
			b.v[j][i] *= a.v[0][0];
			b.v[j][i] %= MOD;
		}

		return b;
	}
	else
	{
		Matrix res(b.W, a.H);
		for(int i=0; i<a.H; i++)
		for(int j=0; j<b.W; j++) {
			for(int k=0; k<a.W;k++) {
				res.v[j][i] += a.v[k][i] * b.v[j][k];
				res.v[j][i] %= MOD;
			}
		}

		return res;
	}
}

Matrix neg(Matrix a)
{
	Matrix res(a.W, a.H);

	for(int i=0; i<a.H; i++)
	for(int j=0; j<a.W; j++) {
		res.v[j][i] = ( -a.v[j][i] + MOD ) % MOD;
	}

	return res;
}


Matrix expr()
{
	Matrix a = term();
	while(str[p] == '+' || str[p] == '-') {
		char pm = str[p++];
		Matrix b = term();

		if(pm == '+') a = add(a,b);
		if(pm == '-') a = sub(a,b);
	}

	return a;
}

Matrix term()
{
	Matrix a = factor();
	while(str[p] == '*') {
		p++;
		Matrix b = term();

		a = mul(a,b);
	}

	return a;
}

Matrix factor()
{
	bool ng = false;
	while(str[p]=='-') {
		p++;
		ng = !ng;
	}

	Matrix a = primary();
	if(ng) a = neg(a);

	return a;
}

Matrix primary()
{
	Matrix a;
	if(isdigit(str[p])) {
		a = inum();
	}
	else if(isalpha(str[p])) {
		a = dic[str[p]];
		p++;
	}
	else if(str[p]=='[') {
		p++;
		a = row_seq();
		p++;
	}
	else if(str[p]=='(') {
		p++;
		a = expr();
		p++;
	}

	while(str[p] == '(' || str[p] == '\'') {
		if(str[p]=='(') {
			p++;
			Matrix b = expr();
			p++;
			Matrix c = expr();
			p++;

			Matrix tmp(c.W,b.W);
			for(int i=0; i<b.W; i++)
			for(int j=0; j<c.W; j++)
				tmp.v[j][i] = a.v[c.v[j][0]-1][b.v[i][0]-1];
			a = tmp;

		}
		else if(str[p] == '\'') {
			p++;
			a = trans(a);
		}
	}


	return a;
}

Matrix row_seq()
{
	Matrix a = row();
	while(str[p] == ';') {
		p++;
		Matrix b = row();

		int nH = a.H + b.H;
		for(int i=a.H; i<nH; i++)
		for(int j=0; j<a.W; j++)
			a.v[j][i] = b.v[j][i-a.H];

		a.H = nH;
	}

	return a;
}

Matrix row()
{
	Matrix a = expr();
	while(str[p] == ' ') {
		p++;
		Matrix b = expr();
		
		int nW = a.W+b.W;
		for(int i=0; i<b.H; i++)
		for(int j=a.W; j<nW; j++)
			a.v[j][i] = b.v[j-a.W][i];

		a.W = nW;
	}

	return a;
}

Matrix inum()
{
	Matrix res(1,1);
	int v = 0;

	while(isdigit(str[p])) {
		v *= 10;
		v += str[p] - '0';
		p++;
	}

	res.v[0][0] = v;

	return res;
}

void print(Matrix a)
{
	for(int i=0; i<a.H; i++) {
		for(int j=0; j<a.W; j++) {
			if(j!=0) cout << " ";
			cout << a.v[j][i];
		}

		cout << endl;
	}
}

int main()
{
	int N;
	while(cin >> N, N) {
		getline(cin, str);
		while(N--) {
			getline(cin, str);
			p=2;
			Matrix res = expr();

			dic[str[0]] = res;

			print(res);
		}

		cout << "-----" << endl;
	}
}

Measured memory_bytes: 5032